Code

git-diff-tree: fix output with just "--pretty".
[git.git] / entry.c
1 #include <sys/types.h>
2 #include <dirent.h>
3 #include "cache.h"
5 static void create_directories(const char *path, struct checkout *state)
6 {
7         int len = strlen(path);
8         char *buf = xmalloc(len + 1);
9         const char *slash = path;
11         while ((slash = strchr(slash+1, '/')) != NULL) {
12                 len = slash - path;
13                 memcpy(buf, path, len);
14                 buf[len] = 0;
15                 if (mkdir(buf, 0755)) {
16                         if (errno == EEXIST) {
17                                 struct stat st;
18                                 if (len > state->base_dir_len && state->force && !unlink(buf) && !mkdir(buf, 0755))
19                                         continue;
20                                 if (!stat(buf, &st) && S_ISDIR(st.st_mode))
21                                         continue; /* ok */
22                         }
23                         die("cannot create directory at %s", buf);
24                 }
25         }
26         free(buf);
27 }
29 static void remove_subtree(const char *path)
30 {
31         DIR *dir = opendir(path);
32         struct dirent *de;
33         char pathbuf[PATH_MAX];
34         char *name;
35         
36         if (!dir)
37                 die("cannot opendir %s", path);
38         strcpy(pathbuf, path);
39         name = pathbuf + strlen(path);
40         *name++ = '/';
41         while ((de = readdir(dir)) != NULL) {
42                 struct stat st;
43                 if ((de->d_name[0] == '.') &&
44                     ((de->d_name[1] == 0) ||
45                      ((de->d_name[1] == '.') && de->d_name[2] == 0)))
46                         continue;
47                 strcpy(name, de->d_name);
48                 if (lstat(pathbuf, &st))
49                         die("cannot lstat %s", pathbuf);
50                 if (S_ISDIR(st.st_mode))
51                         remove_subtree(pathbuf);
52                 else if (unlink(pathbuf))
53                         die("cannot unlink %s", pathbuf);
54         }
55         closedir(dir);
56         if (rmdir(path))
57                 die("cannot rmdir %s", path);
58 }
60 static int create_file(const char *path, unsigned int mode, int force)
61 {
62         int fd;
64         mode = (mode & 0100) ? 0777 : 0666;
65         fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
66         if (fd < 0) {
67                 if (errno == EISDIR && force) {
68                         remove_subtree(path);
69                         fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
70                 }
71         }
72         return fd;
73 }
75 static int write_entry(struct cache_entry *ce, const char *path, struct checkout *state)
76 {
77         int fd;
78         void *new;
79         unsigned long size;
80         long wrote;
81         char type[20];
82         char target[1024];
84         new = read_sha1_file(ce->sha1, type, &size);
85         if (!new || strcmp(type, "blob")) {
86                 if (new)
87                         free(new);
88                 return error("git-checkout-cache: unable to read sha1 file of %s (%s)",
89                         path, sha1_to_hex(ce->sha1));
90         }
91         switch (ntohl(ce->ce_mode) & S_IFMT) {
92         case S_IFREG:
93                 fd = create_file(path, ntohl(ce->ce_mode), state->force);
94                 if (fd < 0) {
95                         free(new);
96                         return error("git-checkout-cache: unable to create file %s (%s)",
97                                 path, strerror(errno));
98                 }
99                 wrote = write(fd, new, size);
100                 close(fd);
101                 free(new);
102                 if (wrote != size)
103                         return error("git-checkout-cache: unable to write file %s", path);
104                 break;
105         case S_IFLNK:
106                 memcpy(target, new, size);
107                 target[size] = '\0';
108                 if (symlink(target, path)) {
109                         free(new);
110                         return error("git-checkout-cache: unable to create symlink %s (%s)",
111                                 path, strerror(errno));
112                 }
113                 free(new);
114                 break;
115         default:
116                 free(new);
117                 return error("git-checkout-cache: unknown file mode for %s", path);
118         }
120         if (state->refresh_cache) {
121                 struct stat st;
122                 lstat(ce->name, &st);
123                 fill_stat_cache_info(ce, &st);
124         }
125         return 0;
128 int checkout_entry(struct cache_entry *ce, struct checkout *state)
130         struct stat st;
131         static char path[MAXPATHLEN+1];
132         int len = state->base_dir_len;
134         memcpy(path, state->base_dir, len);
135         strcpy(path + len, ce->name);
137         if (!lstat(path, &st)) {
138                 unsigned changed = ce_match_stat(ce, &st);
139                 if (!changed)
140                         return 0;
141                 if (!state->force) {
142                         if (!state->quiet)
143                                 fprintf(stderr, "git-checkout-cache: %s already exists\n", path);
144                         return 0;
145                 }
147                 /*
148                  * We unlink the old file, to get the new one with the
149                  * right permissions (including umask, which is nasty
150                  * to emulate by hand - much easier to let the system
151                  * just do the right thing)
152                  */
153                 unlink(path);
154         } else if (state->not_new) 
155                 return 0;
156         create_directories(path, state);
157         return write_entry(ce, path, state);