2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
5 Copyright (C) 2006-2014 Free Software Foundation Europe e.V.
7 The original author of Bacula is Kern Sibbald, with contributions
8 from many others, a complete list can be found in the file AUTHORS.
10 You may use this file and others of this release according to the
11 license defined in the LICENSE file, which includes the Affero General
12 Public License, v3.0 ("AGPLv3") and some additional permissions and
13 terms pursuant to its AGPLv3 Section 7.
15 This notice must be preserved when any source code is
16 conveyed and/or propagated.
18 Bacula(R) is a registered trademark of Kern Sibbald.
22 * scan.c scan a directory (on a removable file) for a valid
23 * Volume name. If found, open the file for append.
32 /* Forward referenced functions */
33 static bool is_volume_name_legal(char *name);
36 bool DEVICE::scan_dir_for_volume(DCR *dcr)
39 struct dirent *entry, *result;
42 VOLUME_CAT_INFO dcrVolCatInfo, devVolCatInfo;
43 char VolumeName[MAX_NAME_LENGTH];
46 POOL_MEM fname(PM_FNAME);
47 bool need_slash = false;
50 dcrVolCatInfo = dcr->VolCatInfo; /* structure assignment */
51 devVolCatInfo = VolCatInfo; /* structure assignment */
52 bstrncpy(VolumeName, dcr->VolumeName, sizeof(VolumeName));
54 name_max = pathconf(".", _PC_NAME_MAX);
55 if (name_max < 1024) {
59 if (device->mount_point) {
60 mount_point = device->mount_point;
62 mount_point = device->device_name;
65 if (!(dp = opendir(mount_point))) {
68 Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n",
69 mount_point, print_name(), be.bstrerror());
73 len = strlen(mount_point);
75 need_slash = !IsPathSeparator(mount_point[len - 1]);
77 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
79 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
81 Dmsg2(129, "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n",
82 mount_point, print_name());
85 if (strcmp(result->d_name, ".") == 0 ||
86 strcmp(result->d_name, "..") == 0) {
90 if (!is_volume_name_legal(result->d_name)) {
93 pm_strcpy(fname, mount_point);
95 pm_strcat(fname, "/");
97 pm_strcat(fname, result->d_name);
98 if (lstat(fname.c_str(), &statp) != 0 ||
99 !S_ISREG(statp.st_mode)) {
100 continue; /* ignore directories & special files */
104 * OK, we got a different volume mounted. First save the
105 * requested Volume info (dcr) structure, then query if
106 * this volume is really OK. If not, put back the desired
107 * volume name, mark it not in changer and continue.
109 /* Check if this is a valid Volume in the pool */
110 bstrncpy(dcr->VolumeName, result->d_name, sizeof(dcr->VolumeName));
111 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
114 /* This was not the volume we expected, but it is OK with
115 * the Director, so use it.
117 VolCatInfo = dcr->VolCatInfo; /* structure assignment */
119 break; /* got a Volume */
126 /* Restore VolumeName we really wanted */
127 bstrncpy(dcr->VolumeName, VolumeName, sizeof(dcr->VolumeName));
128 dcr->VolCatInfo = dcrVolCatInfo; /* structure assignment */
129 VolCatInfo = devVolCatInfo; /* structure assignment */
136 * Check if the Volume name has legal characters
137 * If ua is non-NULL send the message
139 static bool is_volume_name_legal(char *name)
143 const char *accept = ":.-_";
145 /* Restrict the characters permitted in the Volume name */
146 for (p=name; *p; p++) {
147 if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
153 if (len >= MAX_NAME_LENGTH) {