2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
21 * scan.c scan a directory (on a removable file) for a valid
22 * Volume name. If found, open the file for append.
31 /* Forward referenced functions */
32 static bool is_volume_name_legal(char *name);
35 bool DEVICE::scan_dir_for_volume(DCR *dcr)
38 struct dirent *entry, *result;
41 VOLUME_CAT_INFO dcrVolCatInfo, devVolCatInfo;
42 char VolumeName[MAX_NAME_LENGTH];
45 POOL_MEM fname(PM_FNAME);
46 bool need_slash = false;
49 dcrVolCatInfo = dcr->VolCatInfo; /* structure assignment */
50 devVolCatInfo = VolCatInfo; /* structure assignment */
51 bstrncpy(VolumeName, dcr->VolumeName, sizeof(VolumeName));
53 name_max = pathconf(".", _PC_NAME_MAX);
54 if (name_max < 1024) {
58 if (device->mount_point) {
59 mount_point = device->mount_point;
61 mount_point = device->device_name;
64 if (!(dp = opendir(mount_point))) {
67 Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n",
68 mount_point, print_name(), be.bstrerror());
72 len = strlen(mount_point);
74 need_slash = !IsPathSeparator(mount_point[len - 1]);
76 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
78 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
80 Dmsg2(129, "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n",
81 mount_point, print_name());
84 if (strcmp(result->d_name, ".") == 0 ||
85 strcmp(result->d_name, "..") == 0) {
89 if (!is_volume_name_legal(result->d_name)) {
92 pm_strcpy(fname, mount_point);
94 pm_strcat(fname, "/");
96 pm_strcat(fname, result->d_name);
97 if (lstat(fname.c_str(), &statp) != 0 ||
98 !S_ISREG(statp.st_mode)) {
99 continue; /* ignore directories & special files */
103 * OK, we got a different volume mounted. First save the
104 * requested Volume info (dcr) structure, then query if
105 * this volume is really OK. If not, put back the desired
106 * volume name, mark it not in changer and continue.
108 bstrncpy(dcr->VolumeName, result->d_name, sizeof(dcr->VolumeName));
109 /* Check if this is a valid Volume in the pool */
110 if (!dir_get_volume_info(dcr, dcr->VolumeName, GET_VOL_INFO_FOR_WRITE)) {
113 /* This was not the volume we expected, but it is OK with
114 * the Director, so use it.
116 VolCatInfo = dcr->VolCatInfo; /* structure assignment */
118 break; /* got a Volume */
125 /* Restore VolumeName we really wanted */
126 bstrncpy(dcr->VolumeName, VolumeName, sizeof(dcr->VolumeName));
127 dcr->VolCatInfo = dcrVolCatInfo; /* structure assignment */
128 VolCatInfo = devVolCatInfo; /* structure assignment */
135 * Check if the Volume name has legal characters
136 * If ua is non-NULL send the message
138 static bool is_volume_name_legal(char *name)
142 const char *accept = ":.-_";
144 /* Restrict the characters permitted in the Volume name */
145 for (p=name; *p; p++) {
146 if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
152 if (len >= MAX_NAME_LENGTH) {