]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/scan.c
commit changes
[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    Copyright (C) 2006 Kern Sibbald
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License
15    version 2 as amended with additional clauses defined in the
16    file LICENSE in the main source directory.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
21    the file LICENSE for additional details.
22
23  */
24
25 #include "bacula.h"
26 #include "stored.h"
27
28 /* Forward referenced functions */
29 static bool is_volume_name_legal(char *name);
30
31
32 bool DEVICE::scan_dir_for_volume(DCR *dcr)
33 {
34    DIR* dp;
35    struct dirent *entry, *result;
36    int name_max;
37    char *mount_point;
38    VOLUME_CAT_INFO dcrVolCatInfo, devVolCatInfo;
39    struct stat statp;
40    bool found = false;
41    POOL_MEM fname(PM_FNAME);
42    bool need_slash = false;
43    int len;
44
45    
46    name_max = pathconf(".", _PC_NAME_MAX);
47    if (name_max < 1024) {
48       name_max = 1024;
49    }
50
51    if (device->mount_point) {
52       mount_point = device->mount_point;
53    } else {
54       mount_point = device->device_name;
55    }
56       
57    if (!(dp = opendir(mount_point))) {
58       berrno be;
59       dev_errno = errno;
60       Dmsg3(29, "scan_dir_for_vol: failed to open dir %s (dev=%s), ERR=%s\n", 
61             mount_point, print_name(), be.strerror());
62       goto get_out;
63    }
64    
65    len = strlen(mount_point);
66    if (len > 0) {
67       need_slash = mount_point[len - 1] != '/';
68    }
69    entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
70    for ( ;; ) {
71       if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
72          dev_errno = EIO;
73          Dmsg2(129, "scan_dir_for_vol: failed to find suitable file in dir %s (dev=%s)\n", 
74                mount_point, print_name());
75          break;
76       }
77       if (strcmp(result->d_name, ".") == 0 || 
78           strcmp(result->d_name, "..") == 0) {
79          continue;
80       }
81        
82       if (!is_volume_name_legal(result->d_name)) {
83          continue;
84       }
85       pm_strcpy(fname, mount_point);
86       if (need_slash) {
87          pm_strcat(fname, "/");
88       }
89       pm_strcat(fname, result->d_name);
90       if (lstat(fname.c_str(), &statp) != 0 ||
91           !S_ISREG(statp.st_mode)) {
92          continue;                 /* ignore directories & special files */
93       }
94
95       /*
96        * OK, we got a different volume mounted. First save the
97        *  requested Volume info (dcr) structure, then query if
98        *  this volume is really OK. If not, put back the desired
99        *  volume name, mark it not in changer and continue.
100        */
101       memcpy(&dcrVolCatInfo, &dcr->VolCatInfo, sizeof(dcrVolCatInfo));
102       memcpy(&devVolCatInfo, &VolCatInfo, sizeof(devVolCatInfo));
103       /* Check if this is a valid Volume in the pool */
104       bstrncpy(dcr->VolumeName, result->d_name, sizeof(dcr->VolumeName));
105       if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
106          continue;
107       }
108       /* This was not the volume we expected, but it is OK with
109        * the Director, so use it.
110        */
111       memcpy(&VolCatInfo, &dcr->VolCatInfo, sizeof(VolCatInfo));
112       found = true;
113       break;                /* got a Volume */
114    }
115    free(entry);
116    closedir(dp);
117    
118 get_out:
119    sm_check(__FILE__, __LINE__, false);
120    return found;
121 }
122
123 /*
124  * Check if the Volume name has legal characters
125  * If ua is non-NULL send the message
126  */
127 static bool is_volume_name_legal(char *name)
128 {
129    int len;
130    const char *p;
131    const char *accept = ":.-_";
132
133    /* Restrict the characters permitted in the Volume name */
134    for (p=name; *p; p++) {
135       if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
136          continue;
137       }
138       return false;
139    }
140    len = strlen(name);
141    if (len >= MAX_NAME_LENGTH) {
142       return false;
143    }
144    if (len == 0) {
145       return false;
146    }
147    return true;
148 }