3 * scan.c scan a directory (on a removable file) for a valid
4 * Volume name. If found, open the file for append.
11 Copyright (C) 2006 Kern Sibbald
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License
15 version 2 as amended with additional clauses defined in the
16 file LICENSE in the main source directory.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 the file LICENSE for additional details.
28 /* Forward referenced functions */
29 static bool is_volume_name_legal(char *name);
32 bool DEVICE::scan_dir_for_volume(DCR *dcr)
35 struct dirent *entry, *result;
38 VOLUME_CAT_INFO dcrVolCatInfo, devVolCatInfo;
41 POOL_MEM fname(PM_FNAME);
42 bool need_slash = false;
46 name_max = pathconf(".", _PC_NAME_MAX);
47 if (name_max < 1024) {
51 if (device->mount_point) {
52 mount_point = device->mount_point;
54 mount_point = device->device_name;
57 if (!(dp = opendir(mount_point))) {
60 Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n",
61 mount_point, print_name(), be.strerror());
65 len = strlen(mount_point);
67 need_slash = mount_point[len - 1] != '/';
69 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
71 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
73 Dmsg2(129, "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n",
74 mount_point, print_name());
77 if (strcmp(result->d_name, ".") == 0 ||
78 strcmp(result->d_name, "..") == 0) {
82 if (!is_volume_name_legal(result->d_name)) {
85 pm_strcpy(fname, mount_point);
87 pm_strcat(fname, "/");
89 pm_strcat(fname, result->d_name);
90 if (lstat(fname.c_str(), &statp) != 0 ||
91 !S_ISREG(statp.st_mode)) {
92 continue; /* ignore directories & special files */
96 * OK, we got a different volume mounted. First save the
97 * requested Volume info (dcr) structure, then query if
98 * this volume is really OK. If not, put back the desired
99 * volume name, mark it not in changer and continue.
101 memcpy(&dcrVolCatInfo, &dcr->VolCatInfo, sizeof(dcrVolCatInfo));
102 memcpy(&devVolCatInfo, &VolCatInfo, sizeof(devVolCatInfo));
103 /* Check if this is a valid Volume in the pool */
104 bstrncpy(dcr->VolumeName, result->d_name, sizeof(dcr->VolumeName));
105 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
108 /* This was not the volume we expected, but it is OK with
109 * the Director, so use it.
111 memcpy(&VolCatInfo, &dcr->VolCatInfo, sizeof(VolCatInfo));
113 break; /* got a Volume */
119 sm_check(__FILE__, __LINE__, false);
124 * Check if the Volume name has legal characters
125 * If ua is non-NULL send the message
127 static bool is_volume_name_legal(char *name)
131 const char *accept = ":.-_";
133 /* Restrict the characters permitted in the Volume name */
134 for (p=name; *p; p++) {
135 if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
141 if (len >= MAX_NAME_LENGTH) {