Merge branch 'ew/apply'
[gitweb.git] / builtin-tar-tree.c
index f646c5bd6e01f07e6b6046e465ff9044d20f0c8f..e5aaded820bddfe2d5175508cc9c422b55a66eec 100644 (file)
@@ -20,10 +20,13 @@ static char block[BLOCKSIZE];
 static unsigned long offset;
 
 static time_t archive_time;
+static int tar_umask;
 
 /* tries hard to write, either succeeds or dies in the attempt */
-static void reliable_write(void *buf, unsigned long size)
+static void reliable_write(const void *data, unsigned long size)
 {
+       const char *buf = data;
+
        while (size > 0) {
                long ret = xwrite(1, buf, size);
                if (ret < 0) {
@@ -51,8 +54,9 @@ static void write_if_needed(void)
  * queues up writes, so that all our write(2) calls write exactly one
  * full block; pads writes to RECORDSIZE
  */
-static void write_blocked(void *buf, unsigned long size)
+static void write_blocked(const void *data, unsigned long size)
 {
+       const char *buf = data;
        unsigned long tail;
 
        if (offset) {
@@ -185,13 +189,13 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
        } else {
                if (S_ISDIR(mode)) {
                        *header.typeflag = TYPEFLAG_DIR;
-                       mode |= 0777;
+                       mode = (mode | 0777) & ~tar_umask;
                } else if (S_ISLNK(mode)) {
                        *header.typeflag = TYPEFLAG_LNK;
                        mode |= 0777;
                } else if (S_ISREG(mode)) {
                        *header.typeflag = TYPEFLAG_REG;
-                       mode |= (mode & 0100) ? 0777 : 0666;
+                       mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
                } else {
                        error("unsupported file mode: 0%o (SHA1: %s)",
                              mode, sha1_to_hex(sha1));
@@ -230,8 +234,8 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
        /* XXX: should we provide more meaningful info here? */
        sprintf(header.uid, "%07o", 0);
        sprintf(header.gid, "%07o", 0);
-       safe_strncpy(header.uname, "git", sizeof(header.uname));
-       safe_strncpy(header.gname, "git", sizeof(header.gname));
+       strlcpy(header.uname, "git", sizeof(header.uname));
+       strlcpy(header.gname, "git", sizeof(header.gname));
        sprintf(header.devmajor, "%07o", 0);
        sprintf(header.devminor, "%07o", 0);
 
@@ -290,6 +294,20 @@ static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
        }
 }
 
+int git_tar_config(const char *var, const char *value)
+{
+       if (!strcmp(var, "tar.umask")) {
+               if (!strcmp(value, "user")) {
+                       tar_umask = umask(0);
+                       umask(tar_umask);
+               } else {
+                       tar_umask = git_config_int(var, value);
+               }
+               return 0;
+       }
+       return git_default_config(var, value);
+}
+
 static int generate_tar(int argc, const char **argv, char** envp)
 {
        unsigned char sha1[20], tree_sha1[20];
@@ -302,7 +320,7 @@ static int generate_tar(int argc, const char **argv, char** envp)
        current_path.len = current_path.eof = 0;
 
        setup_git_directory();
-       git_config(git_default_config);
+       git_config(git_tar_config);
 
        switch (argc) {
        case 3: