]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/scan.c
e4290b2722fb8bab5a1ea81b1bf4257bfd9dd78c
[bacula/bacula] / bacula / src / stored / scan.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2006-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21  *
22  *   scan.c scan a directory (on a removable file) for a valid
23  *      Volume name. If found, open the file for append.
24  *
25  *    Kern Sibbald, MMVI
26  *
27  */
28
29 #include "bacula.h"
30 #include "stored.h"
31
32 /* Forward referenced functions */
33 static bool is_volume_name_legal(char *name);
34
35
36 bool DEVICE::scan_dir_for_volume(DCR *dcr)
37 {
38    DIR* dp;
39    struct dirent *entry, *result;
40    int name_max;
41    char *mount_point;
42    VOLUME_CAT_INFO dcrVolCatInfo, devVolCatInfo;
43    char VolumeName[MAX_NAME_LENGTH];
44    struct stat statp;
45    bool found = false;
46    POOL_MEM fname(PM_FNAME);
47    bool need_slash = false;
48    int len;
49
50    dcrVolCatInfo = dcr->VolCatInfo;     /* structure assignment */
51    devVolCatInfo = VolCatInfo;          /* structure assignment */
52    bstrncpy(VolumeName, dcr->VolumeName, sizeof(VolumeName));
53
54    name_max = pathconf(".", _PC_NAME_MAX);
55    if (name_max < 1024) {
56       name_max = 1024;
57    }
58
59    if (device->mount_point) {
60       mount_point = device->mount_point;
61    } else {
62       mount_point = device->device_name;
63    }
64
65    if (!(dp = opendir(mount_point))) {
66       berrno be;
67       dev_errno = errno;
68       Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n",
69             mount_point, print_name(), be.bstrerror());
70       goto get_out;
71    }
72
73    len = strlen(mount_point);
74    if (len > 0) {
75       need_slash = !IsPathSeparator(mount_point[len - 1]);
76    }
77    entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
78    for ( ;; ) {
79       if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
80          dev_errno = EIO;
81          Dmsg2(129, "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n",
82                mount_point, print_name());
83          break;
84       }
85       if (strcmp(result->d_name, ".") == 0 ||
86           strcmp(result->d_name, "..") == 0) {
87          continue;
88       }
89
90       if (!is_volume_name_legal(result->d_name)) {
91          continue;
92       }
93       pm_strcpy(fname, mount_point);
94       if (need_slash) {
95          pm_strcat(fname, "/");
96       }
97       pm_strcat(fname, result->d_name);
98       if (lstat(fname.c_str(), &statp) != 0 ||
99           !S_ISREG(statp.st_mode)) {
100          continue;                 /* ignore directories & special files */
101       }
102
103       /*
104        * OK, we got a different volume mounted. First save the
105        *  requested Volume info (dcr) structure, then query if
106        *  this volume is really OK. If not, put back the desired
107        *  volume name, mark it not in changer and continue.
108        */
109       /* Check if this is a valid Volume in the pool */
110       bstrncpy(dcr->VolumeName, result->d_name, sizeof(dcr->VolumeName));
111       if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
112          continue;
113       }
114       /* This was not the volume we expected, but it is OK with
115        * the Director, so use it.
116        */
117       VolCatInfo = dcr->VolCatInfo;       /* structure assignment */
118       found = true;
119       break;                /* got a Volume */
120    }
121    free(entry);
122    closedir(dp);
123
124 get_out:
125    if (!found) {
126       /* Restore VolumeName we really wanted */
127       bstrncpy(dcr->VolumeName, VolumeName, sizeof(dcr->VolumeName));
128       dcr->VolCatInfo = dcrVolCatInfo;     /* structure assignment */
129       VolCatInfo = devVolCatInfo;          /* structure assignment */
130    }
131    Dsm_check(100);
132    return found;
133 }
134
135 /*
136  * Check if the Volume name has legal characters
137  * If ua is non-NULL send the message
138  */
139 static bool is_volume_name_legal(char *name)
140 {
141    int len;
142    const char *p;
143    const char *accept = ":.-_";
144
145    /* Restrict the characters permitted in the Volume name */
146    for (p=name; *p; p++) {
147       if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
148          continue;
149       }
150       return false;
151    }
152    len = strlen(name);
153    if (len >= MAX_NAME_LENGTH) {
154       return false;
155    }
156    if (len == 0) {
157       return false;
158    }
159    return true;
160 }