Fix all warnings in source3 with gcc4.3.
[ira/wip.git] / source3 / modules / vfs_recycle.c
index cb5eaffa54d21b53aaa4e7539f6e6aefa29808ed..cb985e1be22f45bdf982afac63bd3e010aa62948 100644 (file)
@@ -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,8 +19,7 @@
  * 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 <http://www.gnu.org/licenses/>.
  */
 
 #include "includes.h"
@@ -76,9 +75,9 @@ static const char *recycle_repository(vfs_handle_struct *handle)
        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);
 
@@ -87,9 +86,9 @@ static BOOL recycle_keep_dir_tree(vfs_handle_struct *handle)
        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);
 
@@ -98,9 +97,9 @@ static BOOL recycle_versions(vfs_handle_struct *handle)
        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);
 
@@ -109,9 +108,9 @@ static BOOL recycle_touch(vfs_handle_struct *handle)
        return ret;
 }
 
-static BOOL recycle_touch_mtime(vfs_handle_struct *handle)
+static bool recycle_touch_mtime(vfs_handle_struct *handle)
 {
-       BOOL ret;
+       bool ret;
 
        ret = lp_parm_bool(SNUM(handle->conn), "recycle", "touch_mtime", False);
 
@@ -153,17 +152,30 @@ static const char **recycle_noversions(vfs_handle_struct *handle)
        return tmp_lp;
 }
 
-static int recycle_maxsize(vfs_handle_struct *handle)
+static SMB_OFF_T recycle_maxsize(vfs_handle_struct *handle)
 {
-       int maxsize;
+       SMB_OFF_T maxsize;
        
-       maxsize = lp_parm_int(SNUM(handle->conn), "recycle", "maxsize", -1);
+       maxsize = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
+                                           "recycle", "maxsize", NULL));
 
-       DEBUG(10, ("recycle: maxsize = %d\n", maxsize));
+       DEBUG(10, ("recycle: maxsize = %lu\n", (long unsigned int)maxsize));
        
        return maxsize;
 }
 
+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;
@@ -198,7 +210,7 @@ static mode_t recycle_subdir_mode(vfs_handle_struct *handle)
        return (mode_t)dirmode;
 }
 
-static BOOL recycle_directory_exist(vfs_handle_struct *handle, const char *dname)
+static bool recycle_directory_exist(vfs_handle_struct *handle, const char *dname)
 {
        SMB_STRUCT_STAT st;
 
@@ -211,7 +223,7 @@ 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 char *fname)
 {
        SMB_STRUCT_STAT st;
 
@@ -248,7 +260,7 @@ 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)
 {
        size_t len;
        mode_t mode;
@@ -256,7 +268,8 @@ static BOOL recycle_create_dir(vfs_handle_struct *handle, const char *dname)
        char *tmp_str = NULL;
        char *token;
        char *tok_str;
-       BOOL ret = False;
+       bool ret = False;
+       char *saveptr;
 
        mode = recycle_directory_mode(handle);
 
@@ -274,7 +287,8 @@ static BOOL recycle_create_dir(vfs_handle_struct *handle, const char *dname)
        }
 
        /* 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));
@@ -298,23 +312,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 = '/';
                }
        }
 
@@ -327,7 +366,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;
 
@@ -348,27 +387,31 @@ static BOOL matchparam(const char **haystack_list, const char *needle)
 /**
  * Touch access or modify date
  **/
-static void recycle_do_touch(vfs_handle_struct *handle, const char *fname, BOOL touch_mtime)
+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;
-       
+       struct timespec ts[2];
+       int ret, err;
+
        if (SMB_VFS_NEXT_STAT(handle, fname, &st) != 0) {
-               DEBUG(0,("recycle: stat for %s returned %s\n", fname, strerror(errno)));
+               DEBUG(0,("recycle: stat for %s returned %s\n",
+                        fname, strerror(errno)));
                return;
        }
-       currtime = time(&currtime);
-       tb.actime = currtime;
-       tb.modtime = touch_mtime ? currtime : st.st_mtime;
-
-       if (SMB_VFS_NEXT_UTIME(handle, fname, &tb) == -1 ) {
-               DEBUG(0, ("recycle: touching %s failed, reason = %s\n", fname, strerror(errno)));
+       ts[0] = timespec_current(); /* atime */
+       ts[1] = touch_mtime ? ts[0] : get_mtimespec(&st); /* mtime */
+
+       become_root();
+       ret = SMB_VFS_NEXT_NTIMES(handle, fname, ts);
+       err = errno;
+       unbecome_root();
+       if (ret == -1 ) {
+               DEBUG(0, ("recycle: touching %s failed, reason = %s\n",
+                         fname, strerror(err)));
        }
 }
 
-extern userdom_struct current_user_info;
-
 /**
  * Check if file should be recycled
  **/
@@ -381,16 +424,17 @@ static int recycle_unlink(vfs_handle_struct *handle, const char *file_name)
        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;
        int rc = -1;
 
-       repository = alloc_sub_advanced(lp_servicename(SNUM(conn)),
-                                       conn->user,
-                                       conn->connectpath, conn->gid,
-                                       get_current_username(),
-                                       current_user_info.domain,
+       repository = talloc_sub_advanced(NULL, lp_servicename(SNUM(conn)),
+                                       conn->server_info->unix_name,
+                                       conn->connectpath,
+                                       conn->server_info->utok.gid,
+                                       conn->server_info->sanitized_username,
+                                       pdb_get_domain(conn->server_info->sam_account),
                                        recycle_repository(handle));
        ALLOC_CHECK(repository, done);
        /* shouldn't we allow absolute path names here? --metze */
@@ -431,6 +475,12 @@ static int recycle_unlink(vfs_handle_struct *handle, const char *file_name)
                rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
                goto done;
        }
+       minsize = recycle_minsize(handle);
+       if(minsize > 0 && file_size < minsize) {
+               DEBUG(3, ("recycle: File %s lowers minimum recycle size, purging... \n", 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
@@ -468,18 +518,16 @@ static int recycle_unlink(vfs_handle_struct *handle, const char *file_name)
                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, file_name);
                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 = SMB_STRDUP(repository);
        }
@@ -497,8 +545,9 @@ static int recycle_unlink(vfs_handle_struct *handle, const char *file_name)
                }
        }
 
-       asprintf(&final_name, "%s/%s", temp_name, base);
-       ALLOC_CHECK(final_name, done);
+       if (asprintf(&final_name, "%s/%s", temp_name, base) == -1) {
+               ALLOC_CHECK(final_name, done);
+       }
        DEBUG(10, ("recycle: recycled file name: %s\n", final_name));           /* new filename with path */
 
        /* check if we should delete file from recycle bin */
@@ -515,7 +564,9 @@ static int recycle_unlink(vfs_handle_struct *handle, const char *file_name)
        i = 1;
        while (recycle_file_exist(handle, final_name)) {
                SAFE_FREE(final_name);
-               asprintf(&final_name, "%s/Copy #%d of %s", temp_name, i++, base);
+               if (asprintf(&final_name, "%s/Copy #%d of %s", temp_name, i++, base) == -1) {
+                       ALLOC_CHECK(final_name, done);
+               }
        }
 
        DEBUG(10, ("recycle: Moving %s to %s\n", file_name, final_name));
@@ -534,10 +585,11 @@ done:
        SAFE_FREE(path_name);
        SAFE_FREE(temp_name);
        SAFE_FREE(final_name);
-       SAFE_FREE(repository);
+       TALLOC_FREE(repository);
        return rc;
 }
 
+NTSTATUS vfs_recycle_init(void);
 NTSTATUS vfs_recycle_init(void)
 {
        NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "recycle", recycle_ops);