88135c0aed14b0f77a89ed745993d2f98be0c685
[abartlet/samba.git/.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 2 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, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.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 DIR *shadow_copy_opendir(vfs_handle_struct *handle, connection_struct *conn, const char *fname)
76 {
77         shadow_copy_Dir *dirp;
78         DIR *p = SMB_VFS_NEXT_OPENDIR(handle,conn,fname);
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 = (shadow_copy_Dir *)malloc(sizeof(shadow_copy_Dir));
86         if (!dirp) {
87                 DEBUG(0,("shadow_copy_opendir: Out of memory\n"));
88                 SMB_VFS_NEXT_CLOSEDIR(handle,conn,p);
89                 return NULL;
90         }
91
92         ZERO_STRUCTP(dirp);
93
94         while (True) {
95                 SMB_STRUCT_DIRENT *d;
96                 SMB_STRUCT_DIRENT *r;
97
98
99                 d = SMB_VFS_NEXT_READDIR(handle, conn, p);
100                 if (d == NULL) {
101                         break;
102                 }
103
104                 if (shadow_copy_match_name(d->d_name)) {
105                         DEBUG(8,("shadow_copy_opendir: hide [%s]\n",d->d_name));
106                         continue;
107                 }
108
109                 DEBUG(10,("shadow_copy_opendir: not hide [%s]\n",d->d_name));
110
111                 r = (SMB_STRUCT_DIRENT *)Realloc(dirp->dirs,(dirp->num+1)*sizeof(SMB_STRUCT_DIRENT));
112                 if (!r) {
113                         DEBUG(0,("shadow_copy_opendir: Out of memory\n"));
114                         break;
115                 }
116
117                 dirp->dirs = r;
118                 dirp->dirs[dirp->num++] = *d;
119         }
120
121         SMB_VFS_NEXT_CLOSEDIR(handle,conn,p);
122         return((DIR *)dirp);
123 }
124
125 SMB_STRUCT_DIRENT *shadow_copy_readdir(vfs_handle_struct *handle, connection_struct *conn, DIR *_dirp)
126 {
127         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
128
129         if (dirp->pos < dirp->num) {
130                 return &(dirp->dirs[dirp->pos++]);
131         }
132
133         return NULL;
134 }
135
136 int shadow_copy_closedir(vfs_handle_struct *handle, connection_struct *conn, DIR *_dirp)
137 {
138         shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
139
140         SAFE_FREE(dirp);
141  
142         return 0;       
143 }
144
145 static int shadow_copy_get_shadow_copy_data(vfs_handle_struct *handle, files_struct *fsp, SHADOW_COPY_DATA *shadow_copy_data, BOOL labels)
146 {
147         DIR *p = SMB_VFS_NEXT_OPENDIR(handle,fsp->conn,fsp->conn->connectpath);
148
149         shadow_copy_data->num_volumes = 0;
150         shadow_copy_data->labels = NULL;
151
152         if (!p) {
153                 DEBUG(0,("shadow_copy_get_shadow_copy_data: SMB_VFS_NEXT_OPENDIR() failed for [%s]\n",fsp->conn->connectpath));
154                 return -1;
155         }
156
157         while (True) {
158                 SHADOW_COPY_LABEL *tlabels;
159                 SMB_STRUCT_DIRENT *d;
160
161                 d = SMB_VFS_NEXT_READDIR(handle, fsp->conn, p);
162                 if (d == NULL) {
163                         break;
164                 }
165
166                 /* */
167                 if (!shadow_copy_match_name(d->d_name)) {
168                         DEBUG(10,("shadow_copy_get_shadow_copy_data: ignore [%s]\n",d->d_name));
169                         continue;
170                 }
171
172                 DEBUG(7,("shadow_copy_get_shadow_copy_data: not ignore [%s]\n",d->d_name));
173
174                 if (!labels) {
175                         shadow_copy_data->num_volumes++;
176                         continue;
177                 }
178
179                 tlabels = (SHADOW_COPY_LABEL *)talloc_realloc(shadow_copy_data->mem_ctx,
180                                                                         shadow_copy_data->labels,
181                                                                         (shadow_copy_data->num_volumes+1)*sizeof(SHADOW_COPY_LABEL));
182                 if (tlabels == NULL) {
183                         DEBUG(0,("shadow_copy_get_shadow_copy_data: Out of memory\n"));
184                         SMB_VFS_NEXT_CLOSEDIR(handle,fsp->conn,p);
185                         return -1;
186                 }
187
188                 snprintf(tlabels[shadow_copy_data->num_volumes++], sizeof(*tlabels), "%s",d->d_name);
189
190                 shadow_copy_data->labels = tlabels;
191         }
192
193         SMB_VFS_NEXT_CLOSEDIR(handle,fsp->conn,p);
194         return 0;
195 }
196
197 /* VFS operations structure */
198
199 static vfs_op_tuple shadow_copy_ops[] = {
200         {SMB_VFS_OP(shadow_copy_opendir),               SMB_VFS_OP_OPENDIR,             SMB_VFS_LAYER_TRANSPARENT},
201         {SMB_VFS_OP(shadow_copy_readdir),               SMB_VFS_OP_READDIR,             SMB_VFS_LAYER_TRANSPARENT},
202         {SMB_VFS_OP(shadow_copy_closedir),              SMB_VFS_OP_CLOSEDIR,            SMB_VFS_LAYER_TRANSPARENT},
203
204         {SMB_VFS_OP(shadow_copy_get_shadow_copy_data),  SMB_VFS_OP_GET_SHADOW_COPY_DATA,SMB_VFS_LAYER_OPAQUE},
205
206         {SMB_VFS_OP(NULL),                              SMB_VFS_OP_NOOP,                SMB_VFS_LAYER_NOOP}
207 };
208
209 NTSTATUS vfs_shadow_copy_init(void)
210 {
211         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "shadow_copy", shadow_copy_ops);
212
213         if (!NT_STATUS_IS_OK(ret))
214                 return ret;
215
216         vfs_shadow_copy_debug_level = debug_add_class("shadow_copy");
217         if (vfs_shadow_copy_debug_level == -1) {
218                 vfs_shadow_copy_debug_level = DBGC_VFS;
219                 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
220                         "vfs_shadow_copy_init"));
221         } else {
222                 DEBUG(10, ("%s: Debug class number of '%s': %d\n", 
223                         "vfs_shadow_copy_init","shadow_copy",vfs_shadow_copy_debug_level));
224         }
225
226         return ret;
227 }