The branch, master has been updated
       via  a8c3d38 s3: Replace shadow_copy2 with a new implementation
      from  29e043a pyldb: add unit test for get(myattribute, defVal)

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit a8c3d38bc806c6972d10b6a371de8941da25a9ae
Author: Volker Lendecke <[email protected]>
Date:   Mon Jun 20 09:52:21 2011 +0200

    s3: Replace shadow_copy2 with a new implementation
    
    Autobuild-User: Volker Lendecke <[email protected]>
    Autobuild-Date: Mon Jun 20 11:17:47 CEST 2011 on sn-devel-104

-----------------------------------------------------------------------

Summary of changes:
 source3/modules/vfs_shadow_copy2.c | 1803 ++++++++++++++++++++++++------------
 1 files changed, 1205 insertions(+), 598 deletions(-)


Changeset truncated at 500 lines:

diff --git a/source3/modules/vfs_shadow_copy2.c 
b/source3/modules/vfs_shadow_copy2.c
index 25cc5d4..1ed828b 100644
--- a/source3/modules/vfs_shadow_copy2.c
+++ b/source3/modules/vfs_shadow_copy2.c
@@ -1,32 +1,29 @@
-/* 
- * implementation of an Shadow Copy module - version 2
+/*
+ * Third attempt at a shadow copy module
  *
- * Copyright (C) Andrew Tridgell     2007
- * Copyright (C) Ed Plese            2009
+ * Copyright (C) Andrew Tridgell   2007 (portions taken from shadow_copy2)
+ * Copyright (C) Ed Plese          2009
+ * Copyright (C) Volker Lendecke   2011
+ * Copyright (C) Christian Ambach  2011
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- *  
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- *  
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-#include "includes.h"
-#include "smbd/smbd.h"
-#include "system/filesys.h"
-#include "ntioctl.h"
-
 /*
 
-  This is a 2nd implemetation of a shadow copy module for exposing
+  This is a 3rd implemetation of a shadow copy module for exposing
   snapshots to windows clients as shadow copies. This version has the
   following features:
 
@@ -96,243 +93,169 @@
   The following command would generate a correctly formatted directory name
   for use with the default parameters:
      date -u +@GMT-%Y.%m.%d-%H.%M.%S
-  
  */
 
-static int vfs_shadow_copy2_debug_level = DBGC_VFS;
-
-#undef DBGC_CLASS
-#define DBGC_CLASS vfs_shadow_copy2_debug_level
+#include "includes.h"
+#include "system/filesys.h"
+#include "include/ntioctl.h"
+#include "smbd/proto.h"
+#include <tdb.h>
+#include "util_tdb.h"
 
 #define GMT_NAME_LEN 24 /* length of a @GMT- name */
-#define SHADOW_COPY2_GMT_FORMAT "@GMT-%Y.%m.%d-%H.%M.%S"
+#define GMT_FORMAT "@GMT-%Y.%m.%d-%H.%M.%S"
 
-#define SHADOW_COPY2_DEFAULT_SORT NULL
-#define SHADOW_COPY2_DEFAULT_FORMAT "@GMT-%Y.%m.%d-%H.%M.%S"
-#define SHADOW_COPY2_DEFAULT_LOCALTIME false
-
-/*
-  make very sure it is one of our special names 
- */
-static inline bool shadow_copy2_match_name(const char *name, const char 
**gmt_start)
+static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
+                                     size_t **poffsets,
+                                     unsigned *pnum_offsets)
 {
-       unsigned year, month, day, hr, min, sec;
+       unsigned num_offsets;
+       size_t *offsets;
        const char *p;
-       if (gmt_start) {
-               (*gmt_start) = NULL;
-       }
-       p = strstr_m(name, "@GMT-");
-       if (p == NULL) return false;
-       if (p > name && p[-1] != '/') return False;
-       if (sscanf(p, "@GMT-%04u.%02u.%02u-%02u.%02u.%02u", &year, &month,
-                  &day, &hr, &min, &sec) != 6) {
-               return False;
-       }
-       if (p[24] != 0 && p[24] != '/') {
-               return False;
-       }
-       if (gmt_start) {
-               (*gmt_start) = p;
-       }
-       return True;
-}
 
-static char *shadow_copy2_snapshot_to_gmt(TALLOC_CTX *mem_ctx,
-                               vfs_handle_struct *handle, const char *name)
-{
-       struct tm timestamp;
-       time_t timestamp_t;
-       char gmt[GMT_NAME_LEN + 1];
-       const char *fmt;
+       num_offsets = 0;
 
-       fmt = lp_parm_const_string(SNUM(handle->conn), "shadow",
-                                  "format", SHADOW_COPY2_DEFAULT_FORMAT);
+       p = str;
+       while ((p = strchr(p, '/')) != NULL) {
+               num_offsets += 1;
+               p += 1;
+       }
 
-       ZERO_STRUCT(timestamp);
-       if (strptime(name, fmt, &timestamp) == NULL) {
-               DEBUG(10, ("shadow_copy2_snapshot_to_gmt: no match %s: %s\n",
-                          fmt, name));
-               return NULL;
+       offsets = talloc_array(mem_ctx, size_t, num_offsets);
+       if (offsets == NULL) {
+               return false;
        }
 
-       DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n", fmt, name));
-       if (lp_parm_bool(SNUM(handle->conn), "shadow", "localtime",
-                        SHADOW_COPY2_DEFAULT_LOCALTIME))
-       {
-               timestamp.tm_isdst = -1;
-               timestamp_t = mktime(&timestamp);
-               gmtime_r(&timestamp_t, &timestamp);
+       p = str;
+       num_offsets = 0;
+       while ((p = strchr(p, '/')) != NULL) {
+               offsets[num_offsets] = p-str;
+               num_offsets += 1;
+               p += 1;
        }
-       strftime(gmt, sizeof(gmt), SHADOW_COPY2_GMT_FORMAT, &timestamp);
 
-       return talloc_strdup(mem_ctx, gmt);
+       *poffsets = offsets;
+       *pnum_offsets = num_offsets;
+       return true;
 }
 
-/*
-  shadow copy paths can also come into the server in this form:
-
-    /foo/bar/@GMT-XXXXX/some/file
-
-  This function normalises the filename to be of the form:
-
-    @GMT-XXXX/foo/bar/some/file
- */
-static const char *shadow_copy2_normalise_path(TALLOC_CTX *mem_ctx, const char 
*path, const char *gmt_start)
+static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
+                                       struct vfs_handle_struct *handle,
+                                       time_t snapshot)
 {
-       char *pcopy;
-       char buf[GMT_NAME_LEN];
-       size_t prefix_len;
+       struct tm snap_tm;
+       char gmt[NAME_MAX+1];
+       size_t gmt_len;
 
-       if (path == gmt_start) {
-               return path;
+       if (localtime_r(&snapshot, &snap_tm) == 0) {
+               DEBUG(10, ("gmtime_r failed\n"));
+               return NULL;
        }
-
-       prefix_len = gmt_start - path - 1;
-
-       DEBUG(10, ("path=%s, gmt_start=%s, prefix_len=%d\n", path, gmt_start,
-                  (int)prefix_len));
-
-       /*
-        * We've got a/b/c/@GMT-YYYY.MM.DD-HH.MM.SS/d/e. convert to
-        * @GMT-YYYY.MM.DD-HH.MM.SS/a/b/c/d/e before further
-        * processing. As many VFS calls provide a const char *,
-        * unfortunately we have to make a copy.
-        */
-
-       pcopy = talloc_strdup(talloc_tos(), path);
-       if (pcopy == NULL) {
+       gmt_len = strftime(gmt, sizeof(gmt),
+                          lp_parm_const_string(SNUM(handle->conn), "shadow",
+                                               "format", GMT_FORMAT),
+                          &snap_tm);
+       if (gmt_len == 0) {
+               DEBUG(10, ("strftime failed\n"));
                return NULL;
        }
-
-       gmt_start = pcopy + prefix_len;
-
-       /*
-        * Copy away "@GMT-YYYY.MM.DD-HH.MM.SS"
-        */
-       memcpy(buf, gmt_start+1, GMT_NAME_LEN);
-
-       /*
-        * Make space for it including a trailing /
-        */
-       memmove(pcopy + GMT_NAME_LEN + 1, pcopy, prefix_len);
-
-       /*
-        * Move in "@GMT-YYYY.MM.DD-HH.MM.SS/" at the beginning again
-        */
-       memcpy(pcopy, buf, GMT_NAME_LEN);
-       pcopy[GMT_NAME_LEN] = '/';
-
-       DEBUG(10, ("shadow_copy2_normalise_path: %s -> %s\n", path, pcopy));
-
-       return pcopy;
+       return talloc_asprintf(talloc_tos(), "/%s/%s",
+                              lp_parm_const_string(
+                                      SNUM(handle->conn), "shadow", "snapdir",
+                                      ".snapshots"),
+                              gmt);
 }
 
-/*
-  convert a name to the shadow directory
- */
-
-#define _SHADOW2_NEXT(op, args, rtype, eret, extra) do { \
-       const char *name = fname; \
-       const char *gmt_start; \
-       if (shadow_copy2_match_name(fname, &gmt_start)) {       \
-               char *name2; \
-               rtype ret; \
-               name2 = convert_shadow2_name(handle, fname, gmt_start); \
-               if (name2 == NULL) { \
-                       errno = EINVAL; \
-                       return eret; \
-               } \
-               name = name2; \
-               ret = SMB_VFS_NEXT_ ## op args; \
-               talloc_free(name2); \
-               if (ret != eret) extra; \
-               return ret; \
-       } else { \
-               return SMB_VFS_NEXT_ ## op args; \
-       } \
-} while (0)
-
-#define _SHADOW2_NEXT_SMB_FNAME(op, args, rtype, eret, extra) do { \
-       const char *gmt_start; \
-       if (shadow_copy2_match_name(smb_fname->base_name, &gmt_start)) { \
-               char *name2; \
-               char *smb_base_name_tmp = NULL; \
-               rtype ret; \
-               name2 = convert_shadow2_name(handle, smb_fname->base_name, 
gmt_start); \
-               if (name2 == NULL) { \
-                       errno = EINVAL; \
-                       return eret; \
-               } \
-               smb_base_name_tmp = smb_fname->base_name; \
-               smb_fname->base_name = name2; \
-               ret = SMB_VFS_NEXT_ ## op args; \
-               smb_fname->base_name = smb_base_name_tmp; \
-               talloc_free(name2); \
-               if (ret != eret) extra; \
-               return ret; \
-       } else { \
-               return SMB_VFS_NEXT_ ## op args; \
-       } \
-} while (0)
+static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
+                                       struct vfs_handle_struct *handle,
+                                       const char *name,
+                                       time_t *ptimestamp,
+                                       char **pstripped)
+{
+       struct tm tm;
+       time_t timestamp;
+       const char *p;
+       char *q;
+       char *stripped;
+       size_t rest_len, dst_len;
 
-/*
-  convert a name to the shadow directory: NTSTATUS-specific handling
- */
+       p = strstr_m(name, "@GMT-");
+       if (p == NULL) {
+               goto no_snapshot;
+       }
+       if ((p > name) && (p[-1] != '/')) {
+               goto no_snapshot;
+       }
+       q = strptime(p, GMT_FORMAT, &tm);
+       if (q == NULL) {
+               goto no_snapshot;
+       }
+       tm.tm_isdst = -1;
+       timestamp = mktime(&tm);
+       if (timestamp == (time_t)-1) {
+               goto no_snapshot;
+       }
+       if ((p == name) && (q[0] == '\0')) {
+               if (pstripped != NULL) {
+                       stripped = talloc_strdup(mem_ctx, "");
+                       if (stripped == NULL) {
+                               return false;
+                       }
+                       *pstripped = stripped;
+               }
+               *ptimestamp = timestamp;
+               return true;
+       }
+       if (q[0] != '/') {
+               goto no_snapshot;
+       }
+       q += 1;
+
+       rest_len = strlen(q);
+       dst_len = (p-name) + rest_len;
+
+       if (lp_parm_bool(SNUM(handle->conn), "shadow", "snapdirseverywhere",
+                        false)) {
+               char *insert;
+               bool have_insert;
+               insert = shadow_copy2_insert_string(talloc_tos(), handle,
+                                                   timestamp);
+               if (insert == NULL) {
+                       errno = ENOMEM;
+                       return false;
+               }
 
-#define _SHADOW2_NTSTATUS_NEXT(op, args, eret, extra) do { \
-        const char *name = fname; \
-        const char *gmt_start; \
-        if (shadow_copy2_match_name(fname, &gmt_start)) {      \
-                char *name2; \
-                NTSTATUS ret; \
-                name2 = convert_shadow2_name(handle, fname, gmt_start);        
\
-                if (name2 == NULL) { \
-                        errno = EINVAL; \
-                        return eret; \
-                } \
-                name = name2; \
-                ret = SMB_VFS_NEXT_ ## op args; \
-                talloc_free(name2); \
-                if (!NT_STATUS_EQUAL(ret, eret)) extra; \
-                return ret; \
-        } else { \
-                return SMB_VFS_NEXT_ ## op args; \
-        } \
-} while (0)
-
-#define SHADOW2_NTSTATUS_NEXT(op, args, eret) _SHADOW2_NTSTATUS_NEXT(op, args, 
eret, )
-
-#define SHADOW2_NEXT(op, args, rtype, eret) _SHADOW2_NEXT(op, args, rtype, 
eret, )
-
-#define SHADOW2_NEXT_SMB_FNAME(op, args, rtype, eret) 
_SHADOW2_NEXT_SMB_FNAME(op, args, rtype, eret, )
-
-#define SHADOW2_NEXT2(op, args) do { \
-       const char *gmt_start1, *gmt_start2; \
-       if (shadow_copy2_match_name(oldname, &gmt_start1) || \
-           shadow_copy2_match_name(newname, &gmt_start2)) {    \
-               errno = EROFS; \
-               return -1; \
-       } else { \
-               return SMB_VFS_NEXT_ ## op args; \
-       } \
-} while (0)
-
-#define SHADOW2_NEXT2_SMB_FNAME(op, args) do { \
-       const char *gmt_start1, *gmt_start2; \
-       if (shadow_copy2_match_name(smb_fname_src->base_name, &gmt_start1) || \
-           shadow_copy2_match_name(smb_fname_dst->base_name, &gmt_start2)) { \
-               errno = EROFS; \
-               return -1; \
-       } else { \
-               return SMB_VFS_NEXT_ ## op args; \
-       } \
-} while (0)
+               have_insert = (strstr(name, insert+1) != NULL);
+               TALLOC_FREE(insert);
+               if (have_insert) {
+                       goto no_snapshot;
+               }
+       }
 
+       if (pstripped != NULL) {
+               stripped = talloc_array(mem_ctx, char, dst_len+1);
+               if (stripped == NULL) {
+                       errno = ENOMEM;
+                       return false;
+               }
+               if (p > name) {
+                       memcpy(stripped, name, p-name);
+               }
+               if (rest_len > 0) {
+                       memcpy(stripped + (p-name), q, rest_len);
+               }
+               stripped[dst_len] = '\0';
+               *pstripped = stripped;
+       }
+       *ptimestamp = timestamp;
+       return true;
+no_snapshot:
+       *ptimestamp = 0;
+       return true;
+}
 
-/*
-  find the mount point of a filesystem
- */
-static char *find_mount_point(TALLOC_CTX *mem_ctx, vfs_handle_struct *handle)
+static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
+                                          vfs_handle_struct *handle)
 {
        char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
        dev_t dev;
@@ -358,164 +281,152 @@ static char *find_mount_point(TALLOC_CTX *mem_ctx, 
vfs_handle_struct *handle)
                }
        }
 
-       return path;    
+       return path;
 }
 
-/*
-  work out the location of the snapshot for this share
- */
-static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx, 
vfs_handle_struct *handle)
+static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
+                                 struct vfs_handle_struct *handle,
+                                 const char *name, time_t timestamp)
 {
-       const char *snapdir;
-       char *mount_point;
-       const char *ret;
-
-       snapdir = lp_parm_const_string(SNUM(handle->conn), "shadow", "snapdir", 
NULL);
-       if (snapdir == NULL) {
-               return NULL;
-       }
-       /* if its an absolute path, we're done */
-       if (*snapdir == '/') {
-               return snapdir;
+       struct smb_filename converted_fname;
+       char *result = NULL;
+       size_t *slashes = NULL;
+       unsigned num_slashes;
+       char *path = NULL;
+       size_t pathlen;
+       char *insert = NULL;
+       char *converted = NULL;
+       size_t insertlen;
+       int i, saved_errno;
+       size_t min_offset;
+
+       path = talloc_asprintf(mem_ctx, "%s/%s", handle->conn->connectpath,
+                              name);
+       if (path == NULL) {
+               errno = ENOMEM;
+               goto fail;
        }
+       pathlen = talloc_get_size(path)-1;
 
-       /* other its relative to the filesystem mount point */
-       mount_point = find_mount_point(mem_ctx, handle);
-       if (mount_point == NULL) {
-               return NULL;
-       }
-
-       ret = talloc_asprintf(mem_ctx, "%s/%s", mount_point, snapdir);
-       talloc_free(mount_point);
-       return ret;
-}
-
-/*
-  work out the location of the base directory for snapshots of this share
- */
-static const char *shadow_copy2_find_basedir(TALLOC_CTX *mem_ctx, 
vfs_handle_struct *handle)
-{
-       const char *basedir = lp_parm_const_string(SNUM(handle->conn), 
"shadow", "basedir", NULL);
+       DEBUG(10, ("converting %s\n", path));
 
-       /* other its the filesystem mount point */
-       if (basedir == NULL) {
-               basedir = find_mount_point(mem_ctx, handle);
+       if (!shadow_copy2_find_slashes(talloc_tos(), path,
+                                      &slashes, &num_slashes)) {
+               goto fail;
        }
-


-- 
Samba Shared Repository

Reply via email to