r15703: Fix d_printf call.
[Samba/nascimento.git] / source3 / modules / vfs_shadow_copy.c
blobdb1c8d007dc64aee88133ec7d22cd469db338732
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 2 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, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
24 Please read the VFS module Samba-HowTo-Collection.
25 there's a chapter about this module
27 For this share
28 Z:\
30 the ShadowCopies are in this directories
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\
36 e.g.
38 Z:\testfile.txt
39 Z:\@GMT-2003.08.05-12.02.00\testfile.txt
41 or:
43 Z:\testdir\testfile.txt
44 Z:\@GMT-2003.08.05-12.02.00\testdir\testfile.txt
47 Note: Files must differ to be displayed via Windows Explorer!
48 Directories are always displayed...
51 static int vfs_shadow_copy_debug_level = DBGC_VFS;
53 #undef DBGC_CLASS
54 #define DBGC_CLASS vfs_shadow_copy_debug_level
56 #define SHADOW_COPY_PREFIX "@GMT-"
57 #define SHADOW_COPY_SAMPLE "@GMT-2004.02.18-15.44.00"
59 typedef struct {
60 int pos;
61 int num;
62 SMB_STRUCT_DIRENT *dirs;
63 } shadow_copy_Dir;
65 static BOOL shadow_copy_match_name(const char *name)
67 if (strncmp(SHADOW_COPY_PREFIX,name, sizeof(SHADOW_COPY_PREFIX)-1)==0 &&
68 (strlen(SHADOW_COPY_SAMPLE) == strlen(name))) {
69 return True;
72 return False;
75 static SMB_STRUCT_DIR *shadow_copy_opendir(vfs_handle_struct *handle, connection_struct *conn, const char *fname, const char *mask, uint32 attr)
77 shadow_copy_Dir *dirp;
78 SMB_STRUCT_DIR *p = SMB_VFS_NEXT_OPENDIR(handle,conn,fname,mask,attr);
80 if (!p) {
81 DEBUG(0,("shadow_copy_opendir: SMB_VFS_NEXT_OPENDIR() failed for [%s]\n",fname));
82 return NULL;
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,conn,p);
89 return NULL;
92 ZERO_STRUCTP(dirp);
94 while (True) {
95 SMB_STRUCT_DIRENT *d;
97 d = SMB_VFS_NEXT_READDIR(handle, conn, p);
98 if (d == NULL) {
99 break;
102 if (shadow_copy_match_name(d->d_name)) {
103 DEBUG(8,("shadow_copy_opendir: hide [%s]\n",d->d_name));
104 continue;
107 DEBUG(10,("shadow_copy_opendir: not hide [%s]\n",d->d_name));
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;
115 dirp->dirs[dirp->num++] = *d;
118 SMB_VFS_NEXT_CLOSEDIR(handle,conn,p);
119 return((SMB_STRUCT_DIR *)dirp);
122 SMB_STRUCT_DIRENT *shadow_copy_readdir(vfs_handle_struct *handle, connection_struct *conn, SMB_STRUCT_DIR *_dirp)
124 shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
126 if (dirp->pos < dirp->num) {
127 return &(dirp->dirs[dirp->pos++]);
130 return NULL;
133 static void shadow_copy_seekdir(struct vfs_handle_struct *handle, struct connection_struct *conn, SMB_STRUCT_DIR *_dirp, long offset)
135 shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
137 if (offset < dirp->num) {
138 dirp->pos = offset ;
142 static long shadow_copy_telldir(struct vfs_handle_struct *handle, struct connection_struct *conn, SMB_STRUCT_DIR *_dirp)
144 shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
145 return( dirp->pos ) ;
148 static void shadow_copy_rewinddir(struct vfs_handle_struct *handle, struct connection_struct *conn, SMB_STRUCT_DIR *_dirp)
150 shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
151 dirp->pos = 0 ;
154 int shadow_copy_closedir(vfs_handle_struct *handle, connection_struct *conn, SMB_STRUCT_DIR *_dirp)
156 shadow_copy_Dir *dirp = (shadow_copy_Dir *)_dirp;
158 SAFE_FREE(dirp->dirs);
159 SAFE_FREE(dirp);
161 return 0;
164 static int shadow_copy_get_shadow_copy_data(vfs_handle_struct *handle, files_struct *fsp, SHADOW_COPY_DATA *shadow_copy_data, BOOL labels)
166 SMB_STRUCT_DIR *p = SMB_VFS_NEXT_OPENDIR(handle,fsp->conn,fsp->conn->connectpath,NULL,0);
168 shadow_copy_data->num_volumes = 0;
169 shadow_copy_data->labels = NULL;
171 if (!p) {
172 DEBUG(0,("shadow_copy_get_shadow_copy_data: SMB_VFS_NEXT_OPENDIR() failed for [%s]\n",fsp->conn->connectpath));
173 return -1;
176 while (True) {
177 SHADOW_COPY_LABEL *tlabels;
178 SMB_STRUCT_DIRENT *d;
180 d = SMB_VFS_NEXT_READDIR(handle, fsp->conn, p);
181 if (d == NULL) {
182 break;
185 /* */
186 if (!shadow_copy_match_name(d->d_name)) {
187 DEBUG(10,("shadow_copy_get_shadow_copy_data: ignore [%s]\n",d->d_name));
188 continue;
191 DEBUG(7,("shadow_copy_get_shadow_copy_data: not ignore [%s]\n",d->d_name));
193 if (!labels) {
194 shadow_copy_data->num_volumes++;
195 continue;
198 tlabels = (SHADOW_COPY_LABEL *)TALLOC_REALLOC(shadow_copy_data->mem_ctx,
199 shadow_copy_data->labels,
200 (shadow_copy_data->num_volumes+1)*sizeof(SHADOW_COPY_LABEL));
201 if (tlabels == NULL) {
202 DEBUG(0,("shadow_copy_get_shadow_copy_data: Out of memory\n"));
203 SMB_VFS_NEXT_CLOSEDIR(handle,fsp->conn,p);
204 return -1;
207 snprintf(tlabels[shadow_copy_data->num_volumes++], sizeof(*tlabels), "%s",d->d_name);
209 shadow_copy_data->labels = tlabels;
212 SMB_VFS_NEXT_CLOSEDIR(handle,fsp->conn,p);
213 return 0;
216 /* VFS operations structure */
218 static vfs_op_tuple shadow_copy_ops[] = {
219 {SMB_VFS_OP(shadow_copy_opendir), SMB_VFS_OP_OPENDIR, SMB_VFS_LAYER_TRANSPARENT},
220 {SMB_VFS_OP(shadow_copy_readdir), SMB_VFS_OP_READDIR, SMB_VFS_LAYER_TRANSPARENT},
221 {SMB_VFS_OP(shadow_copy_seekdir), SMB_VFS_OP_SEEKDIR, SMB_VFS_LAYER_TRANSPARENT},
222 {SMB_VFS_OP(shadow_copy_telldir), SMB_VFS_OP_TELLDIR, SMB_VFS_LAYER_TRANSPARENT},
223 {SMB_VFS_OP(shadow_copy_rewinddir), SMB_VFS_OP_REWINDDIR, SMB_VFS_LAYER_TRANSPARENT},
224 {SMB_VFS_OP(shadow_copy_closedir), SMB_VFS_OP_CLOSEDIR, SMB_VFS_LAYER_TRANSPARENT},
226 {SMB_VFS_OP(shadow_copy_get_shadow_copy_data), SMB_VFS_OP_GET_SHADOW_COPY_DATA,SMB_VFS_LAYER_OPAQUE},
228 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
231 NTSTATUS vfs_shadow_copy_init(void)
233 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "shadow_copy", shadow_copy_ops);
235 if (!NT_STATUS_IS_OK(ret))
236 return ret;
238 vfs_shadow_copy_debug_level = debug_add_class("shadow_copy");
239 if (vfs_shadow_copy_debug_level == -1) {
240 vfs_shadow_copy_debug_level = DBGC_VFS;
241 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
242 "vfs_shadow_copy_init"));
243 } else {
244 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
245 "vfs_shadow_copy_init","shadow_copy",vfs_shadow_copy_debug_level));
248 return ret;