]> git.ipfire.org Git - thirdparty/git.git/blame - entry.c
contrib/git-svn: handle array values correctly
[thirdparty/git.git] / entry.c
CommitLineData
12dccc16
LT
1#include <sys/types.h>
2#include <dirent.h>
3#include "cache.h"
4
5static 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;
f312de01 15 if (mkdir(buf, 0777)) {
12dccc16
LT
16 if (errno == EEXIST) {
17 struct stat st;
f312de01 18 if (len > state->base_dir_len && state->force && !unlink(buf) && !mkdir(buf, 0777))
12dccc16
LT
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
29static 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
d48a72f3 60static int create_file(const char *path, unsigned int mode)
12dccc16 61{
12dccc16 62 mode = (mode & 0100) ? 0777 : 0666;
781411ed 63 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
12dccc16
LT
64}
65
de84f99c 66static int write_entry(struct cache_entry *ce, char *path, struct checkout *state, int to_tempfile)
12dccc16
LT
67{
68 int fd;
69 void *new;
70 unsigned long size;
71 long wrote;
72 char type[20];
12dccc16
LT
73
74 new = read_sha1_file(ce->sha1, type, &size);
75 if (!new || strcmp(type, "blob")) {
76 if (new)
77 free(new);
215a7ad1 78 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
12dccc16
LT
79 path, sha1_to_hex(ce->sha1));
80 }
81 switch (ntohl(ce->ce_mode) & S_IFMT) {
82 case S_IFREG:
de84f99c
SP
83 if (to_tempfile) {
84 strcpy(path, ".merge_file_XXXXXX");
85 fd = mkstemp(path);
86 } else
87 fd = create_file(path, ntohl(ce->ce_mode));
12dccc16
LT
88 if (fd < 0) {
89 free(new);
215a7ad1 90 return error("git-checkout-index: unable to create file %s (%s)",
12dccc16
LT
91 path, strerror(errno));
92 }
93 wrote = write(fd, new, size);
94 close(fd);
95 free(new);
96 if (wrote != size)
215a7ad1 97 return error("git-checkout-index: unable to write file %s", path);
12dccc16
LT
98 break;
99 case S_IFLNK:
de84f99c
SP
100 if (to_tempfile) {
101 strcpy(path, ".merge_link_XXXXXX");
102 fd = mkstemp(path);
103 if (fd < 0) {
104 free(new);
105 return error("git-checkout-index: unable to create "
106 "file %s (%s)", path, strerror(errno));
107 }
108 wrote = write(fd, new, size);
109 close(fd);
110 free(new);
111 if (wrote != size)
112 return error("git-checkout-index: unable to write file %s",
113 path);
114 } else {
115 wrote = symlink(new, path);
12dccc16 116 free(new);
de84f99c
SP
117 if (wrote)
118 return error("git-checkout-index: unable to create "
119 "symlink %s (%s)", path, strerror(errno));
12dccc16 120 }
12dccc16
LT
121 break;
122 default:
123 free(new);
215a7ad1 124 return error("git-checkout-index: unknown file mode for %s", path);
12dccc16
LT
125 }
126
6ee67f26 127 if (state->refresh_cache) {
12dccc16
LT
128 struct stat st;
129 lstat(ce->name, &st);
130 fill_stat_cache_info(ce, &st);
131 }
132 return 0;
133}
134
de84f99c 135int checkout_entry(struct cache_entry *ce, struct checkout *state, char *topath)
12dccc16 136{
12dccc16 137 static char path[MAXPATHLEN+1];
de84f99c 138 struct stat st;
12dccc16
LT
139 int len = state->base_dir_len;
140
de84f99c
SP
141 if (topath)
142 return write_entry(ce, topath, state, 1);
143
12dccc16
LT
144 memcpy(path, state->base_dir, len);
145 strcpy(path + len, ce->name);
146
147 if (!lstat(path, &st)) {
5f73076c 148 unsigned changed = ce_match_stat(ce, &st, 1);
12dccc16
LT
149 if (!changed)
150 return 0;
151 if (!state->force) {
152 if (!state->quiet)
215a7ad1 153 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
4b12dae6 154 return -1;
12dccc16
LT
155 }
156
157 /*
158 * We unlink the old file, to get the new one with the
159 * right permissions (including umask, which is nasty
160 * to emulate by hand - much easier to let the system
161 * just do the right thing)
162 */
163 unlink(path);
d48a72f3
LT
164 if (S_ISDIR(st.st_mode)) {
165 if (!state->force)
166 return error("%s is a directory", path);
167 remove_subtree(path);
168 }
de84f99c 169 } else if (state->not_new)
12dccc16
LT
170 return 0;
171 create_directories(path, state);
de84f99c 172 return write_entry(ce, path, state, 0);
12dccc16
LT
173}
174
175