smbd: Remove unused [push_pull]_file_id_24
[samba.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
22 /*
23     Please read the VFS module Samba-HowTo-Collection.
24     there's a chapter about this module
25
26     For this share
27     Z:\
28
29     the ShadowCopies are in this directories
30
31     Z:\@GMT-2003.08.05-12.00.00\
32     Z:\@GMT-2003.08.05-12.01.00\
33     Z:\@GMT-2003.08.05-12.02.00\
34
35     e.g.
36     
37     Z:\testfile.txt
38     Z:\@GMT-2003.08.05-12.02.00\testfile.txt
39
40     or:
41
42     Z:\testdir\testfile.txt
43     Z:\@GMT-2003.08.05-12.02.00\testdir\testfile.txt
44
45
46     Note: Files must differ to be displayed via Windows Explorer!
47           Directories are always displayed...    
48 */
49
50 static int vfs_shadow_copy_debug_level = DBGC_VFS;
51
52 #undef DBGC_CLASS
53 #define DBGC_CLASS vfs_shadow_copy_debug_level
54
55 #define SHADOW_COPY_PREFIX "@GMT-"
56 #define SHADOW_COPY_SAMPLE "@GMT-2004.02.18-15.44.00"
57
58 typedef struct {
59         int pos;
60         int num;
61         SMB_STRUCT_DIRENT *dirs;
62 } shadow_copy_Dir;
63
64 static bool shadow_copy_match_name(const char *name)
65 {
66         if (strncmp(SHADOW_COPY_PREFIX,name, sizeof(SHADOW_COPY_PREFIX)-1)==0 &&
67                 (strlen(SHADOW_COPY_SAMPLE) == strlen(name))) {
68                 return True;
69         }
70
71         return False;
72 }
73
74 static SMB_STRUCT_DIR *shadow_copy_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
75 {
76         shadow_copy_Dir *dirp;
77         SMB_STRUCT_DIR *p = SMB_VFS_NEXT_OPENDIR(handle,fname,mask,attr);
78
79         if (!p) {
80                 DEBUG(0,("shadow_copy_opendir: SMB_VFS_NEXT_OPENDIR() failed for [%s]\n",fname));
81                 return NULL;
82         }
83
84         dirp = SMB_MALLOC_P(shadow_copy_Dir);
85         if (!dirp) {
86                 DEBUG(0,("shadow_copy_opendir: Out of memory\n"));
87                 SMB_VFS_NEXT_CLOSEDIR(handle,p);
88                 return NULL;
89         }
90
91         ZERO_STRUCTP(dirp);
92
93         while (True) {
94                 SMB_STRUCT_DIRENT *d;
95
96                 d = SMB_VFS_NEXT_READDIR(handle, p, NULL);
97                 if (d == NULL) {
98                         break;
99                 }
100
101                 if (shadow_copy_match_name(d->d_name)) {
102                         DEBUG(8,("shadow_copy_opendir: hide [%s]\n",d->d_name));
103                         continue;
104                 }
105
106                 DEBUG(10,("shadow_copy_opendir: not hide [%s]\n",d->d_name));
107
108                 dirp->dirs = SMB_REALLOC_ARRAY(dirp->dirs,SMB_STRUCT_DIRENT, dirp->num+1);
109                 if (!dirp->dirs) {
110                         DEBUG(0,("shadow_copy_opendir: Out of memory\n"));
111                         break;
112                 }
113
114                 dirp->dirs[dirp->num++] = *d;
115         }
116
117         SMB_VFS_NEXT_CLOSEDIR(handle,p);
118         return((SMB_STRUCT_DIR *)dirp);
119 }
120
121 static SMB_STRUCT_DIR *shadow_copy_fdopendir(vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32 attr)
122 {
123         shadow_copy_Dir *dirp;
124         SMB_STRUCT_DIR *p = SMB_VFS_NEXT_FDOPENDIR(handle,fsp,mask,attr);
125
126         if (!p) {
127                 DEBUG(10,("shadow_copy_opendir: SMB_VFS_NEXT_FDOPENDIR() failed for [%s]\n",
128                         smb_fname_str_dbg(fsp->fsp_name)));
129                 return NULL;
130         }
131
132         dirp = SMB_MALLOC_P(shadow_copy_Dir);
133         if (!dirp) {
134                 DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
135                 SMB_VFS_NEXT_CLOSEDIR(handle,p);
136                 /* We have now closed the fd in fsp. */
137                 fsp->fh->fd = -1;
138                 return NULL;
139         }
140
141         ZERO_STRUCTP(dirp);
142
143         while (True) {
144                 SMB_STRUCT_DIRENT *d;
145
146                 d = SMB_VFS_NEXT_READDIR(handle, p, NULL);
147                 if (d == NULL) {
148                         break;
149                 }
150
151                 if (shadow_copy_match_name(d->d_name)) {
152                         DEBUG(8,("shadow_copy_fdopendir: hide [%s]\n",d->d_name));
153                         continue;
154                 }
155
156                 DEBUG(10,("shadow_copy_fdopendir: not hide [%s]\n",d->d_name));
157
158                 dirp->dirs = SMB_REALLOC_ARRAY(dirp->dirs,SMB_STRUCT_DIRENT, dirp->num+1);
159                 if (!dirp->dirs) {
160                         DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
161                         break;
162                 }
163
164                 dirp->dirs[dirp->num++] = *d;
165         }
166
167         SMB_VFS_NEXT_CLOSEDIR(handle,p);
168         /* We have now closed the fd in fsp. */
169         fsp->fh->fd = -1;
170         return((SMB_STRUCT_DIR *)dirp);
171 }
172
173 static SMB_STRUCT_DIRENT *shadow_copy_readdir(vfs_handle_struct *handle,
174                                               SMB_STRUCT_DIR *_dirp,
175                                               SMB_STRUCT_STAT *sbuf)
176 {
177         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
178
179         if (dirp->pos < dirp->num) {
180                 return &(dirp->dirs[dirp->pos++]);
181         }
182
183         return NULL;
184 }
185
186 static void shadow_copy_seekdir(struct vfs_handle_struct *handle, SMB_STRUCT_DIR *_dirp, long offset)
187 {
188         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
189
190         if (offset < dirp->num) {
191                 dirp->pos = offset ;
192         }
193 }
194
195 static long shadow_copy_telldir(struct vfs_handle_struct *handle, SMB_STRUCT_DIR *_dirp)
196 {
197         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
198         return( dirp->pos ) ;
199 }
200
201 static void shadow_copy_rewinddir(struct vfs_handle_struct *handle, SMB_STRUCT_DIR *_dirp)
202 {
203         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
204         dirp->pos = 0 ;
205 }
206
207 static int shadow_copy_closedir(vfs_handle_struct *handle, SMB_STRUCT_DIR *_dirp)
208 {
209         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
210
211         SAFE_FREE(dirp->dirs);
212         SAFE_FREE(dirp);
213  
214         return 0;       
215 }
216
217 static int shadow_copy_get_shadow_copy_data(vfs_handle_struct *handle, files_struct *fsp, SHADOW_COPY_DATA *shadow_copy_data, bool labels)
218 {
219         SMB_STRUCT_DIR *p = SMB_VFS_NEXT_OPENDIR(handle,fsp->conn->connectpath,NULL,0);
220
221         shadow_copy_data->num_volumes = 0;
222         shadow_copy_data->labels = NULL;
223
224         if (!p) {
225                 DEBUG(0,("shadow_copy_get_shadow_copy_data: SMB_VFS_NEXT_OPENDIR() failed for [%s]\n",fsp->conn->connectpath));
226                 return -1;
227         }
228
229         while (True) {
230                 SHADOW_COPY_LABEL *tlabels;
231                 SMB_STRUCT_DIRENT *d;
232
233                 d = SMB_VFS_NEXT_READDIR(handle, p, NULL);
234                 if (d == NULL) {
235                         break;
236                 }
237
238                 /* */
239                 if (!shadow_copy_match_name(d->d_name)) {
240                         DEBUG(10,("shadow_copy_get_shadow_copy_data: ignore [%s]\n",d->d_name));
241                         continue;
242                 }
243
244                 DEBUG(7,("shadow_copy_get_shadow_copy_data: not ignore [%s]\n",d->d_name));
245
246                 if (!labels) {
247                         shadow_copy_data->num_volumes++;
248                         continue;
249                 }
250
251                 tlabels = (SHADOW_COPY_LABEL *)TALLOC_REALLOC(shadow_copy_data->mem_ctx,
252                                                                         shadow_copy_data->labels,
253                                                                         (shadow_copy_data->num_volumes+1)*sizeof(SHADOW_COPY_LABEL));
254                 if (tlabels == NULL) {
255                         DEBUG(0,("shadow_copy_get_shadow_copy_data: Out of memory\n"));
256                         SMB_VFS_NEXT_CLOSEDIR(handle,p);
257                         return -1;
258                 }
259
260                 snprintf(tlabels[shadow_copy_data->num_volumes++], sizeof(*tlabels), "%s",d->d_name);
261
262                 shadow_copy_data->labels = tlabels;
263         }
264
265         SMB_VFS_NEXT_CLOSEDIR(handle,p);
266         return 0;
267 }
268
269 static struct vfs_fn_pointers vfs_shadow_copy_fns = {
270         .opendir = shadow_copy_opendir,
271         .fdopendir = shadow_copy_fdopendir,
272         .readdir = shadow_copy_readdir,
273         .seekdir = shadow_copy_seekdir,
274         .telldir = shadow_copy_telldir,
275         .rewind_dir = shadow_copy_rewinddir,
276         .closedir = shadow_copy_closedir,
277         .get_shadow_copy_data = shadow_copy_get_shadow_copy_data,
278 };
279
280 NTSTATUS vfs_shadow_copy_init(void);
281 NTSTATUS vfs_shadow_copy_init(void)
282 {
283         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
284                                         "shadow_copy", &vfs_shadow_copy_fns);
285
286         if (!NT_STATUS_IS_OK(ret))
287                 return ret;
288
289         vfs_shadow_copy_debug_level = debug_add_class("shadow_copy");
290         if (vfs_shadow_copy_debug_level == -1) {
291                 vfs_shadow_copy_debug_level = DBGC_VFS;
292                 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
293                         "vfs_shadow_copy_init"));
294         } else {
295                 DEBUG(10, ("%s: Debug class number of '%s': %d\n", 
296                         "vfs_shadow_copy_init","shadow_copy",vfs_shadow_copy_debug_level));
297         }
298
299         return ret;
300 }