- Updated the address for the FSF in the opening comment.
[rsync/rsync.git] / util.c
diff --git a/util.c b/util.c
index 50ad347..2ec56a7 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,8 +1,10 @@
-/*  -*- c-file-style: "linux" -*-
+/*
+ * Utility routines used in rsync.
  *
- * Copyright (C) 1996-2000 by Andrew Tridgell
- * Copyright (C) Paul Mackerras 1996
- * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
+ * Copyright (C) 1996-2000 Andrew Tridgell
+ * Copyright (C) 1996 Paul Mackerras
+ * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
+ * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison
  *
  * 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
  *
  * 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.
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-/**
- * @file
- *
- * Utilities used in rsync
- **/
-
 #include "rsync.h"
 
 extern int verbose;
@@ -33,16 +29,13 @@ extern int module_id;
 extern int modify_window;
 extern int relative_paths;
 extern int human_readable;
+extern mode_t orig_umask;
 extern char *partial_dir;
 extern struct filter_list_struct server_filter_list;
 
 int sanitize_paths = 0;
 
-
-
-/**
- * Set a fd into nonblocking mode
- **/
+/* Set a fd into nonblocking mode. */
 void set_nonblocking(int fd)
 {
        int val;
@@ -55,9 +48,7 @@ void set_nonblocking(int fd)
        }
 }
 
-/**
- * Set a fd into blocking mode
- **/
+/* Set a fd into blocking mode. */
 void set_blocking(int fd)
 {
        int val;
@@ -169,28 +160,43 @@ int set_modtime(char *fname, time_t modtime, mode_t mode)
        }
 }
 
-/**
-   Create any necessary directories in fname. Unfortunately we don't know
-   what perms to give the directory when this is called so we need to rely
-   on the umask
-**/
-int create_directory_path(char *fname, int base_umask)
+/* This creates a new directory with default permissions.  Since there
+ * might be some directory-default permissions affecting this, we can't
+ * force the permissions directly using the original umask and mkdir(). */
+int mkdir_defmode(char *fname)
+{
+       int ret;
+
+       umask(orig_umask);
+       ret = do_mkdir(fname, ACCESSPERMS);
+       umask(0);
+
+       return ret;
+}
+
+/* Create any necessary directories in fname.  Any missing directories are
+ * created with default permissions. */
+int create_directory_path(char *fname)
 {
        char *p;
+       int ret = 0;
 
        while (*fname == '/')
                fname++;
        while (strncmp(fname, "./", 2) == 0)
                fname += 2;
 
+       umask(orig_umask);
        p = fname;
        while ((p = strchr(p,'/')) != NULL) {
-               *p = 0;
-               do_mkdir(fname, 0777 & ~base_umask);
-               *p = '/';
-               p++;
+               *p = '\0';
+               if (do_mkdir(fname, ACCESSPERMS) < 0 && errno != EEXIST)
+                   ret = -1;
+               *p++ = '/';
        }
-       return 0;
+       umask(0);
+
+       return ret;
 }
 
 /**
@@ -743,7 +749,7 @@ unsigned int clean_fname(char *name, BOOL collapse_dot_dot)
  * of the path.  Use -1 to allow unlimited depth.
  *
  * We also clean the path in a manner similar to clean_fname() but with a
- * few differences: 
+ * few differences:
  *
  * Turns multiple adjacent slashes into a single slash, gets rid of "." dir
  * elements (INCLUDING a trailing dot dir), PRESERVES a trailing slash, and
@@ -1074,7 +1080,7 @@ char *human_num(int64 num)
 
        if (human_readable) {
                char units = '\0';
-               int mult = human_readable == 1 ? 1024 : 1000;
+               int mult = human_readable == 1 ? 1000 : 1024;
                double dnum = 0;
                if (num > mult*mult*mult) {
                        dnum = (double)num / (mult*mult*mult);