1#include "cache.h"2#include "blob.h"3#include "dir.h"45static void create_directories(const char *path, const struct checkout *state)6{7int len = strlen(path);8char *buf = xmalloc(len + 1);9const char *slash = path;1011while ((slash = strchr(slash+1, '/')) != NULL) {12len = slash - path;13memcpy(buf, path, len);14buf[len] = 0;1516/*17* For 'checkout-index --prefix=<dir>', <dir> is18* allowed to be a symlink to an existing directory,19* and we set 'state->base_dir_len' below, such that20* we test the path components of the prefix with the21* stat() function instead of the lstat() function.22*/23if (has_dirs_only_path(len, buf, state->base_dir_len))24continue; /* ok, it is already a directory. */2526/*27* If this mkdir() would fail, it could be that there28* is already a symlink or something else exists29* there, therefore we then try to unlink it and try30* one more time to create the directory.31*/32if (mkdir(buf, 0777)) {33if (errno == EEXIST && state->force &&34!unlink(buf) && !mkdir(buf, 0777))35continue;36die("cannot create directory at %s", buf);37}38}39free(buf);40}4142static void remove_subtree(const char *path)43{44DIR *dir = opendir(path);45struct dirent *de;46char pathbuf[PATH_MAX];47char *name;4849if (!dir)50die("cannot opendir %s (%s)", path, strerror(errno));51strcpy(pathbuf, path);52name = pathbuf + strlen(path);53*name++ = '/';54while ((de = readdir(dir)) != NULL) {55struct stat st;56if (is_dot_or_dotdot(de->d_name))57continue;58strcpy(name, de->d_name);59if (lstat(pathbuf, &st))60die("cannot lstat %s (%s)", pathbuf, strerror(errno));61if (S_ISDIR(st.st_mode))62remove_subtree(pathbuf);63else if (unlink(pathbuf))64die("cannot unlink %s (%s)", pathbuf, strerror(errno));65}66closedir(dir);67if (rmdir(path))68die("cannot rmdir %s (%s)", path, strerror(errno));69}7071static int create_file(const char *path, unsigned int mode)72{73mode = (mode & 0100) ? 0777 : 0666;74return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);75}7677static void *read_blob_entry(struct cache_entry *ce, const char *path, unsigned long *size)78{79enum object_type type;80void *new = read_sha1_file(ce->sha1, &type, size);8182if (new) {83if (type == OBJ_BLOB)84return new;85free(new);86}87return NULL;88}8990static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)91{92int fd;93long wrote;9495switch (ce->ce_mode & S_IFMT) {96char *new;97struct strbuf buf;98unsigned long size;99100case S_IFREG:101new = read_blob_entry(ce, path, &size);102if (!new)103return error("git checkout-index: unable to read sha1 file of %s (%s)",104path, sha1_to_hex(ce->sha1));105106/*107* Convert from git internal format to working tree format108*/109strbuf_init(&buf, 0);110if (convert_to_working_tree(ce->name, new, size, &buf)) {111size_t newsize = 0;112free(new);113new = strbuf_detach(&buf, &newsize);114size = newsize;115}116117if (to_tempfile) {118strcpy(path, ".merge_file_XXXXXX");119fd = mkstemp(path);120} else121fd = create_file(path, ce->ce_mode);122if (fd < 0) {123free(new);124return error("git checkout-index: unable to create file %s (%s)",125path, strerror(errno));126}127128wrote = write_in_full(fd, new, size);129close(fd);130free(new);131if (wrote != size)132return error("git checkout-index: unable to write file %s", path);133break;134case S_IFLNK:135new = read_blob_entry(ce, path, &size);136if (!new)137return error("git checkout-index: unable to read sha1 file of %s (%s)",138path, sha1_to_hex(ce->sha1));139if (to_tempfile || !has_symlinks) {140if (to_tempfile) {141strcpy(path, ".merge_link_XXXXXX");142fd = mkstemp(path);143} else144fd = create_file(path, 0666);145if (fd < 0) {146free(new);147return error("git checkout-index: unable to create "148"file %s (%s)", path, strerror(errno));149}150wrote = write_in_full(fd, new, size);151close(fd);152free(new);153if (wrote != size)154return error("git checkout-index: unable to write file %s",155path);156} else {157wrote = symlink(new, path);158free(new);159if (wrote)160return error("git checkout-index: unable to create "161"symlink %s (%s)", path, strerror(errno));162}163break;164case S_IFGITLINK:165if (to_tempfile)166return error("git checkout-index: cannot create temporary subproject %s", path);167if (mkdir(path, 0777) < 0)168return error("git checkout-index: cannot create subproject directory %s", path);169break;170default:171return error("git checkout-index: unknown file mode for %s", path);172}173174if (state->refresh_cache) {175struct stat st;176lstat(ce->name, &st);177fill_stat_cache_info(ce, &st);178}179return 0;180}181182int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)183{184static char path[PATH_MAX + 1];185struct stat st;186int len = state->base_dir_len;187188if (topath)189return write_entry(ce, topath, state, 1);190191memcpy(path, state->base_dir, len);192strcpy(path + len, ce->name);193194if (!lstat(path, &st)) {195unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);196if (!changed)197return 0;198if (!state->force) {199if (!state->quiet)200fprintf(stderr, "git-checkout-index: %s already exists\n", path);201return -1;202}203204/*205* We unlink the old file, to get the new one with the206* right permissions (including umask, which is nasty207* to emulate by hand - much easier to let the system208* just do the right thing)209*/210if (S_ISDIR(st.st_mode)) {211/* If it is a gitlink, leave it alone! */212if (S_ISGITLINK(ce->ce_mode))213return 0;214if (!state->force)215return error("%s is a directory", path);216remove_subtree(path);217} else if (unlink(path))218return error("unable to unlink old '%s' (%s)", path, strerror(errno));219} else if (state->not_new)220return 0;221create_directories(path, state);222return write_entry(ce, path, state, 0);223}