Code

Simplify calling of CR/LF conversion routines
[git.git] / entry.c
1 #include "cache.h"
2 #include "blob.h"
4 static void create_directories(const char *path, struct checkout *state)
5 {
6         int len = strlen(path);
7         char *buf = xmalloc(len + 1);
8         const char *slash = path;
10         while ((slash = strchr(slash+1, '/')) != NULL) {
11                 len = slash - path;
12                 memcpy(buf, path, len);
13                 buf[len] = 0;
14                 if (mkdir(buf, 0777)) {
15                         if (errno == EEXIST) {
16                                 struct stat st;
17                                 if (len > state->base_dir_len && state->force && !unlink(buf) && !mkdir(buf, 0777))
18                                         continue;
19                                 if (!stat(buf, &st) && S_ISDIR(st.st_mode))
20                                         continue; /* ok */
21                         }
22                         die("cannot create directory at %s", buf);
23                 }
24         }
25         free(buf);
26 }
28 static void remove_subtree(const char *path)
29 {
30         DIR *dir = opendir(path);
31         struct dirent *de;
32         char pathbuf[PATH_MAX];
33         char *name;
34         
35         if (!dir)
36                 die("cannot opendir %s", path);
37         strcpy(pathbuf, path);
38         name = pathbuf + strlen(path);
39         *name++ = '/';
40         while ((de = readdir(dir)) != NULL) {
41                 struct stat st;
42                 if ((de->d_name[0] == '.') &&
43                     ((de->d_name[1] == 0) ||
44                      ((de->d_name[1] == '.') && de->d_name[2] == 0)))
45                         continue;
46                 strcpy(name, de->d_name);
47                 if (lstat(pathbuf, &st))
48                         die("cannot lstat %s", pathbuf);
49                 if (S_ISDIR(st.st_mode))
50                         remove_subtree(pathbuf);
51                 else if (unlink(pathbuf))
52                         die("cannot unlink %s", pathbuf);
53         }
54         closedir(dir);
55         if (rmdir(path))
56                 die("cannot rmdir %s", path);
57 }
59 static int create_file(const char *path, unsigned int mode)
60 {
61         mode = (mode & 0100) ? 0777 : 0666;
62         return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
63 }
65 static int write_entry(struct cache_entry *ce, char *path, struct checkout *state, int to_tempfile)
66 {
67         int fd;
68         void *new;
69         unsigned long size;
70         long wrote;
71         enum object_type type;
73         new = read_sha1_file(ce->sha1, &type, &size);
74         if (!new || type != OBJ_BLOB) {
75                 if (new)
76                         free(new);
77                 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
78                         path, sha1_to_hex(ce->sha1));
79         }
80         switch (ntohl(ce->ce_mode) & S_IFMT) {
81                 char *buf;
83         case S_IFREG:
84                 if (to_tempfile) {
85                         strcpy(path, ".merge_file_XXXXXX");
86                         fd = mkstemp(path);
87                 } else
88                         fd = create_file(path, ntohl(ce->ce_mode));
89                 if (fd < 0) {
90                         free(new);
91                         return error("git-checkout-index: unable to create file %s (%s)",
92                                 path, strerror(errno));
93                 }
95                 /*
96                  * Convert from git internal format to working tree format
97                  */
98                 buf = convert_to_working_tree(ce->name, new, &size);
99                 if (buf) {
100                         free(new);
101                         new = buf;
102                 }
104                 wrote = write_in_full(fd, new, size);
105                 close(fd);
106                 free(new);
107                 if (wrote != size)
108                         return error("git-checkout-index: unable to write file %s", path);
109                 break;
110         case S_IFLNK:
111                 if (to_tempfile || !has_symlinks) {
112                         if (to_tempfile) {
113                                 strcpy(path, ".merge_link_XXXXXX");
114                                 fd = mkstemp(path);
115                         } else
116                                 fd = create_file(path, 0666);
117                         if (fd < 0) {
118                                 free(new);
119                                 return error("git-checkout-index: unable to create "
120                                                  "file %s (%s)", path, strerror(errno));
121                         }
122                         wrote = write_in_full(fd, new, size);
123                         close(fd);
124                         free(new);
125                         if (wrote != size)
126                                 return error("git-checkout-index: unable to write file %s",
127                                         path);
128                 } else {
129                         wrote = symlink(new, path);
130                         free(new);
131                         if (wrote)
132                                 return error("git-checkout-index: unable to create "
133                                                  "symlink %s (%s)", path, strerror(errno));
134                 }
135                 break;
136         default:
137                 free(new);
138                 return error("git-checkout-index: unknown file mode for %s", path);
139         }
141         if (state->refresh_cache) {
142                 struct stat st;
143                 lstat(ce->name, &st);
144                 fill_stat_cache_info(ce, &st);
145         }
146         return 0;
149 int checkout_entry(struct cache_entry *ce, struct checkout *state, char *topath)
151         static char path[PATH_MAX + 1];
152         struct stat st;
153         int len = state->base_dir_len;
155         if (topath)
156                 return write_entry(ce, topath, state, 1);
158         memcpy(path, state->base_dir, len);
159         strcpy(path + len, ce->name);
161         if (!lstat(path, &st)) {
162                 unsigned changed = ce_match_stat(ce, &st, 1);
163                 if (!changed)
164                         return 0;
165                 if (!state->force) {
166                         if (!state->quiet)
167                                 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
168                         return -1;
169                 }
171                 /*
172                  * We unlink the old file, to get the new one with the
173                  * right permissions (including umask, which is nasty
174                  * to emulate by hand - much easier to let the system
175                  * just do the right thing)
176                  */
177                 unlink(path);
178                 if (S_ISDIR(st.st_mode)) {
179                         if (!state->force)
180                                 return error("%s is a directory", path);
181                         remove_subtree(path);
182                 }
183         } else if (state->not_new)
184                 return 0;
185         create_directories(path, state);
186         return write_entry(ce, path, state, 0);