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"
25 Please read the VFS module Samba-HowTo-Collection.
26 there's a chapter about this module
31 the ShadowCopies are in this directories
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\
40 Z:\@GMT-2003.08.05-12.02.00\testfile.txt
44 Z:\testdir\testfile.txt
45 Z:\@GMT-2003.08.05-12.02.00\testdir\testfile.txt
48 Note: Files must differ to be displayed via Windows Explorer!
49 Directories are always displayed...
52 static int vfs_shadow_copy_debug_level = DBGC_VFS;
55 #define DBGC_CLASS vfs_shadow_copy_debug_level
57 #define SHADOW_COPY_PREFIX "@GMT-"
58 #define SHADOW_COPY_SAMPLE "@GMT-2004.02.18-15.44.00"
66 static bool shadow_copy_match_name(const char *name)
68 if (strncmp(SHADOW_COPY_PREFIX,name, sizeof(SHADOW_COPY_PREFIX)-1)==0 &&
69 (strlen(SHADOW_COPY_SAMPLE) == strlen(name))) {
76 static DIR *shadow_copy_fdopendir(vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32_t attr)
78 shadow_copy_Dir *dirp;
79 DIR *p = SMB_VFS_NEXT_FDOPENDIR(handle,fsp,mask,attr);
82 DEBUG(10,("shadow_copy_opendir: SMB_VFS_NEXT_FDOPENDIR() failed for [%s]\n",
83 smb_fname_str_dbg(fsp->fsp_name)));
87 dirp = SMB_MALLOC_P(shadow_copy_Dir);
89 DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
90 SMB_VFS_NEXT_CLOSEDIR(handle,p);
91 /* We have now closed the fd in fsp. */
101 d = SMB_VFS_NEXT_READDIR(handle, fsp, p);
106 if (shadow_copy_match_name(d->d_name)) {
107 DEBUG(8,("shadow_copy_fdopendir: hide [%s]\n",d->d_name));
111 DEBUG(10,("shadow_copy_fdopendir: not hide [%s]\n",d->d_name));
113 dirp->dirs = SMB_REALLOC_ARRAY(dirp->dirs,struct dirent, dirp->num+1);
115 DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
119 dirp->dirs[dirp->num++] = *d;
122 SMB_VFS_NEXT_CLOSEDIR(handle,p);
123 /* We have now closed the fd in fsp. */
128 static struct dirent *shadow_copy_readdir(vfs_handle_struct *handle,
129 struct files_struct *dirfsp,
132 shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
134 if (dirp->pos < dirp->num) {
135 return &(dirp->dirs[dirp->pos++]);
141 static void shadow_copy_rewinddir(struct vfs_handle_struct *handle, DIR *_dirp)
143 shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
147 static int shadow_copy_closedir(vfs_handle_struct *handle, DIR *_dirp)
149 shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
151 SAFE_FREE(dirp->dirs);
157 static int shadow_copy_get_shadow_copy_data(vfs_handle_struct *handle,
159 struct shadow_copy_data *shadow_copy_data,
162 struct smb_Dir *dir_hnd = NULL;
163 const char *dname = NULL;
164 char *talloced = NULL;
166 struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
167 fsp->conn->connectpath,
172 if (smb_fname == NULL) {
177 status = OpenDir(talloc_tos(),
183 TALLOC_FREE(smb_fname);
184 if (!NT_STATUS_IS_OK(status)) {
185 DBG_ERR("OpenDir() failed for [%s]\n", fsp->conn->connectpath);
186 errno = map_errno_from_nt_status(status);
190 shadow_copy_data->num_volumes = 0;
191 shadow_copy_data->labels = NULL;
194 SHADOW_COPY_LABEL *tlabels;
197 dname = ReadDirName(dir_hnd, &talloced);
203 if (!shadow_copy_match_name(dname)) {
204 DBG_DEBUG("ignore [%s]\n", dname);
205 TALLOC_FREE(talloced);
209 DBG_DEBUG("not ignore [%s]\n", dname);
212 shadow_copy_data->num_volumes++;
213 TALLOC_FREE(talloced);
217 tlabels = (SHADOW_COPY_LABEL *)TALLOC_REALLOC(shadow_copy_data,
218 shadow_copy_data->labels,
219 (shadow_copy_data->num_volumes+1)*sizeof(SHADOW_COPY_LABEL));
220 if (tlabels == NULL) {
221 DEBUG(0,("shadow_copy_get_shadow_copy_data: Out of memory\n"));
222 TALLOC_FREE(talloced);
223 TALLOC_FREE(dir_hnd);
227 ret = strlcpy(tlabels[shadow_copy_data->num_volumes], dname,
228 sizeof(tlabels[shadow_copy_data->num_volumes]));
229 if (ret != sizeof(tlabels[shadow_copy_data->num_volumes]) - 1) {
230 DBG_ERR("malformed label %s\n", dname);
231 TALLOC_FREE(talloced);
232 TALLOC_FREE(dir_hnd);
235 shadow_copy_data->num_volumes++;
237 shadow_copy_data->labels = tlabels;
238 TALLOC_FREE(talloced);
241 TALLOC_FREE(dir_hnd);
245 static struct vfs_fn_pointers vfs_shadow_copy_fns = {
246 .fdopendir_fn = shadow_copy_fdopendir,
247 .readdir_fn = shadow_copy_readdir,
248 .rewind_dir_fn = shadow_copy_rewinddir,
249 .closedir_fn = shadow_copy_closedir,
250 .get_shadow_copy_data_fn = shadow_copy_get_shadow_copy_data,
254 NTSTATUS vfs_shadow_copy_init(TALLOC_CTX *ctx)
256 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
257 "shadow_copy", &vfs_shadow_copy_fns);
259 if (!NT_STATUS_IS_OK(ret))
262 vfs_shadow_copy_debug_level = debug_add_class("shadow_copy");
263 if (vfs_shadow_copy_debug_level == -1) {
264 vfs_shadow_copy_debug_level = DBGC_VFS;
265 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
266 "vfs_shadow_copy_init"));
268 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
269 "vfs_shadow_copy_init","shadow_copy",vfs_shadow_copy_debug_level));