]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/scan.c
Fix automatic labeling of File volumes, which was broken by new
[bacula/bacula] / bacula / src / stored / scan.c
1 /*
2  *
3  *   scan.c scan a directory (on a removable file) for a valid
4  *      Volume name. If found, open the file for append.
5  *
6  *    Kern Sibbald, MMVI
7  *
8  *   Version $Id$
9  */
10 /*
11    Bacula® - The Network Backup Solution
12
13    Copyright (C) 2006-2006 Free Software Foundation Europe e.V.
14
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.
21
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.
26
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
30    02110-1301, USA.
31
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.
36 */
37
38 #include "bacula.h"
39 #include "stored.h"
40
41 /* Forward referenced functions */
42 static bool is_volume_name_legal(char *name);
43
44
45 bool DEVICE::scan_dir_for_volume(DCR *dcr)
46 {
47    DIR* dp;
48    struct dirent *entry, *result;
49    int name_max;
50    char *mount_point;
51    VOLUME_CAT_INFO dcrVolCatInfo, devVolCatInfo;
52    char VolumeName[MAX_NAME_LENGTH];
53    struct stat statp;
54    bool found = false;
55    POOL_MEM fname(PM_FNAME);
56    bool need_slash = false;
57    int len;
58
59    
60    bstrncpy(VolumeName, dcr->VolumeName, sizeof(VolumeName));
61    name_max = pathconf(".", _PC_NAME_MAX);
62    if (name_max < 1024) {
63       name_max = 1024;
64    }
65
66    if (device->mount_point) {
67       mount_point = device->mount_point;
68    } else {
69       mount_point = device->device_name;
70    }
71       
72    if (!(dp = opendir(mount_point))) {
73       berrno be;
74       dev_errno = errno;
75       Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n", 
76             mount_point, print_name(), be.strerror());
77       goto get_out;
78    }
79    
80    len = strlen(mount_point);
81    if (len > 0) {
82       need_slash = !IsPathSeparator(mount_point[len - 1]);
83    }
84    entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
85    for ( ;; ) {
86       if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
87          dev_errno = EIO;
88          Dmsg2(129, "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n", 
89                mount_point, print_name());
90          break;
91       }
92       if (strcmp(result->d_name, ".") == 0 || 
93           strcmp(result->d_name, "..") == 0) {
94          continue;
95       }
96        
97       if (!is_volume_name_legal(result->d_name)) {
98          continue;
99       }
100       pm_strcpy(fname, mount_point);
101       if (need_slash) {
102          pm_strcat(fname, "/");
103       }
104       pm_strcat(fname, result->d_name);
105       if (lstat(fname.c_str(), &statp) != 0 ||
106           !S_ISREG(statp.st_mode)) {
107          continue;                 /* ignore directories & special files */
108       }
109
110       /*
111        * OK, we got a different volume mounted. First save the
112        *  requested Volume info (dcr) structure, then query if
113        *  this volume is really OK. If not, put back the desired
114        *  volume name, mark it not in changer and continue.
115        */
116       dcrVolCatInfo = dcr->VolCatInfo;     /* structure assignment */
117       devVolCatInfo = VolCatInfo;          /* structure assignment */
118       bstrncpy(VolumeName, dcr->VolumeName, sizeof(VolumeName));
119       /* Check if this is a valid Volume in the pool */
120       bstrncpy(dcr->VolumeName, result->d_name, sizeof(dcr->VolumeName));
121       if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
122          continue;
123       }
124       /* This was not the volume we expected, but it is OK with
125        * the Director, so use it.
126        */
127       VolCatInfo = dcr->VolCatInfo;       /* structure assignment */
128       found = true;
129       break;                /* got a Volume */
130    }
131    free(entry);
132    closedir(dp);
133    
134 get_out:
135    if (!found) {
136       /* Restore VolumeName we really wanted */
137       bstrncpy(dcr->VolumeName, VolumeName, sizeof(dcr->VolumeName));
138    }
139    sm_check(__FILE__, __LINE__, false);
140    return found;
141 }
142
143 /*
144  * Check if the Volume name has legal characters
145  * If ua is non-NULL send the message
146  */
147 static bool is_volume_name_legal(char *name)
148 {
149    int len;
150    const char *p;
151    const char *accept = ":.-_";
152
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))) {
156          continue;
157       }
158       return false;
159    }
160    len = strlen(name);
161    if (len >= MAX_NAME_LENGTH) {
162       return false;
163    }
164    if (len == 0) {
165       return false;
166    }
167    return true;
168 }