3 * scan.c scan a directory (on a removable file) for a valid
4 * Volume name. If found, open the file for append.
11 Bacula® - The Network Backup Solution
13 Copyright (C) 2006-2006 Free Software Foundation Europe e.V.
15 The main author of Bacula is Kern Sibbald, with contributions from
16 many others, a complete list can be found in the file AUTHORS.
17 This program is Free Software; you can redistribute it and/or
18 modify it under the terms of version two of the GNU General Public
19 License as published by the Free Software Foundation plus additions
20 that are listed in the file LICENSE.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 Bacula® is a registered trademark of John Walker.
33 The licensor of Bacula is the Free Software Foundation Europe
34 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
35 Switzerland, email:ftf@fsfeurope.org.
41 /* Forward referenced functions */
42 static bool is_volume_name_legal(char *name);
45 bool DEVICE::scan_dir_for_volume(DCR *dcr)
48 struct dirent *entry, *result;
51 VOLUME_CAT_INFO dcrVolCatInfo, devVolCatInfo;
54 POOL_MEM fname(PM_FNAME);
55 bool need_slash = false;
59 name_max = pathconf(".", _PC_NAME_MAX);
60 if (name_max < 1024) {
64 if (device->mount_point) {
65 mount_point = device->mount_point;
67 mount_point = device->device_name;
70 if (!(dp = opendir(mount_point))) {
73 Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n",
74 mount_point, print_name(), be.strerror());
78 len = strlen(mount_point);
80 need_slash = !IsPathSeparator(mount_point[len - 1]);
82 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
84 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
86 Dmsg2(129, "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n",
87 mount_point, print_name());
90 if (strcmp(result->d_name, ".") == 0 ||
91 strcmp(result->d_name, "..") == 0) {
95 if (!is_volume_name_legal(result->d_name)) {
98 pm_strcpy(fname, mount_point);
100 pm_strcat(fname, "/");
102 pm_strcat(fname, result->d_name);
103 if (lstat(fname.c_str(), &statp) != 0 ||
104 !S_ISREG(statp.st_mode)) {
105 continue; /* ignore directories & special files */
109 * OK, we got a different volume mounted. First save the
110 * requested Volume info (dcr) structure, then query if
111 * this volume is really OK. If not, put back the desired
112 * volume name, mark it not in changer and continue.
114 dcrVolCatInfo = dcr->VolCatInfo; /* structure assignment */
115 devVolCatInfo = VolCatInfo; /* structure assignment */
116 /* Check if this is a valid Volume in the pool */
117 bstrncpy(dcr->VolumeName, result->d_name, sizeof(dcr->VolumeName));
118 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
121 /* This was not the volume we expected, but it is OK with
122 * the Director, so use it.
124 VolCatInfo = dcr->VolCatInfo; /* structure assignment */
126 break; /* got a Volume */
132 sm_check(__FILE__, __LINE__, false);
137 * Check if the Volume name has legal characters
138 * If ua is non-NULL send the message
140 static bool is_volume_name_legal(char *name)
144 const char *accept = ":.-_";
146 /* Restrict the characters permitted in the Volume name */
147 for (p=name; *p; p++) {
148 if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
154 if (len >= MAX_NAME_LENGTH) {