4 static void create_directories(const char *path
, const struct checkout
*state
)
6 int len
= strlen(path
);
7 char *buf
= xmalloc(len
+ 1);
8 const char *slash
= path
;
10 while ((slash
= strchr(slash
+1, '/')) != NULL
) {
12 memcpy(buf
, path
, len
);
14 if (mkdir(buf
, 0777)) {
15 if (errno
== EEXIST
) {
17 if (len
> state
->base_dir_len
&& state
->force
&& !unlink(buf
) && !mkdir(buf
, 0777))
19 if (!stat(buf
, &st
) && S_ISDIR(st
.st_mode
))
22 die("cannot create directory at %s", buf
);
28 static void remove_subtree(const char *path
)
30 DIR *dir
= opendir(path
);
32 char pathbuf
[PATH_MAX
];
36 die("cannot opendir %s (%s)", path
, strerror(errno
));
37 strcpy(pathbuf
, path
);
38 name
= pathbuf
+ strlen(path
);
40 while ((de
= readdir(dir
)) != NULL
) {
42 if ((de
->d_name
[0] == '.') &&
43 ((de
->d_name
[1] == 0) ||
44 ((de
->d_name
[1] == '.') && de
->d_name
[2] == 0)))
46 strcpy(name
, de
->d_name
);
47 if (lstat(pathbuf
, &st
))
48 die("cannot lstat %s (%s)", pathbuf
, strerror(errno
));
49 if (S_ISDIR(st
.st_mode
))
50 remove_subtree(pathbuf
);
51 else if (unlink(pathbuf
))
52 die("cannot unlink %s (%s)", pathbuf
, strerror(errno
));
56 die("cannot rmdir %s (%s)", path
, strerror(errno
));
59 static int create_file(const char *path
, unsigned int mode
)
61 mode
= (mode
& 0100) ? 0777 : 0666;
62 return open(path
, O_WRONLY
| O_CREAT
| O_EXCL
, mode
);
65 static void *read_blob_entry(struct cache_entry
*ce
, const char *path
, unsigned long *size
)
67 enum object_type type
;
68 void *new = read_sha1_file(ce
->sha1
, &type
, size
);
78 static int write_entry(struct cache_entry
*ce
, char *path
, const struct checkout
*state
, int to_tempfile
)
83 switch (ntohl(ce
->ce_mode
) & S_IFMT
) {
88 new = read_blob_entry(ce
, path
, &size
);
90 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
91 path
, sha1_to_hex(ce
->sha1
));
93 strcpy(path
, ".merge_file_XXXXXX");
96 fd
= create_file(path
, ntohl(ce
->ce_mode
));
99 return error("git-checkout-index: unable to create file %s (%s)",
100 path
, strerror(errno
));
104 * Convert from git internal format to working tree format
106 buf
= convert_to_working_tree(ce
->name
, new, &size
);
112 wrote
= write_in_full(fd
, new, size
);
116 return error("git-checkout-index: unable to write file %s", path
);
119 new = read_blob_entry(ce
, path
, &size
);
121 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
122 path
, sha1_to_hex(ce
->sha1
));
123 if (to_tempfile
|| !has_symlinks
) {
125 strcpy(path
, ".merge_link_XXXXXX");
128 fd
= create_file(path
, 0666);
131 return error("git-checkout-index: unable to create "
132 "file %s (%s)", path
, strerror(errno
));
134 wrote
= write_in_full(fd
, new, size
);
138 return error("git-checkout-index: unable to write file %s",
141 wrote
= symlink(new, path
);
144 return error("git-checkout-index: unable to create "
145 "symlink %s (%s)", path
, strerror(errno
));
150 return error("git-checkout-index: cannot create temporary subproject %s", path
);
151 if (mkdir(path
, 0777) < 0)
152 return error("git-checkout-index: cannot create subproject directory %s", path
);
155 return error("git-checkout-index: unknown file mode for %s", path
);
158 if (state
->refresh_cache
) {
160 lstat(ce
->name
, &st
);
161 fill_stat_cache_info(ce
, &st
);
166 int checkout_entry(struct cache_entry
*ce
, const struct checkout
*state
, char *topath
)
168 static char path
[PATH_MAX
+ 1];
170 int len
= state
->base_dir_len
;
173 return write_entry(ce
, topath
, state
, 1);
175 memcpy(path
, state
->base_dir
, len
);
176 strcpy(path
+ len
, ce
->name
);
178 if (!lstat(path
, &st
)) {
179 unsigned changed
= ce_match_stat(ce
, &st
, 1);
184 fprintf(stderr
, "git-checkout-index: %s already exists\n", path
);
189 * We unlink the old file, to get the new one with the
190 * right permissions (including umask, which is nasty
191 * to emulate by hand - much easier to let the system
192 * just do the right thing)
195 if (S_ISDIR(st
.st_mode
)) {
196 /* If it is a gitlink, leave it alone! */
197 if (S_ISGITLINK(ntohl(ce
->ce_mode
)))
200 return error("%s is a directory", path
);
201 remove_subtree(path
);
203 } else if (state
->not_new
)
205 create_directories(path
, state
);
206 return write_entry(ce
, path
, state
, 0);