libsmb: Convert cli_NetServerEnum() away from cli_api()
[samba4-gss.git] / source3 / modules / vfs_shadow_copy.c
blob62b929188861a90cc227080bf8fd22aed1546f9c
1 /*
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/>.
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "ntioctl.h"
23 #include "source3/smbd/dir.h"
26 Please read the VFS module Samba-HowTo-Collection.
27 there's a chapter about this module
29 For this share
30 Z:\
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\
38 e.g.
40 Z:\testfile.txt
41 Z:\@GMT-2003.08.05-12.02.00\testfile.txt
43 or:
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;
55 #undef DBGC_CLASS
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"
61 typedef struct {
62 int pos;
63 int num;
64 struct dirent *dirs;
65 } shadow_copy_Dir;
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))) {
71 return True;
74 return False;
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);
82 if (!p) {
83 DEBUG(10,("shadow_copy_opendir: SMB_VFS_NEXT_FDOPENDIR() failed for [%s]\n",
84 smb_fname_str_dbg(fsp->fsp_name)));
85 return NULL;
88 dirp = SMB_MALLOC_P(shadow_copy_Dir);
89 if (!dirp) {
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. */
93 fsp_set_fd(fsp, -1);
94 return NULL;
97 ZERO_STRUCTP(dirp);
99 while (True) {
100 struct dirent *d;
102 d = SMB_VFS_NEXT_READDIR(handle, fsp, p);
103 if (d == NULL) {
104 break;
107 if (shadow_copy_match_name(d->d_name)) {
108 DEBUG(8,("shadow_copy_fdopendir: hide [%s]\n",d->d_name));
109 continue;
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);
115 if (!dirp->dirs) {
116 DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
117 break;
120 dirp->dirs[dirp->num++] = *d;
123 SMB_VFS_NEXT_CLOSEDIR(handle,p);
124 /* We have now closed the fd in fsp. */
125 fsp_set_fd(fsp, -1);
126 return((DIR *)dirp);
129 static struct dirent *shadow_copy_readdir(vfs_handle_struct *handle,
130 struct files_struct *dirfsp,
131 DIR *_dirp)
133 shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
135 if (dirp->pos < dirp->num) {
136 return &(dirp->dirs[dirp->pos++]);
139 return NULL;
142 static void shadow_copy_rewinddir(struct vfs_handle_struct *handle, DIR *_dirp)
144 shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
145 dirp->pos = 0 ;
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);
153 SAFE_FREE(dirp);
155 return 0;
158 static int shadow_copy_get_shadow_copy_data(vfs_handle_struct *handle,
159 files_struct *fsp,
160 struct shadow_copy_data *shadow_copy_data,
161 bool labels)
163 struct smb_Dir *dir_hnd = NULL;
164 const char *dname = NULL;
165 char *talloced = NULL;
166 NTSTATUS status;
167 struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
168 fsp->conn->connectpath,
169 NULL,
170 NULL,
173 if (smb_fname == NULL) {
174 errno = ENOMEM;
175 return -1;
178 status = OpenDir(talloc_tos(),
179 handle->conn,
180 smb_fname,
181 NULL,
183 &dir_hnd);
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);
188 return -1;
191 shadow_copy_data->num_volumes = 0;
192 shadow_copy_data->labels = NULL;
194 while (True) {
195 SHADOW_COPY_LABEL *tlabels;
196 int ret;
198 dname = ReadDirName(dir_hnd, &talloced);
199 if (dname == NULL) {
200 break;
203 /* */
204 if (!shadow_copy_match_name(dname)) {
205 DBG_DEBUG("ignore [%s]\n", dname);
206 TALLOC_FREE(talloced);
207 continue;
210 DBG_DEBUG("not ignore [%s]\n", dname);
212 if (!labels) {
213 shadow_copy_data->num_volumes++;
214 TALLOC_FREE(talloced);
215 continue;
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);
225 return -1;
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);
234 return -1;
236 shadow_copy_data->num_volumes++;
238 shadow_copy_data->labels = tlabels;
239 TALLOC_FREE(talloced);
242 TALLOC_FREE(dir_hnd);
243 return 0;
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,
254 static_decl_vfs;
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))
261 return 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"));
268 } else {
269 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
270 "vfs_shadow_copy_init","shadow_copy",vfs_shadow_copy_debug_level));
273 return ret;