5 static void create_directories(const char *path
, const struct checkout
*state
)
7 int len
= strlen(path
);
8 char *buf
= xmalloc(len
+ 1);
9 const char *slash
= path
;
11 while ((slash
= strchr(slash
+1, '/')) != NULL
) {
13 memcpy(buf
, path
, len
);
17 * For 'checkout-index --prefix=<dir>', <dir> is
18 * allowed to be a symlink to an existing directory,
19 * and we set 'state->base_dir_len' below, such that
20 * we test the path components of the prefix with the
21 * stat() function instead of the lstat() function.
23 if (has_dirs_only_path(len
, buf
, state
->base_dir_len
))
24 continue; /* ok, it is already a directory. */
27 * If this mkdir() would fail, it could be that there
28 * is already a symlink or something else exists
29 * there, therefore we then try to unlink it and try
30 * one more time to create the directory.
32 if (mkdir(buf
, 0777)) {
33 if (errno
== EEXIST
&& state
->force
&&
34 !unlink(buf
) && !mkdir(buf
, 0777))
36 die("cannot create directory at %s", buf
);
42 static void remove_subtree(const char *path
)
44 DIR *dir
= opendir(path
);
46 char pathbuf
[PATH_MAX
];
50 die("cannot opendir %s (%s)", path
, strerror(errno
));
51 strcpy(pathbuf
, path
);
52 name
= pathbuf
+ strlen(path
);
54 while ((de
= readdir(dir
)) != NULL
) {
56 if (is_dot_or_dotdot(de
->d_name
))
58 strcpy(name
, de
->d_name
);
59 if (lstat(pathbuf
, &st
))
60 die("cannot lstat %s (%s)", pathbuf
, strerror(errno
));
61 if (S_ISDIR(st
.st_mode
))
62 remove_subtree(pathbuf
);
63 else if (unlink(pathbuf
))
64 die("cannot unlink %s (%s)", pathbuf
, strerror(errno
));
68 die("cannot rmdir %s (%s)", path
, strerror(errno
));
71 static int create_file(const char *path
, unsigned int mode
)
73 mode
= (mode
& 0100) ? 0777 : 0666;
74 return open(path
, O_WRONLY
| O_CREAT
| O_EXCL
, mode
);
77 static void *read_blob_entry(struct cache_entry
*ce
, const char *path
, unsigned long *size
)
79 enum object_type type
;
80 void *new = read_sha1_file(ce
->sha1
, &type
, size
);
90 static int write_entry(struct cache_entry
*ce
, char *path
, const struct checkout
*state
, int to_tempfile
)
95 switch (ce
->ce_mode
& S_IFMT
) {
101 new = read_blob_entry(ce
, path
, &size
);
103 return error("git checkout-index: unable to read sha1 file of %s (%s)",
104 path
, sha1_to_hex(ce
->sha1
));
107 * Convert from git internal format to working tree format
109 strbuf_init(&buf
, 0);
110 if (convert_to_working_tree(ce
->name
, new, size
, &buf
)) {
113 new = strbuf_detach(&buf
, &newsize
);
118 strcpy(path
, ".merge_file_XXXXXX");
121 fd
= create_file(path
, ce
->ce_mode
);
124 return error("git checkout-index: unable to create file %s (%s)",
125 path
, strerror(errno
));
128 wrote
= write_in_full(fd
, new, size
);
132 return error("git checkout-index: unable to write file %s", path
);
135 new = read_blob_entry(ce
, path
, &size
);
137 return error("git checkout-index: unable to read sha1 file of %s (%s)",
138 path
, sha1_to_hex(ce
->sha1
));
139 if (to_tempfile
|| !has_symlinks
) {
141 strcpy(path
, ".merge_link_XXXXXX");
144 fd
= create_file(path
, 0666);
147 return error("git checkout-index: unable to create "
148 "file %s (%s)", path
, strerror(errno
));
150 wrote
= write_in_full(fd
, new, size
);
154 return error("git checkout-index: unable to write file %s",
157 wrote
= symlink(new, path
);
160 return error("git checkout-index: unable to create "
161 "symlink %s (%s)", path
, strerror(errno
));
166 return error("git checkout-index: cannot create temporary subproject %s", path
);
167 if (mkdir(path
, 0777) < 0)
168 return error("git checkout-index: cannot create subproject directory %s", path
);
171 return error("git checkout-index: unknown file mode for %s", path
);
174 if (state
->refresh_cache
) {
176 lstat(ce
->name
, &st
);
177 fill_stat_cache_info(ce
, &st
);
182 int checkout_entry(struct cache_entry
*ce
, const struct checkout
*state
, char *topath
)
184 static char path
[PATH_MAX
+ 1];
186 int len
= state
->base_dir_len
;
189 return write_entry(ce
, topath
, state
, 1);
191 memcpy(path
, state
->base_dir
, len
);
192 strcpy(path
+ len
, ce
->name
);
194 if (!lstat(path
, &st
)) {
195 unsigned changed
= ce_match_stat(ce
, &st
, CE_MATCH_IGNORE_VALID
);
200 fprintf(stderr
, "git-checkout-index: %s already exists\n", path
);
205 * We unlink the old file, to get the new one with the
206 * right permissions (including umask, which is nasty
207 * to emulate by hand - much easier to let the system
208 * just do the right thing)
210 if (S_ISDIR(st
.st_mode
)) {
211 /* If it is a gitlink, leave it alone! */
212 if (S_ISGITLINK(ce
->ce_mode
))
215 return error("%s is a directory", path
);
216 remove_subtree(path
);
217 } else if (unlink(path
))
218 return error("unable to unlink old '%s' (%s)", path
, strerror(errno
));
219 } else if (state
->not_new
)
221 create_directories(path
, state
);
222 return write_entry(ce
, path
, state
, 0);