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