2 Bacula® - The Network Backup Solution
4 Copyright (C) 2006-2014 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from many
7 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 Bacula® is a registered trademark of Kern Sibbald.
18 * scan.c scan a directory (on a removable file) for a valid
19 * Volume name. If found, open the file for append.
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;
39 char VolumeName[MAX_NAME_LENGTH];
42 POOL_MEM fname(PM_FNAME);
43 bool need_slash = false;
46 dcrVolCatInfo = dcr->VolCatInfo; /* structure assignment */
47 devVolCatInfo = VolCatInfo; /* structure assignment */
48 bstrncpy(VolumeName, dcr->VolumeName, sizeof(VolumeName));
50 name_max = pathconf(".", _PC_NAME_MAX);
51 if (name_max < 1024) {
55 if (device->mount_point) {
56 mount_point = device->mount_point;
58 mount_point = device->device_name;
61 if (!(dp = opendir(mount_point))) {
64 Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n",
65 mount_point, print_name(), be.bstrerror());
69 len = strlen(mount_point);
71 need_slash = !IsPathSeparator(mount_point[len - 1]);
73 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
75 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
77 Dmsg2(129, "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n",
78 mount_point, print_name());
81 if (strcmp(result->d_name, ".") == 0 ||
82 strcmp(result->d_name, "..") == 0) {
86 if (!is_volume_name_legal(result->d_name)) {
89 pm_strcpy(fname, mount_point);
91 pm_strcat(fname, "/");
93 pm_strcat(fname, result->d_name);
94 if (lstat(fname.c_str(), &statp) != 0 ||
95 !S_ISREG(statp.st_mode)) {
96 continue; /* ignore directories & special files */
100 * OK, we got a different volume mounted. First save the
101 * requested Volume info (dcr) structure, then query if
102 * this volume is really OK. If not, put back the desired
103 * volume name, mark it not in changer and continue.
105 /* Check if this is a valid Volume in the pool */
106 bstrncpy(dcr->VolumeName, result->d_name, sizeof(dcr->VolumeName));
107 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
110 /* This was not the volume we expected, but it is OK with
111 * the Director, so use it.
113 VolCatInfo = dcr->VolCatInfo; /* structure assignment */
115 break; /* got a Volume */
122 /* Restore VolumeName we really wanted */
123 bstrncpy(dcr->VolumeName, VolumeName, sizeof(dcr->VolumeName));
124 dcr->VolCatInfo = dcrVolCatInfo; /* structure assignment */
125 VolCatInfo = devVolCatInfo; /* structure assignment */
132 * Check if the Volume name has legal characters
133 * If ua is non-NULL send the message
135 static bool is_volume_name_legal(char *name)
139 const char *accept = ":.-_";
141 /* Restrict the characters permitted in the Volume name */
142 for (p=name; *p; p++) {
143 if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
149 if (len >= MAX_NAME_LENGTH) {