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