/
scan.c
162 lines (144 loc) · 4.67 KB
/
scan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2006-2011 Free Software Foundation Europe e.V.
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* scan.c scan a directory (on a removable file) for a valid
* Volume name. If found, open the file for append.
*
* Kern Sibbald, MMVI
*/
#include "bareos.h"
#include "stored.h"
/* Forward referenced functions */
static bool is_volume_name_legal(char *name);
bool DEVICE::scan_dir_for_volume(DCR *dcr)
{
DIR* dp;
struct dirent *entry, *result;
int name_max;
char *mount_point;
VOLUME_CAT_INFO dcrVolCatInfo, devVolCatInfo;
char VolumeName[MAX_NAME_LENGTH];
struct stat statp;
bool found = false;
POOL_MEM fname(PM_FNAME);
bool need_slash = false;
int len;
dcrVolCatInfo = dcr->VolCatInfo; /* structure assignment */
devVolCatInfo = VolCatInfo; /* structure assignment */
bstrncpy(VolumeName, dcr->VolumeName, sizeof(VolumeName));
name_max = pathconf(".", _PC_NAME_MAX);
if (name_max < 1024) {
name_max = 1024;
}
if (device->mount_point) {
mount_point = device->mount_point;
} else {
mount_point = device->device_name;
}
if (!(dp = opendir(mount_point))) {
berrno be;
dev_errno = errno;
Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n",
mount_point, print_name(), be.bstrerror());
goto get_out;
}
len = strlen(mount_point);
if (len > 0) {
need_slash = !IsPathSeparator(mount_point[len - 1]);
}
entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
for ( ;; ) {
if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
dev_errno = EIO;
Dmsg2(129, "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n",
mount_point, print_name());
break;
}
if (bstrcmp(result->d_name, ".") ||
bstrcmp(result->d_name, "..")) {
continue;
}
if (!is_volume_name_legal(result->d_name)) {
continue;
}
pm_strcpy(fname, mount_point);
if (need_slash) {
pm_strcat(fname, "/");
}
pm_strcat(fname, result->d_name);
if (lstat(fname.c_str(), &statp) != 0 ||
!S_ISREG(statp.st_mode)) {
continue; /* ignore directories & special files */
}
/*
* OK, we got a different volume mounted. First save the
* requested Volume info (dcr) structure, then query if
* this volume is really OK. If not, put back the desired
* volume name, mark it not in changer and continue.
*/
/* Check if this is a valid Volume in the pool */
bstrncpy(dcr->VolumeName, result->d_name, sizeof(dcr->VolumeName));
if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
continue;
}
/* This was not the volume we expected, but it is OK with
* the Director, so use it.
*/
VolCatInfo = dcr->VolCatInfo; /* structure assignment */
found = true;
break; /* got a Volume */
}
free(entry);
closedir(dp);
get_out:
if (!found) {
/* Restore VolumeName we really wanted */
bstrncpy(dcr->VolumeName, VolumeName, sizeof(dcr->VolumeName));
dcr->VolCatInfo = dcrVolCatInfo; /* structure assignment */
VolCatInfo = devVolCatInfo; /* structure assignment */
}
Dsm_check(100);
return found;
}
/*
* Check if the Volume name has legal characters
* If ua is non-NULL send the message
*/
static bool is_volume_name_legal(char *name)
{
int len;
const char *p;
const char *accept = ":.-_/";
if (name[0] == '/') {
return false;
}
/* Restrict the characters permitted in the Volume name */
for (p=name; *p; p++) {
if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
continue;
}
return false;
}
len = strlen(name);
if (len >= MAX_NAME_LENGTH) {
return false;
}
if (len == 0) {
return false;
}
return true;
}