2 * implementation of an Shadow Copy module
4 * Copyright (C) Stefan Metzmacher 2003-2004
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.
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.
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/>.
21 #include "smbd/smbd.h"
23 #include "source3/smbd/dir.h"
26 Please read the VFS module Samba-HowTo-Collection.
27 there's a chapter about this module
32 the ShadowCopies are in this directories
34 Z:\@GMT-2003.08.05-12.00.00\
35 Z:\@GMT-2003.08.05-12.01.00\
36 Z:\@GMT-2003.08.05-12.02.00\
41 Z:\@GMT-2003.08.05-12.02.00\testfile.txt
45 Z:\testdir\testfile.txt
46 Z:\@GMT-2003.08.05-12.02.00\testdir\testfile.txt
49 Note: Files must differ to be displayed via Windows Explorer!
50 Directories are always displayed...
53 static int vfs_shadow_copy_debug_level
= DBGC_VFS
;
56 #define DBGC_CLASS vfs_shadow_copy_debug_level
58 #define SHADOW_COPY_PREFIX "@GMT-"
59 #define SHADOW_COPY_SAMPLE "@GMT-2004.02.18-15.44.00"
67 static bool shadow_copy_match_name(const char *name
)
69 if (strncmp(SHADOW_COPY_PREFIX
,name
, sizeof(SHADOW_COPY_PREFIX
)-1)==0 &&
70 (strlen(SHADOW_COPY_SAMPLE
) == strlen(name
))) {
77 static DIR *shadow_copy_fdopendir(vfs_handle_struct
*handle
, files_struct
*fsp
, const char *mask
, uint32_t attr
)
79 shadow_copy_Dir
*dirp
;
80 DIR *p
= SMB_VFS_NEXT_FDOPENDIR(handle
,fsp
,mask
,attr
);
83 DEBUG(10,("shadow_copy_opendir: SMB_VFS_NEXT_FDOPENDIR() failed for [%s]\n",
84 smb_fname_str_dbg(fsp
->fsp_name
)));
88 dirp
= SMB_MALLOC_P(shadow_copy_Dir
);
90 DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
91 SMB_VFS_NEXT_CLOSEDIR(handle
,p
);
92 /* We have now closed the fd in fsp. */
102 d
= SMB_VFS_NEXT_READDIR(handle
, fsp
, p
);
107 if (shadow_copy_match_name(d
->d_name
)) {
108 DEBUG(8,("shadow_copy_fdopendir: hide [%s]\n",d
->d_name
));
112 DEBUG(10,("shadow_copy_fdopendir: not hide [%s]\n",d
->d_name
));
114 dirp
->dirs
= SMB_REALLOC_ARRAY(dirp
->dirs
,struct dirent
, dirp
->num
+1);
116 DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
120 dirp
->dirs
[dirp
->num
++] = *d
;
123 SMB_VFS_NEXT_CLOSEDIR(handle
,p
);
124 /* We have now closed the fd in fsp. */
129 static struct dirent
*shadow_copy_readdir(vfs_handle_struct
*handle
,
130 struct files_struct
*dirfsp
,
133 shadow_copy_Dir
*dirp
= (shadow_copy_Dir
*)_dirp
;
135 if (dirp
->pos
< dirp
->num
) {
136 return &(dirp
->dirs
[dirp
->pos
++]);
142 static void shadow_copy_rewinddir(struct vfs_handle_struct
*handle
, DIR *_dirp
)
144 shadow_copy_Dir
*dirp
= (shadow_copy_Dir
*)_dirp
;
148 static int shadow_copy_closedir(vfs_handle_struct
*handle
, DIR *_dirp
)
150 shadow_copy_Dir
*dirp
= (shadow_copy_Dir
*)_dirp
;
152 SAFE_FREE(dirp
->dirs
);
158 static int shadow_copy_get_shadow_copy_data(vfs_handle_struct
*handle
,
160 struct shadow_copy_data
*shadow_copy_data
,
163 struct smb_Dir
*dir_hnd
= NULL
;
164 const char *dname
= NULL
;
165 char *talloced
= NULL
;
167 struct smb_filename
*smb_fname
= synthetic_smb_fname(talloc_tos(),
168 fsp
->conn
->connectpath
,
173 if (smb_fname
== NULL
) {
178 status
= OpenDir(talloc_tos(),
184 TALLOC_FREE(smb_fname
);
185 if (!NT_STATUS_IS_OK(status
)) {
186 DBG_ERR("OpenDir() failed for [%s]\n", fsp
->conn
->connectpath
);
187 errno
= map_errno_from_nt_status(status
);
191 shadow_copy_data
->num_volumes
= 0;
192 shadow_copy_data
->labels
= NULL
;
195 SHADOW_COPY_LABEL
*tlabels
;
198 dname
= ReadDirName(dir_hnd
, &talloced
);
204 if (!shadow_copy_match_name(dname
)) {
205 DBG_DEBUG("ignore [%s]\n", dname
);
206 TALLOC_FREE(talloced
);
210 DBG_DEBUG("not ignore [%s]\n", dname
);
213 shadow_copy_data
->num_volumes
++;
214 TALLOC_FREE(talloced
);
218 tlabels
= (SHADOW_COPY_LABEL
*)TALLOC_REALLOC(shadow_copy_data
,
219 shadow_copy_data
->labels
,
220 (shadow_copy_data
->num_volumes
+1)*sizeof(SHADOW_COPY_LABEL
));
221 if (tlabels
== NULL
) {
222 DEBUG(0,("shadow_copy_get_shadow_copy_data: Out of memory\n"));
223 TALLOC_FREE(talloced
);
224 TALLOC_FREE(dir_hnd
);
228 ret
= strlcpy(tlabels
[shadow_copy_data
->num_volumes
], dname
,
229 sizeof(tlabels
[shadow_copy_data
->num_volumes
]));
230 if (ret
!= sizeof(tlabels
[shadow_copy_data
->num_volumes
]) - 1) {
231 DBG_ERR("malformed label %s\n", dname
);
232 TALLOC_FREE(talloced
);
233 TALLOC_FREE(dir_hnd
);
236 shadow_copy_data
->num_volumes
++;
238 shadow_copy_data
->labels
= tlabels
;
239 TALLOC_FREE(talloced
);
242 TALLOC_FREE(dir_hnd
);
246 static struct vfs_fn_pointers vfs_shadow_copy_fns
= {
247 .fdopendir_fn
= shadow_copy_fdopendir
,
248 .readdir_fn
= shadow_copy_readdir
,
249 .rewind_dir_fn
= shadow_copy_rewinddir
,
250 .closedir_fn
= shadow_copy_closedir
,
251 .get_shadow_copy_data_fn
= shadow_copy_get_shadow_copy_data
,
255 NTSTATUS
vfs_shadow_copy_init(TALLOC_CTX
*ctx
)
257 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
258 "shadow_copy", &vfs_shadow_copy_fns
);
260 if (!NT_STATUS_IS_OK(ret
))
263 vfs_shadow_copy_debug_level
= debug_add_class("shadow_copy");
264 if (vfs_shadow_copy_debug_level
== -1) {
265 vfs_shadow_copy_debug_level
= DBGC_VFS
;
266 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
267 "vfs_shadow_copy_init"));
269 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
270 "vfs_shadow_copy_init","shadow_copy",vfs_shadow_copy_debug_level
));