X-Git-Url: http://git.samba.org/?p=samba.git;a=blobdiff_plain;f=source3%2Fmodules%2Fvfs_recycle.c;h=607ce3a4b83ee1ecc90ee98fd1bc4ba7da97fa1f;hp=45c661edb7a48c8672c5fba531506b4a59f62c66;hb=0e771263eed0b9bc364ce523765ea17dd1192841;hpb=bc2a3748e9caa8f60f7c2387e7eecd7fb3fae899 diff --git a/source3/modules/vfs_recycle.c b/source3/modules/vfs_recycle.c index 45c661edb7a..607ce3a4b83 100644 --- a/source3/modules/vfs_recycle.c +++ b/source3/modules/vfs_recycle.c @@ -10,7 +10,7 @@ * * 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 + * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, @@ -19,11 +19,12 @@ * 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. + * along with this program; if not, see . */ #include "includes.h" +#include "system/filesys.h" +#include "../librpc/gen_ndr/ndr_netlogon.h" #define ALLOC_CHECK(ptr, label) do { if ((ptr) == NULL) { DEBUG(0, ("recycle.bin: out of memory!\n")); errno = ENOMEM; goto label; } } while(0) @@ -32,133 +33,185 @@ static int vfs_recycle_debug_level = DBGC_VFS; #undef DBGC_CLASS #define DBGC_CLASS vfs_recycle_debug_level -static int recycle_connect(vfs_handle_struct *handle, connection_struct *conn, const char *service, const char *user); -static void recycle_disconnect(vfs_handle_struct *handle, connection_struct *conn); -static int recycle_unlink(vfs_handle_struct *handle, connection_struct *conn, const char *name); +static int recycle_connect(vfs_handle_struct *handle, const char *service, const char *user); +static void recycle_disconnect(vfs_handle_struct *handle); +static int recycle_unlink(vfs_handle_struct *handle, + const struct smb_filename *smb_fname); -static vfs_op_tuple recycle_ops[] = { - - /* Disk operations */ - {SMB_VFS_OP(recycle_connect), SMB_VFS_OP_CONNECT, SMB_VFS_LAYER_TRANSPARENT}, - {SMB_VFS_OP(recycle_disconnect), SMB_VFS_OP_DISCONNECT, SMB_VFS_LAYER_TRANSPARENT}, - - /* File operations */ - {SMB_VFS_OP(recycle_unlink), SMB_VFS_OP_UNLINK, SMB_VFS_LAYER_TRANSPARENT}, +static int recycle_connect(vfs_handle_struct *handle, const char *service, const char *user) +{ + int ret = SMB_VFS_NEXT_CONNECT(handle, service, user); - {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP} -}; + if (ret < 0) { + return ret; + } -static int recycle_connect(vfs_handle_struct *handle, connection_struct *conn, const char *service, const char *user) -{ DEBUG(10,("recycle_connect() connect to service[%s] as user[%s].\n", service,user)); - return SMB_VFS_NEXT_CONNECT(handle, conn, service, user); + return 0; } -static void recycle_disconnect(vfs_handle_struct *handle, connection_struct *conn) +static void recycle_disconnect(vfs_handle_struct *handle) { DEBUG(10,("recycle_disconnect() connect to service[%s].\n", - lp_servicename(SNUM(conn)))); + lp_servicename(SNUM(handle->conn)))); - SMB_VFS_NEXT_DISCONNECT(handle, conn); + SMB_VFS_NEXT_DISCONNECT(handle); } static const char *recycle_repository(vfs_handle_struct *handle) { const char *tmp_str = NULL; - tmp_str = lp_parm_const_string(SNUM(handle->conn), "recycle", "repository",".recycle"); DEBUG(10, ("recycle: repository = %s\n", tmp_str)); - + return tmp_str; } -static BOOL recycle_keep_dir_tree(vfs_handle_struct *handle) +static bool recycle_keep_dir_tree(vfs_handle_struct *handle) { - BOOL ret; - + bool ret; + ret = lp_parm_bool(SNUM(handle->conn), "recycle", "keeptree", False); DEBUG(10, ("recycle_bin: keeptree = %s\n", ret?"True":"False")); - + return ret; } -static BOOL recycle_versions(vfs_handle_struct *handle) +static bool recycle_versions(vfs_handle_struct *handle) { - BOOL ret; + bool ret; ret = lp_parm_bool(SNUM(handle->conn), "recycle", "versions", False); DEBUG(10, ("recycle: versions = %s\n", ret?"True":"False")); - + return ret; } -static BOOL recycle_touch(vfs_handle_struct *handle) +static bool recycle_touch(vfs_handle_struct *handle) { - BOOL ret; + bool ret; ret = lp_parm_bool(SNUM(handle->conn), "recycle", "touch", False); DEBUG(10, ("recycle: touch = %s\n", ret?"True":"False")); - + + return ret; +} + +static bool recycle_touch_mtime(vfs_handle_struct *handle) +{ + bool ret; + + ret = lp_parm_bool(SNUM(handle->conn), "recycle", "touch_mtime", False); + + DEBUG(10, ("recycle: touch_mtime = %s\n", ret?"True":"False")); + return ret; } static const char **recycle_exclude(vfs_handle_struct *handle) { const char **tmp_lp; - + tmp_lp = lp_parm_string_list(SNUM(handle->conn), "recycle", "exclude", NULL); DEBUG(10, ("recycle: exclude = %s ...\n", tmp_lp?*tmp_lp:"")); - + return tmp_lp; } static const char **recycle_exclude_dir(vfs_handle_struct *handle) { const char **tmp_lp; - + tmp_lp = lp_parm_string_list(SNUM(handle->conn), "recycle", "exclude_dir", NULL); DEBUG(10, ("recycle: exclude_dir = %s ...\n", tmp_lp?*tmp_lp:"")); - + return tmp_lp; } static const char **recycle_noversions(vfs_handle_struct *handle) { const char **tmp_lp; - + tmp_lp = lp_parm_string_list(SNUM(handle->conn), "recycle", "noversions", NULL); DEBUG(10, ("recycle: noversions = %s\n", tmp_lp?*tmp_lp:"")); - + return tmp_lp; } -static int recycle_maxsize(vfs_handle_struct *handle) +static SMB_OFF_T recycle_maxsize(vfs_handle_struct *handle) { - int maxsize; - - maxsize = lp_parm_int(SNUM(handle->conn), "recycle", "maxsize", -1); + SMB_OFF_T maxsize; + + maxsize = conv_str_size(lp_parm_const_string(SNUM(handle->conn), + "recycle", "maxsize", NULL)); + + DEBUG(10, ("recycle: maxsize = %lu\n", (long unsigned int)maxsize)); - DEBUG(10, ("recycle: maxsize = %d\n", maxsize)); - return maxsize; } -static BOOL recycle_directory_exist(vfs_handle_struct *handle, const char *dname) +static SMB_OFF_T recycle_minsize(vfs_handle_struct *handle) +{ + SMB_OFF_T minsize; + + minsize = conv_str_size(lp_parm_const_string(SNUM(handle->conn), + "recycle", "minsize", NULL)); + + DEBUG(10, ("recycle: minsize = %lu\n", (long unsigned int)minsize)); + + return minsize; +} + +static mode_t recycle_directory_mode(vfs_handle_struct *handle) +{ + int dirmode; + const char *buff; + + buff = lp_parm_const_string(SNUM(handle->conn), "recycle", "directory_mode", NULL); + + if (buff != NULL ) { + sscanf(buff, "%o", &dirmode); + } else { + dirmode=S_IRUSR | S_IWUSR | S_IXUSR; + } + + DEBUG(10, ("recycle: directory_mode = %o\n", dirmode)); + return (mode_t)dirmode; +} + +static mode_t recycle_subdir_mode(vfs_handle_struct *handle) +{ + int dirmode; + const char *buff; + + buff = lp_parm_const_string(SNUM(handle->conn), "recycle", "subdir_mode", NULL); + + if (buff != NULL ) { + sscanf(buff, "%o", &dirmode); + } else { + dirmode=recycle_directory_mode(handle); + } + + DEBUG(10, ("recycle: subdir_mode = %o\n", dirmode)); + return (mode_t)dirmode; +} + +static bool recycle_directory_exist(vfs_handle_struct *handle, const char *dname) { SMB_STRUCT_STAT st; - if (SMB_VFS_NEXT_STAT(handle, handle->conn, dname, &st) == 0) { - if (S_ISDIR(st.st_mode)) { + if (vfs_stat_smb_fname(handle->conn, dname, &st) == 0) { + if (S_ISDIR(st.st_ex_mode)) { return True; } } @@ -166,17 +219,26 @@ static BOOL recycle_directory_exist(vfs_handle_struct *handle, const char *dname return False; } -static BOOL recycle_file_exist(vfs_handle_struct *handle, const char *fname) +static bool recycle_file_exist(vfs_handle_struct *handle, + const struct smb_filename *smb_fname) { - SMB_STRUCT_STAT st; + struct smb_filename *smb_fname_tmp = NULL; + NTSTATUS status; + bool ret = false; - if (SMB_VFS_NEXT_STAT(handle, handle->conn, fname, &st) == 0) { - if (S_ISREG(st.st_mode)) { - return True; + status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp); + if (!NT_STATUS_IS_OK(status)) { + return false; + } + + if (SMB_VFS_STAT(handle->conn, smb_fname_tmp) == 0) { + if (S_ISREG(smb_fname_tmp->st.st_ex_mode)) { + ret = true; } } - return False; + TALLOC_FREE(smb_fname_tmp); + return ret; } /** @@ -185,16 +247,30 @@ static BOOL recycle_file_exist(vfs_handle_struct *handle, const char *fname) * @param fname file name * @return size in bytes **/ -static SMB_OFF_T recycle_get_file_size(vfs_handle_struct *handle, const char *fname) +static SMB_OFF_T recycle_get_file_size(vfs_handle_struct *handle, + const struct smb_filename *smb_fname) { - SMB_STRUCT_STAT st; + struct smb_filename *smb_fname_tmp = NULL; + NTSTATUS status; + SMB_OFF_T size; + + status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp); + if (!NT_STATUS_IS_OK(status)) { + size = (SMB_OFF_T)0; + goto out; + } - if (SMB_VFS_NEXT_STAT(handle, handle->conn, fname, &st) != 0) { - DEBUG(0,("recycle: stat for %s returned %s\n", fname, strerror(errno))); - return (SMB_OFF_T)0; + if (SMB_VFS_STAT(handle->conn, smb_fname_tmp) != 0) { + DEBUG(0,("recycle: stat for %s returned %s\n", + smb_fname_str_dbg(smb_fname_tmp), strerror(errno))); + size = (SMB_OFF_T)0; + goto out; } - return(st.st_size); + size = smb_fname_tmp->st.st_ex_size; + out: + TALLOC_FREE(smb_fname_tmp); + return size; } /** @@ -203,41 +279,48 @@ static SMB_OFF_T recycle_get_file_size(vfs_handle_struct *handle, const char *fn * @param dname Directory tree to be created * @return Returns True for success **/ -static BOOL recycle_create_dir(vfs_handle_struct *handle, const char *dname) +static bool recycle_create_dir(vfs_handle_struct *handle, const char *dname) { - int len; + size_t len; mode_t mode; char *new_dir = NULL; char *tmp_str = NULL; char *token; char *tok_str; - BOOL ret = False; + bool ret = False; + char *saveptr; - mode = S_IREAD | S_IWRITE | S_IEXEC; + mode = recycle_directory_mode(handle); - tmp_str = strdup(dname); + tmp_str = SMB_STRDUP(dname); ALLOC_CHECK(tmp_str, done); tok_str = tmp_str; - len = strlen(dname); - new_dir = (char *)malloc(len + 1); + len = strlen(dname)+1; + new_dir = (char *)SMB_MALLOC(len + 1); ALLOC_CHECK(new_dir, done); *new_dir = '\0'; + if (dname[0] == '/') { + /* Absolute path. */ + safe_strcat(new_dir,"/",len); + } /* Create directory tree if neccessary */ - for(token = strtok(tok_str, "/"); token; token = strtok(NULL, "/")) { + for(token = strtok_r(tok_str, "/", &saveptr); token; + token = strtok_r(NULL, "/", &saveptr)) { safe_strcat(new_dir, token, len); if (recycle_directory_exist(handle, new_dir)) DEBUG(10, ("recycle: dir %s already exists\n", new_dir)); else { DEBUG(5, ("recycle: creating new dir %s\n", new_dir)); - if (SMB_VFS_NEXT_MKDIR(handle, handle->conn, new_dir, mode) != 0) { + if (SMB_VFS_NEXT_MKDIR(handle, new_dir, mode) != 0) { DEBUG(1,("recycle: mkdir failed for %s with error: %s\n", new_dir, strerror(errno))); ret = False; goto done; } } safe_strcat(new_dir, "/", len); + mode = recycle_subdir_mode(handle); } ret = True; @@ -248,23 +331,48 @@ done: } /** - * Check if needle is contained exactly in haystack - * @param haystack list of parameters separated by delimimiter character - * @param needle string to be matched exactly to haystack - * @return True if found + * Check if any of the components of "exclude_list" are contained in path. + * Return True if found **/ -static BOOL checkparam(const char **haystack_list, const char *needle) + +static bool matchdirparam(const char **dir_exclude_list, char *path) { - int i; + char *startp = NULL, *endp = NULL; - if (haystack_list == NULL || haystack_list[0] == NULL || - *haystack_list[0] == '\0' || needle == NULL || *needle == '\0') { + if (dir_exclude_list == NULL || dir_exclude_list[0] == NULL || + *dir_exclude_list[0] == '\0' || path == NULL || *path == '\0') { return False; } - for(i=0; haystack_list[i] ; i++) { - if(strequal(haystack_list[i], needle)) { - return True; + /* + * Walk the components of path, looking for matches with the + * exclude list on each component. + */ + + for (startp = path; startp; startp = endp) { + int i; + + while (*startp == '/') { + startp++; + } + endp = strchr(startp, '/'); + if (endp) { + *endp = '\0'; + } + + for(i=0; dir_exclude_list[i] ; i++) { + if(unix_wild_match(dir_exclude_list[i], startp)) { + /* Repair path. */ + if (endp) { + *endp = '/'; + } + return True; + } + } + + /* Repair path. */ + if (endp) { + *endp = '/'; } } @@ -277,7 +385,7 @@ static BOOL checkparam(const char **haystack_list, const char *needle) * @param needle string to be matched exectly to haystack including pattern matching * @return True if found **/ -static BOOL matchparam(const char **haystack_list, const char *needle) +static bool matchparam(const char **haystack_list, const char *needle) { int i; @@ -287,7 +395,7 @@ static BOOL matchparam(const char **haystack_list, const char *needle) } for(i=0; haystack_list[i] ; i++) { - if(!unix_wild_match((char *)haystack_list[i], (char *)needle)) { + if(unix_wild_match(haystack_list[i], needle)) { return True; } } @@ -296,133 +404,174 @@ static BOOL matchparam(const char **haystack_list, const char *needle) } /** - * Touch access date + * Touch access or modify date **/ -static void recycle_do_touch(vfs_handle_struct *handle, const char *fname) +static void recycle_do_touch(vfs_handle_struct *handle, + const struct smb_filename *smb_fname, + bool touch_mtime) { - SMB_STRUCT_STAT st; - struct utimbuf tb; - time_t currtime; - - if (SMB_VFS_NEXT_STAT(handle, handle->conn, fname, &st) != 0) { - DEBUG(0,("recycle: stat for %s returned %s\n", fname, strerror(errno))); + struct smb_filename *smb_fname_tmp = NULL; + struct smb_file_time ft; + NTSTATUS status; + int ret, err; + + ZERO_STRUCT(ft); + + status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp); + if (!NT_STATUS_IS_OK(status)) { return; } - currtime = time(&currtime); - tb.actime = currtime; - tb.modtime = st.st_mtime; - if (SMB_VFS_NEXT_UTIME(handle, handle->conn, fname, &tb) == -1 ) { - DEBUG(0, ("recycle: touching %s failed, reason = %s\n", fname, strerror(errno))); + if (SMB_VFS_STAT(handle->conn, smb_fname_tmp) != 0) { + DEBUG(0,("recycle: stat for %s returned %s\n", + smb_fname_str_dbg(smb_fname_tmp), strerror(errno))); + goto out; } + /* atime */ + ft.atime = timespec_current(); + /* mtime */ + ft.mtime = touch_mtime ? ft.atime : smb_fname_tmp->st.st_ex_mtime; + + become_root(); + ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, &ft); + err = errno; + unbecome_root(); + if (ret == -1 ) { + DEBUG(0, ("recycle: touching %s failed, reason = %s\n", + smb_fname_str_dbg(smb_fname_tmp), strerror(err))); + } + out: + TALLOC_FREE(smb_fname_tmp); } /** * Check if file should be recycled **/ -static int recycle_unlink(vfs_handle_struct *handle, connection_struct *conn, const char *file_name) +static int recycle_unlink(vfs_handle_struct *handle, + const struct smb_filename *smb_fname) { + connection_struct *conn = handle->conn; char *path_name = NULL; char *temp_name = NULL; char *final_name = NULL; + struct smb_filename *smb_fname_final = NULL; const char *base; char *repository = NULL; int i = 1; - int maxsize; + SMB_OFF_T maxsize, minsize; SMB_OFF_T file_size; /* space_avail; */ - BOOL exist; + bool exist; + NTSTATUS status; int rc = -1; - repository = alloc_sub_conn(conn, (char *)recycle_repository(handle)); + repository = talloc_sub_advanced(NULL, lp_servicename(SNUM(conn)), + conn->session_info->unix_name, + conn->connectpath, + conn->session_info->utok.gid, + conn->session_info->sanitized_username, + conn->session_info->info3->base.domain.string, + recycle_repository(handle)); ALLOC_CHECK(repository, done); /* shouldn't we allow absolute path names here? --metze */ - trim_string(repository, "/", "/"); - + /* Yes :-). JRA. */ + trim_char(repository, '\0', '/'); + if(!repository || *(repository) == '\0') { - DEBUG(3, ("recycle: repository path not set, purging %s...\n", file_name)); - rc = SMB_VFS_NEXT_UNLINK(handle, conn, file_name); + DEBUG(3, ("recycle: repository path not set, purging %s...\n", + smb_fname_str_dbg(smb_fname))); + rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname); goto done; } /* we don't recycle the recycle bin... */ - if (strncmp(file_name, repository, strlen(repository)) == 0) { + if (strncmp(smb_fname->base_name, repository, + strlen(repository)) == 0) { DEBUG(3, ("recycle: File is within recycling bin, unlinking ...\n")); - rc = SMB_VFS_NEXT_UNLINK(handle, conn, file_name); + rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname); goto done; } - file_size = recycle_get_file_size(handle, file_name); + file_size = recycle_get_file_size(handle, smb_fname); /* it is wrong to purge filenames only because they are empty imho * --- simo * if(fsize == 0) { DEBUG(3, ("recycle: File %s is empty, purging...\n", file_name)); - rc = SMB_VFS_NEXT_UNLINK(handle,conn,file_name); + rc = SMB_VFS_NEXT_UNLINK(handle,file_name); goto done; } */ - /* FIXME: this is wrong, we should check the hole size of the recycle bin is + /* FIXME: this is wrong, we should check the whole size of the recycle bin is * not greater then maxsize, not the size of the single file, also it is better * to remove older files */ maxsize = recycle_maxsize(handle); if(maxsize > 0 && file_size > maxsize) { - DEBUG(3, ("recycle: File %s exceeds maximum recycle size, purging... \n", file_name)); - rc = SMB_VFS_NEXT_UNLINK(handle, conn, file_name); + DEBUG(3, ("recycle: File %s exceeds maximum recycle size, " + "purging... \n", smb_fname_str_dbg(smb_fname))); + rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname); + goto done; + } + minsize = recycle_minsize(handle); + if(minsize > 0 && file_size < minsize) { + DEBUG(3, ("recycle: File %s lowers minimum recycle size, " + "purging... \n", smb_fname_str_dbg(smb_fname))); + rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname); goto done; } /* FIXME: this is wrong: moving files with rename does not change the disk space * allocation * - space_avail = SMB_VFS_NEXT_DISK_FREE(handle, conn, ".", True, &bsize, &dfree, &dsize) * 1024L; + space_avail = SMB_VFS_NEXT_DISK_FREE(handle, ".", True, &bsize, &dfree, &dsize) * 1024L; DEBUG(5, ("space_avail = %Lu, file_size = %Lu\n", space_avail, file_size)); if(space_avail < file_size) { DEBUG(3, ("recycle: Not enough diskspace, purging file %s\n", file_name)); - rc = SMB_VFS_NEXT_UNLINK(handle, conn, file_name); + rc = SMB_VFS_NEXT_UNLINK(handle, file_name); goto done; } */ /* extract filename and path */ - base = strrchr(file_name, '/'); + base = strrchr(smb_fname->base_name, '/'); if (base == NULL) { - base = file_name; - path_name = strdup("/"); + base = smb_fname->base_name; + path_name = SMB_STRDUP("/"); ALLOC_CHECK(path_name, done); } else { - path_name = strdup(file_name); + path_name = SMB_STRDUP(smb_fname->base_name); ALLOC_CHECK(path_name, done); - path_name[base - file_name] = '\0'; + path_name[base - smb_fname->base_name] = '\0'; base++; } - DEBUG(10, ("recycle: fname = %s\n", file_name)); /* original filename with path */ - DEBUG(10, ("recycle: fpath = %s\n", path_name)); /* original path */ - DEBUG(10, ("recycle: base = %s\n", base)); /* filename without path */ + /* original filename with path */ + DEBUG(10, ("recycle: fname = %s\n", smb_fname_str_dbg(smb_fname))); + /* original path */ + DEBUG(10, ("recycle: fpath = %s\n", path_name)); + /* filename without path */ + DEBUG(10, ("recycle: base = %s\n", base)); if (matchparam(recycle_exclude(handle), base)) { DEBUG(3, ("recycle: file %s is excluded \n", base)); - rc = SMB_VFS_NEXT_UNLINK(handle, conn, file_name); + rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname); goto done; } - /* FIXME: this check will fail if we have more than one level of directories, - * we shoud check for every level 1, 1/2, 1/2/3, 1/2/3/4 .... - * ---simo - */ - if (checkparam(recycle_exclude_dir(handle), path_name)) { + if (matchdirparam(recycle_exclude_dir(handle), path_name)) { DEBUG(3, ("recycle: directory %s is excluded \n", path_name)); - rc = SMB_VFS_NEXT_UNLINK(handle, conn, file_name); + rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname); goto done; } if (recycle_keep_dir_tree(handle) == True) { - asprintf(&temp_name, "%s/%s", repository, path_name); + if (asprintf(&temp_name, "%s/%s", repository, path_name) == -1) { + ALLOC_CHECK(temp_name, done); + } } else { - temp_name = strdup(repository); + temp_name = SMB_STRDUP(repository); } ALLOC_CHECK(temp_name, done); @@ -432,21 +581,38 @@ static int recycle_unlink(vfs_handle_struct *handle, connection_struct *conn, co } else { DEBUG(10, ("recycle: Creating directory %s\n", temp_name)); if (recycle_create_dir(handle, temp_name) == False) { - DEBUG(3, ("recycle: Could not create directory, purging %s...\n", file_name)); - rc = SMB_VFS_NEXT_UNLINK(handle, conn, file_name); + DEBUG(3, ("recycle: Could not create directory, " + "purging %s...\n", + smb_fname_str_dbg(smb_fname))); + rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname); goto done; } } - asprintf(&final_name, "%s/%s", temp_name, base); - ALLOC_CHECK(final_name, done); - DEBUG(10, ("recycle: recycled file name: %s\n", final_name)); /* new filename with path */ + if (asprintf(&final_name, "%s/%s", temp_name, base) == -1) { + ALLOC_CHECK(final_name, done); + } + + /* Create smb_fname with final base name and orig stream name. */ + status = create_synthetic_smb_fname(talloc_tos(), final_name, + smb_fname->stream_name, NULL, + &smb_fname_final); + if (!NT_STATUS_IS_OK(status)) { + rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname); + goto done; + } + + /* new filename with path */ + DEBUG(10, ("recycle: recycled file name: %s\n", + smb_fname_str_dbg(smb_fname_final))); /* check if we should delete file from recycle bin */ - if (recycle_file_exist(handle, final_name)) { + if (recycle_file_exist(handle, smb_fname_final)) { if (recycle_versions(handle) == False || matchparam(recycle_noversions(handle), base) == True) { - DEBUG(3, ("recycle: Removing old file %s from recycle bin\n", final_name)); - if (SMB_VFS_NEXT_UNLINK(handle, conn, final_name) != 0) { + DEBUG(3, ("recycle: Removing old file %s from recycle " + "bin\n", smb_fname_str_dbg(smb_fname_final))); + if (SMB_VFS_NEXT_UNLINK(handle, + smb_fname_final) != 0) { DEBUG(1, ("recycle: Error deleting old file: %s\n", strerror(errno))); } } @@ -454,37 +620,61 @@ static int recycle_unlink(vfs_handle_struct *handle, connection_struct *conn, co /* rename file we move to recycle bin */ i = 1; - while (recycle_file_exist(handle, final_name)) { - snprintf(final_name, PATH_MAX -1, "%s/Copy #%d of %s", temp_name, i++, base); + while (recycle_file_exist(handle, smb_fname_final)) { + SAFE_FREE(final_name); + if (asprintf(&final_name, "%s/Copy #%d of %s", temp_name, i++, base) == -1) { + ALLOC_CHECK(final_name, done); + } + TALLOC_FREE(smb_fname_final->base_name); + smb_fname_final->base_name = talloc_strdup(smb_fname_final, + final_name); + if (smb_fname_final->base_name == NULL) { + rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname); + goto done; + } } - DEBUG(10, ("recycle: Moving %s to %s\n", file_name, final_name)); - rc = SMB_VFS_NEXT_RENAME(handle, conn, file_name, final_name); + DEBUG(10, ("recycle: Moving %s to %s\n", smb_fname_str_dbg(smb_fname), + smb_fname_str_dbg(smb_fname_final))); + rc = SMB_VFS_NEXT_RENAME(handle, smb_fname, smb_fname_final); if (rc != 0) { - DEBUG(3, ("recycle: Move error %d (%s), purging file %s (%s)\n", errno, strerror(errno), file_name, final_name)); - rc = SMB_VFS_NEXT_UNLINK(handle, conn, file_name); + DEBUG(3, ("recycle: Move error %d (%s), purging file %s " + "(%s)\n", errno, strerror(errno), + smb_fname_str_dbg(smb_fname), + smb_fname_str_dbg(smb_fname_final))); + rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname); goto done; } /* touch access date of moved file */ - if (recycle_touch(handle) == True ) - recycle_do_touch(handle, final_name); + if (recycle_touch(handle) == True || recycle_touch_mtime(handle)) + recycle_do_touch(handle, smb_fname_final, + recycle_touch_mtime(handle)); done: SAFE_FREE(path_name); SAFE_FREE(temp_name); SAFE_FREE(final_name); - SAFE_FREE(repository); + TALLOC_FREE(smb_fname_final); + TALLOC_FREE(repository); return rc; } +static struct vfs_fn_pointers vfs_recycle_fns = { + .connect_fn = recycle_connect, + .disconnect = recycle_disconnect, + .unlink = recycle_unlink +}; + +NTSTATUS vfs_recycle_init(void); NTSTATUS vfs_recycle_init(void) { - NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "recycle", recycle_ops); + NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "recycle", + &vfs_recycle_fns); - if (NT_STATUS_IS_ERR(ret)) + if (!NT_STATUS_IS_OK(ret)) return ret; - + vfs_recycle_debug_level = debug_add_class("recycle"); if (vfs_recycle_debug_level == -1) { vfs_recycle_debug_level = DBGC_VFS; @@ -492,6 +682,6 @@ NTSTATUS vfs_recycle_init(void) } else { DEBUG(10, ("vfs_recycle: Debug class number of 'recycle': %d\n", vfs_recycle_debug_level)); } - + return ret; }