mailinfo: iconv does not like "latin-1" -- should spell it "latin1"
[git.git] / entry.c
1 #include <sys/types.h>
2 #include <dirent.h>
3 #include "cache.h"
4
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;
10
11         while ((slash = strchr(slash+1, '/')) != NULL) {
12                 len = slash - path;
13                 memcpy(buf, path, len);
14                 buf[len] = 0;
15                 if (mkdir(buf, 0777)) {
16                         if (errno == EEXIST) {
17                                 struct stat st;
18                                 if (len > state->base_dir_len && state->force && !unlink(buf) && !mkdir(buf, 0777))
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 }
28
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 }
59
60 static int create_file(const char *path, unsigned int mode)
61 {
62         mode = (mode & 0100) ? 0777 : 0666;
63         return open(path, O_WRONLY | O_TRUNC | O_CREAT | O_EXCL, mode);
64 }
65
66 static int write_entry(struct cache_entry *ce, const char *path, struct checkout *state)
67 {
68         int fd;
69         void *new;
70         unsigned long size;
71         long wrote;
72         char type[20];
73         char target[1024];
74
75         new = read_sha1_file(ce->sha1, type, &size);
76         if (!new || strcmp(type, "blob")) {
77                 if (new)
78                         free(new);
79                 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
80                         path, sha1_to_hex(ce->sha1));
81         }
82         switch (ntohl(ce->ce_mode) & S_IFMT) {
83         case S_IFREG:
84                 fd = create_file(path, ntohl(ce->ce_mode));
85                 if (fd < 0) {
86                         free(new);
87                         return error("git-checkout-index: unable to create file %s (%s)",
88                                 path, strerror(errno));
89                 }
90                 wrote = write(fd, new, size);
91                 close(fd);
92                 free(new);
93                 if (wrote != size)
94                         return error("git-checkout-index: unable to write file %s", path);
95                 break;
96         case S_IFLNK:
97                 memcpy(target, new, size);
98                 target[size] = '\0';
99                 if (symlink(target, path)) {
100                         free(new);
101                         return error("git-checkout-index: unable to create symlink %s (%s)",
102                                 path, strerror(errno));
103                 }
104                 free(new);
105                 break;
106         default:
107                 free(new);
108                 return error("git-checkout-index: unknown file mode for %s", path);
109         }
110
111         if (state->refresh_cache) {
112                 struct stat st;
113                 lstat(ce->name, &st);
114                 fill_stat_cache_info(ce, &st);
115         }
116         return 0;
117 }
118
119 int checkout_entry(struct cache_entry *ce, struct checkout *state)
120 {
121         struct stat st;
122         static char path[MAXPATHLEN+1];
123         int len = state->base_dir_len;
124
125         memcpy(path, state->base_dir, len);
126         strcpy(path + len, ce->name);
127
128         if (!lstat(path, &st)) {
129                 unsigned changed = ce_match_stat(ce, &st);
130                 if (!changed)
131                         return 0;
132                 if (!state->force) {
133                         if (!state->quiet)
134                                 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
135                         return -1;
136                 }
137
138                 /*
139                  * We unlink the old file, to get the new one with the
140                  * right permissions (including umask, which is nasty
141                  * to emulate by hand - much easier to let the system
142                  * just do the right thing)
143                  */
144                 unlink(path);
145                 if (S_ISDIR(st.st_mode)) {
146                         if (!state->force)
147                                 return error("%s is a directory", path);
148                         remove_subtree(path);
149                 }
150         } else if (state->not_new) 
151                 return 0;
152         create_directories(path, state);
153         return write_entry(ce, path, state);
154 }
155
156