r16945: Sync trunk -> 3.0 for 3.0.24 code. Still need
[ira/wip.git] / source3 / modules / vfs_recycle.c
index 5ff7931c585fe8893733f5a23c02fb159bed052e..cb5eaffa54d21b53aaa4e7539f6e6aefa29808ed 100644 (file)
@@ -32,9 +32,9 @@ 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 char *name);
 
 static vfs_op_tuple recycle_ops[] = {
 
@@ -48,20 +48,20 @@ static vfs_op_tuple recycle_ops[] = {
        {SMB_VFS_OP(NULL),              SMB_VFS_OP_NOOP,        SMB_VFS_LAYER_NOOP}
 };
 
-static int recycle_connect(vfs_handle_struct *handle, connection_struct *conn, const char *service, const char *user)
+static int recycle_connect(vfs_handle_struct *handle, 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 SMB_VFS_NEXT_CONNECT(handle, service, user);
 }
 
-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)
@@ -109,6 +109,17 @@ static BOOL recycle_touch(vfs_handle_struct *handle)
        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;
@@ -153,11 +164,45 @@ static int recycle_maxsize(vfs_handle_struct *handle)
        return maxsize;
 }
 
+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 (SMB_VFS_NEXT_STAT(handle, dname, &st) == 0) {
                if (S_ISDIR(st.st_mode)) {
                        return True;
                }
@@ -170,7 +215,7 @@ static BOOL recycle_file_exist(vfs_handle_struct *handle, const char *fname)
 {
        SMB_STRUCT_STAT st;
 
-       if (SMB_VFS_NEXT_STAT(handle, handle->conn, fname, &st) == 0) {
+       if (SMB_VFS_NEXT_STAT(handle, fname, &st) == 0) {
                if (S_ISREG(st.st_mode)) {
                        return True;
                }
@@ -189,7 +234,7 @@ static SMB_OFF_T recycle_get_file_size(vfs_handle_struct *handle, const char *fn
 {
        SMB_STRUCT_STAT st;
 
-       if (SMB_VFS_NEXT_STAT(handle, handle->conn, fname, &st) != 0) {
+       if (SMB_VFS_NEXT_STAT(handle, fname, &st) != 0) {
                DEBUG(0,("recycle: stat for %s returned %s\n", fname, strerror(errno)));
                return (SMB_OFF_T)0;
        }
@@ -205,7 +250,7 @@ static SMB_OFF_T recycle_get_file_size(vfs_handle_struct *handle, const char *fn
  **/
 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;
@@ -213,7 +258,7 @@ static BOOL recycle_create_dir(vfs_handle_struct *handle, const char *dname)
        char *tok_str;
        BOOL ret = False;
 
-       mode = S_IRUSR | S_IWUSR | S_IXUSR;
+       mode = recycle_directory_mode(handle);
 
        tmp_str = SMB_STRDUP(dname);
        ALLOC_CHECK(tmp_str, done);
@@ -223,6 +268,10 @@ static BOOL recycle_create_dir(vfs_handle_struct *handle, const char *dname)
        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, "/")) {
@@ -231,13 +280,14 @@ static BOOL recycle_create_dir(vfs_handle_struct *handle, const char *dname)
                        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;
@@ -287,7 +337,7 @@ static BOOL matchparam(const char **haystack_list, const char *needle)
        }
 
        for(i=0; haystack_list[i] ; i++) {
-               if(!unix_wild_match(haystack_list[i], needle)) {
+               if(unix_wild_match(haystack_list[i], needle)) {
                        return True;
                }
        }
@@ -296,32 +346,35 @@ 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 char *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) {
+       if (SMB_VFS_NEXT_STAT(handle, fname, &st) != 0) {
                DEBUG(0,("recycle: stat for %s returned %s\n", fname, strerror(errno)));
                return;
        }
        currtime = time(&currtime);
        tb.actime = currtime;
-       tb.modtime = st.st_mtime;
+       tb.modtime = touch_mtime ? currtime : st.st_mtime;
 
-       if (SMB_VFS_NEXT_UTIME(handle, handle->conn, fname, &tb) == -1 ) {
+       if (SMB_VFS_NEXT_UTIME(handle, fname, &tb) == -1 ) {
                DEBUG(0, ("recycle: touching %s failed, reason = %s\n", fname, strerror(errno)));
        }
 }
 
+extern userdom_struct current_user_info;
+
 /**
  * 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 char *file_name)
 {
+       connection_struct *conn = handle->conn;
        char *path_name = NULL;
                char *temp_name = NULL;
        char *final_name = NULL;
@@ -333,21 +386,27 @@ static int recycle_unlink(vfs_handle_struct *handle, connection_struct *conn, co
        BOOL exist;
        int rc = -1;
 
-       repository = alloc_sub_conn(conn, recycle_repository(handle));
+       repository = alloc_sub_advanced(lp_servicename(SNUM(conn)),
+                                       conn->user,
+                                       conn->connectpath, conn->gid,
+                                       get_current_username(),
+                                       current_user_info.domain,
+                                       recycle_repository(handle));
        ALLOC_CHECK(repository, done);
        /* shouldn't we allow absolute path names here? --metze */
-       trim_char(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);
+               rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
                goto done;
        }
 
        /* we don't recycle the recycle bin... */
        if (strncmp(file_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, file_name);
                goto done;
        }
 
@@ -357,30 +416,30 @@ static int recycle_unlink(vfs_handle_struct *handle, connection_struct *conn, co
         *
        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);
+               rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
                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;
        }
         */
@@ -405,7 +464,7 @@ static int recycle_unlink(vfs_handle_struct *handle, connection_struct *conn, co
 
        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, file_name);
                goto done;
        }
 
@@ -415,7 +474,7 @@ static int recycle_unlink(vfs_handle_struct *handle, connection_struct *conn, co
         */
        if (checkparam(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, file_name);
                goto done;
        }
 
@@ -433,7 +492,7 @@ static int recycle_unlink(vfs_handle_struct *handle, connection_struct *conn, co
                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);
+                       rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
                        goto done;
                }
        }
@@ -446,7 +505,7 @@ static int recycle_unlink(vfs_handle_struct *handle, connection_struct *conn, co
        if (recycle_file_exist(handle, final_name)) {
                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) {
+                       if (SMB_VFS_NEXT_UNLINK(handle, final_name) != 0) {
                                DEBUG(1, ("recycle: Error deleting old file: %s\n", strerror(errno)));
                        }
                }
@@ -460,16 +519,16 @@ static int recycle_unlink(vfs_handle_struct *handle, connection_struct *conn, co
        }
 
        DEBUG(10, ("recycle: Moving %s to %s\n", file_name, final_name));
-       rc = SMB_VFS_NEXT_RENAME(handle, conn, file_name, final_name);
+       rc = SMB_VFS_NEXT_RENAME(handle, file_name, final_name);
        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);
+               rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
                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, final_name, recycle_touch_mtime(handle));
 
 done:
        SAFE_FREE(path_name);