5 static void create_directories(const char *path
, int path_len
,
6 const struct checkout
*state
)
8 char *buf
= xmalloc(path_len
+ 1);
11 while (len
< path_len
) {
15 } while (len
< path_len
&& path
[len
] != '/');
21 * For 'checkout-index --prefix=<dir>', <dir> is
22 * allowed to be a symlink to an existing directory,
23 * and we set 'state->base_dir_len' below, such that
24 * we test the path components of the prefix with the
25 * stat() function instead of the lstat() function.
27 if (has_dirs_only_path(buf
, len
, state
->base_dir_len
))
28 continue; /* ok, it is already a directory. */
31 * If this mkdir() would fail, it could be that there
32 * is already a symlink or something else exists
33 * there, therefore we then try to unlink it and try
34 * one more time to create the directory.
36 if (mkdir(buf
, 0777)) {
37 if (errno
== EEXIST
&& state
->force
&&
38 !unlink(buf
) && !mkdir(buf
, 0777))
40 die("cannot create directory at %s", buf
);
46 static void remove_subtree(const char *path
)
48 DIR *dir
= opendir(path
);
50 char pathbuf
[PATH_MAX
];
54 die("cannot opendir %s (%s)", path
, strerror(errno
));
55 strcpy(pathbuf
, path
);
56 name
= pathbuf
+ strlen(path
);
58 while ((de
= readdir(dir
)) != NULL
) {
60 if (is_dot_or_dotdot(de
->d_name
))
62 strcpy(name
, de
->d_name
);
63 if (lstat(pathbuf
, &st
))
64 die("cannot lstat %s (%s)", pathbuf
, strerror(errno
));
65 if (S_ISDIR(st
.st_mode
))
66 remove_subtree(pathbuf
);
67 else if (unlink(pathbuf
))
68 die("cannot unlink %s (%s)", pathbuf
, strerror(errno
));
72 die("cannot rmdir %s (%s)", path
, strerror(errno
));
75 static int create_file(const char *path
, unsigned int mode
)
77 mode
= (mode
& 0100) ? 0777 : 0666;
78 return open(path
, O_WRONLY
| O_CREAT
| O_EXCL
, mode
);
81 static void *read_blob_entry(struct cache_entry
*ce
, const char *path
, unsigned long *size
)
83 enum object_type type
;
84 void *new = read_sha1_file(ce
->sha1
, &type
, size
);
94 static int write_entry(struct cache_entry
*ce
, char *path
, const struct checkout
*state
, int to_tempfile
)
99 switch (ce
->ce_mode
& S_IFMT
) {
105 new = read_blob_entry(ce
, path
, &size
);
107 return error("git checkout-index: unable to read sha1 file of %s (%s)",
108 path
, sha1_to_hex(ce
->sha1
));
111 * Convert from git internal format to working tree format
113 strbuf_init(&buf
, 0);
114 if (convert_to_working_tree(ce
->name
, new, size
, &buf
)) {
117 new = strbuf_detach(&buf
, &newsize
);
122 strcpy(path
, ".merge_file_XXXXXX");
125 fd
= create_file(path
, ce
->ce_mode
);
128 return error("git checkout-index: unable to create file %s (%s)",
129 path
, strerror(errno
));
132 wrote
= write_in_full(fd
, new, size
);
136 return error("git checkout-index: unable to write file %s", path
);
139 new = read_blob_entry(ce
, path
, &size
);
141 return error("git checkout-index: unable to read sha1 file of %s (%s)",
142 path
, sha1_to_hex(ce
->sha1
));
143 if (to_tempfile
|| !has_symlinks
) {
145 strcpy(path
, ".merge_link_XXXXXX");
148 fd
= create_file(path
, 0666);
151 return error("git checkout-index: unable to create "
152 "file %s (%s)", path
, strerror(errno
));
154 wrote
= write_in_full(fd
, new, size
);
158 return error("git checkout-index: unable to write file %s",
161 wrote
= symlink(new, path
);
164 return error("git checkout-index: unable to create "
165 "symlink %s (%s)", path
, strerror(errno
));
170 return error("git checkout-index: cannot create temporary subproject %s", path
);
171 if (mkdir(path
, 0777) < 0)
172 return error("git checkout-index: cannot create subproject directory %s", path
);
175 return error("git checkout-index: unknown file mode for %s", path
);
178 if (state
->refresh_cache
) {
180 lstat(ce
->name
, &st
);
181 fill_stat_cache_info(ce
, &st
);
186 int checkout_entry(struct cache_entry
*ce
, const struct checkout
*state
, char *topath
)
188 static char path
[PATH_MAX
+ 1];
190 int len
= state
->base_dir_len
;
193 return write_entry(ce
, topath
, state
, 1);
195 memcpy(path
, state
->base_dir
, len
);
196 strcpy(path
+ len
, ce
->name
);
197 len
+= ce_namelen(ce
);
199 if (!lstat(path
, &st
)) {
200 unsigned changed
= ce_match_stat(ce
, &st
, CE_MATCH_IGNORE_VALID
);
205 fprintf(stderr
, "git-checkout-index: %s already exists\n", path
);
210 * We unlink the old file, to get the new one with the
211 * right permissions (including umask, which is nasty
212 * to emulate by hand - much easier to let the system
213 * just do the right thing)
215 if (S_ISDIR(st
.st_mode
)) {
216 /* If it is a gitlink, leave it alone! */
217 if (S_ISGITLINK(ce
->ce_mode
))
220 return error("%s is a directory", path
);
221 remove_subtree(path
);
222 } else if (unlink(path
))
223 return error("unable to unlink old '%s' (%s)", path
, strerror(errno
));
224 } else if (state
->not_new
)
226 create_directories(path
, len
, state
);
227 return write_entry(ce
, path
, state
, 0);