2 Bacula® - The Network Backup Solution
4 Copyright (C) 2006-2011 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * scan.c scan a directory (on a removable file) for a valid
31 * Volume name. If found, open the file for append.
40 /* Forward referenced functions */
41 static bool is_volume_name_legal(char *name);
44 bool DEVICE::scan_dir_for_volume(DCR *dcr)
47 struct dirent *entry, *result;
50 VOLUME_CAT_INFO dcrVolCatInfo, devVolCatInfo;
51 char VolumeName[MAX_NAME_LENGTH];
54 POOL_MEM fname(PM_FNAME);
55 bool need_slash = false;
58 dcrVolCatInfo = dcr->VolCatInfo; /* structure assignment */
59 devVolCatInfo = VolCatInfo; /* structure assignment */
60 bstrncpy(VolumeName, dcr->VolumeName, sizeof(VolumeName));
62 name_max = pathconf(".", _PC_NAME_MAX);
63 if (name_max < 1024) {
67 if (device->mount_point) {
68 mount_point = device->mount_point;
70 mount_point = device->device_name;
73 if (!(dp = opendir(mount_point))) {
76 Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n",
77 mount_point, print_name(), be.bstrerror());
81 len = strlen(mount_point);
83 need_slash = !IsPathSeparator(mount_point[len - 1]);
85 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
87 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
89 Dmsg2(129, "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n",
90 mount_point, print_name());
93 if (strcmp(result->d_name, ".") == 0 ||
94 strcmp(result->d_name, "..") == 0) {
98 if (!is_volume_name_legal(result->d_name)) {
101 pm_strcpy(fname, mount_point);
103 pm_strcat(fname, "/");
105 pm_strcat(fname, result->d_name);
106 if (lstat(fname.c_str(), &statp) != 0 ||
107 !S_ISREG(statp.st_mode)) {
108 continue; /* ignore directories & special files */
112 * OK, we got a different volume mounted. First save the
113 * requested Volume info (dcr) structure, then query if
114 * this volume is really OK. If not, put back the desired
115 * volume name, mark it not in changer and continue.
117 /* Check if this is a valid Volume in the pool */
118 bstrncpy(dcr->VolumeName, result->d_name, sizeof(dcr->VolumeName));
119 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
122 /* This was not the volume we expected, but it is OK with
123 * the Director, so use it.
125 VolCatInfo = dcr->VolCatInfo; /* structure assignment */
127 break; /* got a Volume */
134 /* Restore VolumeName we really wanted */
135 bstrncpy(dcr->VolumeName, VolumeName, sizeof(dcr->VolumeName));
136 dcr->VolCatInfo = dcrVolCatInfo; /* structure assignment */
137 VolCatInfo = devVolCatInfo; /* structure assignment */
144 * Check if the Volume name has legal characters
145 * If ua is non-NULL send the message
147 static bool is_volume_name_legal(char *name)
151 const char *accept = ":.-_";
153 /* Restrict the characters permitted in the Volume name */
154 for (p=name; *p; p++) {
155 if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
161 if (len >= MAX_NAME_LENGTH) {