s3: Filenames: Add uint32_t flags parameter to synthetic_smb_fname().
[kai/samba-autobuild/.git] / source3 / modules / vfs_shadow_copy.c
1 /* 
2  * implementation of an Shadow Copy module
3  *
4  * Copyright (C) Stefan Metzmacher      2003-2004
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *  
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *  
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "ntioctl.h"
23
24 /*
25     Please read the VFS module Samba-HowTo-Collection.
26     there's a chapter about this module
27
28     For this share
29     Z:\
30
31     the ShadowCopies are in this directories
32
33     Z:\@GMT-2003.08.05-12.00.00\
34     Z:\@GMT-2003.08.05-12.01.00\
35     Z:\@GMT-2003.08.05-12.02.00\
36
37     e.g.
38     
39     Z:\testfile.txt
40     Z:\@GMT-2003.08.05-12.02.00\testfile.txt
41
42     or:
43
44     Z:\testdir\testfile.txt
45     Z:\@GMT-2003.08.05-12.02.00\testdir\testfile.txt
46
47
48     Note: Files must differ to be displayed via Windows Explorer!
49           Directories are always displayed...    
50 */
51
52 static int vfs_shadow_copy_debug_level = DBGC_VFS;
53
54 #undef DBGC_CLASS
55 #define DBGC_CLASS vfs_shadow_copy_debug_level
56
57 #define SHADOW_COPY_PREFIX "@GMT-"
58 #define SHADOW_COPY_SAMPLE "@GMT-2004.02.18-15.44.00"
59
60 typedef struct {
61         int pos;
62         int num;
63         struct dirent *dirs;
64 } shadow_copy_Dir;
65
66 static bool shadow_copy_match_name(const char *name)
67 {
68         if (strncmp(SHADOW_COPY_PREFIX,name, sizeof(SHADOW_COPY_PREFIX)-1)==0 &&
69                 (strlen(SHADOW_COPY_SAMPLE) == strlen(name))) {
70                 return True;
71         }
72
73         return False;
74 }
75
76 static DIR *shadow_copy_opendir(vfs_handle_struct *handle,
77                         const struct smb_filename *smb_fname,
78                         const char *mask,
79                         uint32_t attr)
80 {
81         shadow_copy_Dir *dirp;
82         DIR *p = SMB_VFS_NEXT_OPENDIR(handle,smb_fname,mask,attr);
83
84         if (!p) {
85                 DEBUG(0,("shadow_copy_opendir: SMB_VFS_NEXT_OPENDIR() "
86                         "failed for [%s]\n",
87                         smb_fname->base_name));
88                 return NULL;
89         }
90
91         dirp = SMB_MALLOC_P(shadow_copy_Dir);
92         if (!dirp) {
93                 DEBUG(0,("shadow_copy_opendir: Out of memory\n"));
94                 SMB_VFS_NEXT_CLOSEDIR(handle,p);
95                 return NULL;
96         }
97
98         ZERO_STRUCTP(dirp);
99
100         while (True) {
101                 struct dirent *d;
102
103                 d = SMB_VFS_NEXT_READDIR(handle, p, NULL);
104                 if (d == NULL) {
105                         break;
106                 }
107
108                 if (shadow_copy_match_name(d->d_name)) {
109                         DEBUG(8,("shadow_copy_opendir: hide [%s]\n",d->d_name));
110                         continue;
111                 }
112
113                 DEBUG(10,("shadow_copy_opendir: not hide [%s]\n",d->d_name));
114
115                 dirp->dirs = SMB_REALLOC_ARRAY(dirp->dirs,struct dirent, dirp->num+1);
116                 if (!dirp->dirs) {
117                         DEBUG(0,("shadow_copy_opendir: Out of memory\n"));
118                         break;
119                 }
120
121                 dirp->dirs[dirp->num++] = *d;
122         }
123
124         SMB_VFS_NEXT_CLOSEDIR(handle,p);
125         return((DIR *)dirp);
126 }
127
128 static DIR *shadow_copy_fdopendir(vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32_t attr)
129 {
130         shadow_copy_Dir *dirp;
131         DIR *p = SMB_VFS_NEXT_FDOPENDIR(handle,fsp,mask,attr);
132
133         if (!p) {
134                 DEBUG(10,("shadow_copy_opendir: SMB_VFS_NEXT_FDOPENDIR() failed for [%s]\n",
135                         smb_fname_str_dbg(fsp->fsp_name)));
136                 return NULL;
137         }
138
139         dirp = SMB_MALLOC_P(shadow_copy_Dir);
140         if (!dirp) {
141                 DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
142                 SMB_VFS_NEXT_CLOSEDIR(handle,p);
143                 /* We have now closed the fd in fsp. */
144                 fsp->fh->fd = -1;
145                 return NULL;
146         }
147
148         ZERO_STRUCTP(dirp);
149
150         while (True) {
151                 struct dirent *d;
152
153                 d = SMB_VFS_NEXT_READDIR(handle, p, NULL);
154                 if (d == NULL) {
155                         break;
156                 }
157
158                 if (shadow_copy_match_name(d->d_name)) {
159                         DEBUG(8,("shadow_copy_fdopendir: hide [%s]\n",d->d_name));
160                         continue;
161                 }
162
163                 DEBUG(10,("shadow_copy_fdopendir: not hide [%s]\n",d->d_name));
164
165                 dirp->dirs = SMB_REALLOC_ARRAY(dirp->dirs,struct dirent, dirp->num+1);
166                 if (!dirp->dirs) {
167                         DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
168                         break;
169                 }
170
171                 dirp->dirs[dirp->num++] = *d;
172         }
173
174         SMB_VFS_NEXT_CLOSEDIR(handle,p);
175         /* We have now closed the fd in fsp. */
176         fsp->fh->fd = -1;
177         return((DIR *)dirp);
178 }
179
180 static struct dirent *shadow_copy_readdir(vfs_handle_struct *handle,
181                                               DIR *_dirp,
182                                               SMB_STRUCT_STAT *sbuf)
183 {
184         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
185
186         if (dirp->pos < dirp->num) {
187                 return &(dirp->dirs[dirp->pos++]);
188         }
189
190         return NULL;
191 }
192
193 static void shadow_copy_seekdir(struct vfs_handle_struct *handle, DIR *_dirp, long offset)
194 {
195         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
196
197         if (offset < dirp->num) {
198                 dirp->pos = offset ;
199         }
200 }
201
202 static long shadow_copy_telldir(struct vfs_handle_struct *handle, DIR *_dirp)
203 {
204         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
205         return( dirp->pos ) ;
206 }
207
208 static void shadow_copy_rewinddir(struct vfs_handle_struct *handle, DIR *_dirp)
209 {
210         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
211         dirp->pos = 0 ;
212 }
213
214 static int shadow_copy_closedir(vfs_handle_struct *handle, DIR *_dirp)
215 {
216         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
217
218         SAFE_FREE(dirp->dirs);
219         SAFE_FREE(dirp);
220  
221         return 0;       
222 }
223
224 static int shadow_copy_get_shadow_copy_data(vfs_handle_struct *handle,
225                                             files_struct *fsp,
226                                             struct shadow_copy_data *shadow_copy_data,
227                                             bool labels)
228 {
229         DIR *p = NULL;
230         struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
231                                                 fsp->conn->connectpath,
232                                                 NULL,
233                                                 NULL,
234                                                 0);
235         if (smb_fname == NULL) {
236                 errno = ENOMEM;
237                 return -1;
238         }
239
240         p = SMB_VFS_NEXT_OPENDIR(handle,smb_fname,NULL,0);
241
242         TALLOC_FREE(smb_fname);
243
244         shadow_copy_data->num_volumes = 0;
245         shadow_copy_data->labels = NULL;
246
247         if (!p) {
248                 DEBUG(0,("shadow_copy_get_shadow_copy_data: SMB_VFS_NEXT_OPENDIR() failed for [%s]\n",fsp->conn->connectpath));
249                 return -1;
250         }
251
252         while (True) {
253                 SHADOW_COPY_LABEL *tlabels;
254                 struct dirent *d;
255
256                 d = SMB_VFS_NEXT_READDIR(handle, p, NULL);
257                 if (d == NULL) {
258                         break;
259                 }
260
261                 /* */
262                 if (!shadow_copy_match_name(d->d_name)) {
263                         DEBUG(10,("shadow_copy_get_shadow_copy_data: ignore [%s]\n",d->d_name));
264                         continue;
265                 }
266
267                 DEBUG(7,("shadow_copy_get_shadow_copy_data: not ignore [%s]\n",d->d_name));
268
269                 if (!labels) {
270                         shadow_copy_data->num_volumes++;
271                         continue;
272                 }
273
274                 tlabels = (SHADOW_COPY_LABEL *)TALLOC_REALLOC(shadow_copy_data,
275                                                                         shadow_copy_data->labels,
276                                                                         (shadow_copy_data->num_volumes+1)*sizeof(SHADOW_COPY_LABEL));
277                 if (tlabels == NULL) {
278                         DEBUG(0,("shadow_copy_get_shadow_copy_data: Out of memory\n"));
279                         SMB_VFS_NEXT_CLOSEDIR(handle,p);
280                         return -1;
281                 }
282
283                 snprintf(tlabels[shadow_copy_data->num_volumes++], sizeof(*tlabels), "%s",d->d_name);
284
285                 shadow_copy_data->labels = tlabels;
286         }
287
288         SMB_VFS_NEXT_CLOSEDIR(handle,p);
289         return 0;
290 }
291
292 static struct vfs_fn_pointers vfs_shadow_copy_fns = {
293         .opendir_fn = shadow_copy_opendir,
294         .fdopendir_fn = shadow_copy_fdopendir,
295         .readdir_fn = shadow_copy_readdir,
296         .seekdir_fn = shadow_copy_seekdir,
297         .telldir_fn = shadow_copy_telldir,
298         .rewind_dir_fn = shadow_copy_rewinddir,
299         .closedir_fn = shadow_copy_closedir,
300         .get_shadow_copy_data_fn = shadow_copy_get_shadow_copy_data,
301 };
302
303 NTSTATUS vfs_shadow_copy_init(void);
304 NTSTATUS vfs_shadow_copy_init(void)
305 {
306         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
307                                         "shadow_copy", &vfs_shadow_copy_fns);
308
309         if (!NT_STATUS_IS_OK(ret))
310                 return ret;
311
312         vfs_shadow_copy_debug_level = debug_add_class("shadow_copy");
313         if (vfs_shadow_copy_debug_level == -1) {
314                 vfs_shadow_copy_debug_level = DBGC_VFS;
315                 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
316                         "vfs_shadow_copy_init"));
317         } else {
318                 DEBUG(10, ("%s: Debug class number of '%s': %d\n", 
319                         "vfs_shadow_copy_init","shadow_copy",vfs_shadow_copy_debug_level));
320         }
321
322         return ret;
323 }