From: Volker Lendecke Date: Sat, 17 Sep 2022 17:13:27 +0000 (-0700) Subject: lib: Move extract_snapshot_token() to util_path.c X-Git-Tag: talloc-2.4.0~929 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bfe07fda67f1dac932f57c984b0cdac1df8bc11c;p=thirdparty%2Fsamba.git lib: Move extract_snapshot_token() to util_path.c Make it available to replace clistr_is_previous_version_path() in libsmb/ Signed-off-by: Volker Lendecke Reviewed-by: Jeremy Allison --- diff --git a/source3/include/smb.h b/source3/include/smb.h index 6363bdc0c93..cfaf922b461 100644 --- a/source3/include/smb.h +++ b/source3/include/smb.h @@ -454,18 +454,6 @@ Offset Data length. #define NOTIFY_ACTION_REMOVED_STREAM 7 #define NOTIFY_ACTION_MODIFIED_STREAM 8 -/* - * Timestamp format used in "previous versions": - * This is the windows-level format of the @GMT- token. - * It is a fixed format not to be confused with the - * format for the POSIX-Level token of the shadow_copy2 - * VFS module that can be configured via the "shadow:format" - * configuration option but defaults to the same format. - * See the shadow_copy2 module. - */ -#define GMT_NAME_LEN 24 /* length of a @GMT- name */ -#define GMT_FORMAT "@GMT-%Y.%m.%d-%H.%M.%S" - /* where to find the base of the SMB packet proper */ #define smb_base(buf) (((const char *)(buf))+4) diff --git a/source3/lib/util_path.c b/source3/lib/util_path.c index 3591589cb8e..6d2161caf84 100644 --- a/source3/lib/util_path.c +++ b/source3/lib/util_path.c @@ -24,6 +24,7 @@ #include "replace.h" #include #include "lib/util/samba_util.h" +#include "lib/util/debug.h" #include "lib/util_path.h" struct loadparm_substitution; @@ -204,3 +205,74 @@ char *canonicalize_absolute_path(TALLOC_CTX *ctx, const char *pathname_in) *p++ = '\0'; return pathname; } + +static bool find_snapshot_token( + const char *filename, + const char **_start, + const char **_next_component, + NTTIME *twrp) +{ + const char *start = NULL; + const char *end = NULL; + struct tm tm; + time_t t; + + start = strstr_m(filename, "@GMT-"); + + if (start == NULL) { + return false; + } + + if ((start > filename) && (start[-1] != '/')) { + /* the GMT-token does not start a path-component */ + return false; + } + + end = strptime(start, GMT_FORMAT, &tm); + if (end == NULL) { + /* Not a valid timestring. */ + return false; + } + + if ((end[0] != '\0') && (end[0] != '/')) { + /* + * It is not a complete path component, i.e. the path + * component continues after the gmt-token. + */ + return false; + } + + tm.tm_isdst = -1; + t = timegm(&tm); + unix_to_nt_time(twrp, t); + + DBG_DEBUG("Extracted @GMT-Timestamp %s\n", + nt_time_string(talloc_tos(), *twrp)); + + *_start = start; + + if (end[0] == '/') { + end += 1; + } + *_next_component = end; + + return true; +} + +bool extract_snapshot_token(char *fname, NTTIME *twrp) +{ + const char *start = NULL; + const char *next = NULL; + size_t remaining; + bool found; + + found = find_snapshot_token(fname, &start, &next, twrp); + if (!found) { + return false; + } + + remaining = strlen(next); + memmove(discard_const_p(char, start), next, remaining+1); + + return true; +} diff --git a/source3/lib/util_path.h b/source3/lib/util_path.h index 3e7d04de550..3251eb1e43a 100644 --- a/source3/lib/util_path.h +++ b/source3/lib/util_path.h @@ -26,10 +26,24 @@ #include "replace.h" #include +#include "lib/util/time.h" + +/* + * Timestamp format used in "previous versions": + * This is the windows-level format of the @GMT- token. + * It is a fixed format not to be confused with the + * format for the POSIX-Level token of the shadow_copy2 + * VFS module that can be configured via the "shadow:format" + * configuration option but defaults to the same format. + * See the shadow_copy2 module. + */ +#define GMT_NAME_LEN 24 /* length of a @GMT- name */ +#define GMT_FORMAT "@GMT-%Y.%m.%d-%H.%M.%S" char *lock_path(TALLOC_CTX *mem_ctx, const char *name); char *state_path(TALLOC_CTX *mem_ctx, const char *name); char *cache_path(TALLOC_CTX *mem_ctx, const char *name); char *canonicalize_absolute_path(TALLOC_CTX *ctx, const char *abs_path); +bool extract_snapshot_token(char *fname, NTTIME *twrp); #endif diff --git a/source3/smbd/filename.c b/source3/smbd/filename.c index 0be8e320ffa..79bb7dffe99 100644 --- a/source3/smbd/filename.c +++ b/source3/smbd/filename.c @@ -87,77 +87,6 @@ static bool mangled_equal(const char *name1, return strequal(name1, mname); } -static bool find_snapshot_token( - const char *filename, - const char **_start, - const char **_next_component, - NTTIME *twrp) -{ - const char *start = NULL; - const char *end = NULL; - struct tm tm; - time_t t; - - start = strstr_m(filename, "@GMT-"); - - if (start == NULL) { - return false; - } - - if ((start > filename) && (start[-1] != '/')) { - /* the GMT-token does not start a path-component */ - return false; - } - - end = strptime(start, GMT_FORMAT, &tm); - if (end == NULL) { - /* Not a valid timestring. */ - return false; - } - - if ((end[0] != '\0') && (end[0] != '/')) { - /* - * It is not a complete path component, i.e. the path - * component continues after the gmt-token. - */ - return false; - } - - tm.tm_isdst = -1; - t = timegm(&tm); - unix_to_nt_time(twrp, t); - - DBG_DEBUG("Extracted @GMT-Timestamp %s\n", - nt_time_string(talloc_tos(), *twrp)); - - *_start = start; - - if (end[0] == '/') { - end += 1; - } - *_next_component = end; - - return true; -} - -bool extract_snapshot_token(char *fname, NTTIME *twrp) -{ - const char *start = NULL; - const char *next = NULL; - size_t remaining; - bool found; - - found = find_snapshot_token(fname, &start, &next, twrp); - if (!found) { - return false; - } - - remaining = strlen(next); - memmove(discard_const_p(char, start), next, remaining+1); - - return true; -} - /* * Strip a valid @GMT-token from any incoming filename path, * adding any NTTIME encoded in the pathname into the diff --git a/source3/smbd/proto.h b/source3/smbd/proto.h index 89b3d224a8b..743375a7d90 100644 --- a/source3/smbd/proto.h +++ b/source3/smbd/proto.h @@ -350,7 +350,6 @@ NTSTATUS sync_file(connection_struct *conn, files_struct *fsp, bool write_throug uint32_t ucf_flags_from_smb_request(struct smb_request *req); uint32_t filename_create_ucf_flags(struct smb_request *req, uint32_t create_disposition); -bool extract_snapshot_token(char *fname, NTTIME *twrp); NTSTATUS canonicalize_snapshot_path(struct smb_filename *smb_fname, uint32_t ucf_flags, NTTIME twrp);