]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/scan.c
kes Change Bacula trademark owner from John Walker to Kern Sibbald
[bacula/bacula] / bacula / src / stored / scan.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2006-2007 Free Software Foundation Europe e.V.
5
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 two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
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.
17
18    You should have received a copy of the GNU 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
21    02110-1301, USA.
22
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.
27 */
28 /*
29  *
30  *   scan.c scan a directory (on a removable file) for a valid
31  *      Volume name. If found, open the file for append.
32  *
33  *    Kern Sibbald, MMVI
34  *
35  *   Version $Id$
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    dcrVolCatInfo = dcr->VolCatInfo;     /* structure assignment */
60    devVolCatInfo = VolCatInfo;          /* structure assignment */
61    bstrncpy(VolumeName, dcr->VolumeName, sizeof(VolumeName));
62
63    name_max = pathconf(".", _PC_NAME_MAX);
64    if (name_max < 1024) {
65       name_max = 1024;
66    }
67
68    if (device->mount_point) {
69       mount_point = device->mount_point;
70    } else {
71       mount_point = device->device_name;
72    }
73       
74    if (!(dp = opendir(mount_point))) {
75       berrno be;
76       dev_errno = errno;
77       Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n", 
78             mount_point, print_name(), be.bstrerror());
79       goto get_out;
80    }
81    
82    len = strlen(mount_point);
83    if (len > 0) {
84       need_slash = !IsPathSeparator(mount_point[len - 1]);
85    }
86    entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
87    for ( ;; ) {
88       if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
89          dev_errno = EIO;
90          Dmsg2(129, "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n", 
91                mount_point, print_name());
92          break;
93       }
94       if (strcmp(result->d_name, ".") == 0 || 
95           strcmp(result->d_name, "..") == 0) {
96          continue;
97       }
98        
99       if (!is_volume_name_legal(result->d_name)) {
100          continue;
101       }
102       pm_strcpy(fname, mount_point);
103       if (need_slash) {
104          pm_strcat(fname, "/");
105       }
106       pm_strcat(fname, result->d_name);
107       if (lstat(fname.c_str(), &statp) != 0 ||
108           !S_ISREG(statp.st_mode)) {
109          continue;                 /* ignore directories & special files */
110       }
111
112       /*
113        * OK, we got a different volume mounted. First save the
114        *  requested Volume info (dcr) structure, then query if
115        *  this volume is really OK. If not, put back the desired
116        *  volume name, mark it not in changer and continue.
117        */
118       /* Check if this is a valid Volume in the pool */
119       bstrncpy(dcr->VolumeName, result->d_name, sizeof(dcr->VolumeName));
120       if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
121          continue;
122       }
123       /* This was not the volume we expected, but it is OK with
124        * the Director, so use it.
125        */
126       VolCatInfo = dcr->VolCatInfo;       /* structure assignment */
127       found = true;
128       break;                /* got a Volume */
129    }
130    free(entry);
131    closedir(dp);
132    
133 get_out:
134    if (!found) {
135       /* Restore VolumeName we really wanted */
136       bstrncpy(dcr->VolumeName, VolumeName, sizeof(dcr->VolumeName));
137       dcr->VolCatInfo = dcrVolCatInfo;     /* structure assignment */
138       VolCatInfo = devVolCatInfo;          /* structure assignment */
139    }
140    sm_check(__FILE__, __LINE__, false);
141    return found;
142 }
143
144 /*
145  * Check if the Volume name has legal characters
146  * If ua is non-NULL send the message
147  */
148 static bool is_volume_name_legal(char *name)
149 {
150    int len;
151    const char *p;
152    const char *accept = ":.-_";
153
154    /* Restrict the characters permitted in the Volume name */
155    for (p=name; *p; p++) {
156       if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
157          continue;
158       }
159       return false;
160    }
161    len = strlen(name);
162    if (len >= MAX_NAME_LENGTH) {
163       return false;
164    }
165    if (len == 0) {
166       return false;
167    }
168    return true;
169 }