entry.con commit index-pack: correct --keep[=<msg>] (0e94242)
   1#include "cache.h"
   2#include "blob.h"
   3#include "dir.h"
   4#include "streaming.h"
   5
   6static void create_directories(const char *path, int path_len,
   7                               const struct checkout *state)
   8{
   9        char *buf = xmalloc(path_len + 1);
  10        int len = 0;
  11
  12        while (len < path_len) {
  13                do {
  14                        buf[len] = path[len];
  15                        len++;
  16                } while (len < path_len && path[len] != '/');
  17                if (len >= path_len)
  18                        break;
  19                buf[len] = 0;
  20
  21                /*
  22                 * For 'checkout-index --prefix=<dir>', <dir> is
  23                 * allowed to be a symlink to an existing directory,
  24                 * and we set 'state->base_dir_len' below, such that
  25                 * we test the path components of the prefix with the
  26                 * stat() function instead of the lstat() function.
  27                 */
  28                if (has_dirs_only_path(buf, len, state->base_dir_len))
  29                        continue; /* ok, it is already a directory. */
  30
  31                /*
  32                 * If this mkdir() would fail, it could be that there
  33                 * is already a symlink or something else exists
  34                 * there, therefore we then try to unlink it and try
  35                 * one more time to create the directory.
  36                 */
  37                if (mkdir(buf, 0777)) {
  38                        if (errno == EEXIST && state->force &&
  39                            !unlink_or_warn(buf) && !mkdir(buf, 0777))
  40                                continue;
  41                        die_errno("cannot create directory at '%s'", buf);
  42                }
  43        }
  44        free(buf);
  45}
  46
  47static void remove_subtree(struct strbuf *path)
  48{
  49        DIR *dir = opendir(path->buf);
  50        struct dirent *de;
  51        int origlen = path->len;
  52
  53        if (!dir)
  54                die_errno("cannot opendir '%s'", path->buf);
  55        while ((de = readdir(dir)) != NULL) {
  56                struct stat st;
  57
  58                if (is_dot_or_dotdot(de->d_name))
  59                        continue;
  60
  61                strbuf_addch(path, '/');
  62                strbuf_addstr(path, de->d_name);
  63                if (lstat(path->buf, &st))
  64                        die_errno("cannot lstat '%s'", path->buf);
  65                if (S_ISDIR(st.st_mode))
  66                        remove_subtree(path);
  67                else if (unlink(path->buf))
  68                        die_errno("cannot unlink '%s'", path->buf);
  69                strbuf_setlen(path, origlen);
  70        }
  71        closedir(dir);
  72        if (rmdir(path->buf))
  73                die_errno("cannot rmdir '%s'", path->buf);
  74}
  75
  76static int create_file(const char *path, unsigned int mode)
  77{
  78        mode = (mode & 0100) ? 0777 : 0666;
  79        return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
  80}
  81
  82static void *read_blob_entry(const struct cache_entry *ce, unsigned long *size)
  83{
  84        enum object_type type;
  85        void *new = read_sha1_file(ce->sha1, &type, size);
  86
  87        if (new) {
  88                if (type == OBJ_BLOB)
  89                        return new;
  90                free(new);
  91        }
  92        return NULL;
  93}
  94
  95static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempfile)
  96{
  97        int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
  98        if (to_tempfile) {
  99                strcpy(path, symlink
 100                       ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
 101                return mkstemp(path);
 102        } else {
 103                return create_file(path, !symlink ? ce->ce_mode : 0666);
 104        }
 105}
 106
 107static int fstat_output(int fd, const struct checkout *state, struct stat *st)
 108{
 109        /* use fstat() only when path == ce->name */
 110        if (fstat_is_reliable() &&
 111            state->refresh_cache && !state->base_dir_len) {
 112                fstat(fd, st);
 113                return 1;
 114        }
 115        return 0;
 116}
 117
 118static int streaming_write_entry(const struct cache_entry *ce, char *path,
 119                                 struct stream_filter *filter,
 120                                 const struct checkout *state, int to_tempfile,
 121                                 int *fstat_done, struct stat *statbuf)
 122{
 123        int result = 0;
 124        int fd;
 125
 126        fd = open_output_fd(path, ce, to_tempfile);
 127        if (fd < 0)
 128                return -1;
 129
 130        result |= stream_blob_to_fd(fd, ce->sha1, filter, 1);
 131        *fstat_done = fstat_output(fd, state, statbuf);
 132        result |= close(fd);
 133
 134        if (result)
 135                unlink(path);
 136        return result;
 137}
 138
 139static int write_entry(struct cache_entry *ce,
 140                       char *path, const struct checkout *state, int to_tempfile)
 141{
 142        unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
 143        int fd, ret, fstat_done = 0;
 144        char *new;
 145        struct strbuf buf = STRBUF_INIT;
 146        unsigned long size;
 147        size_t wrote, newsize = 0;
 148        struct stat st;
 149
 150        if (ce_mode_s_ifmt == S_IFREG) {
 151                struct stream_filter *filter = get_stream_filter(ce->name, ce->sha1);
 152                if (filter &&
 153                    !streaming_write_entry(ce, path, filter,
 154                                           state, to_tempfile,
 155                                           &fstat_done, &st))
 156                        goto finish;
 157        }
 158
 159        switch (ce_mode_s_ifmt) {
 160        case S_IFREG:
 161        case S_IFLNK:
 162                new = read_blob_entry(ce, &size);
 163                if (!new)
 164                        return error("unable to read sha1 file of %s (%s)",
 165                                path, sha1_to_hex(ce->sha1));
 166
 167                if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
 168                        ret = symlink(new, path);
 169                        free(new);
 170                        if (ret)
 171                                return error("unable to create symlink %s (%s)",
 172                                             path, strerror(errno));
 173                        break;
 174                }
 175
 176                /*
 177                 * Convert from git internal format to working tree format
 178                 */
 179                if (ce_mode_s_ifmt == S_IFREG &&
 180                    convert_to_working_tree(ce->name, new, size, &buf)) {
 181                        free(new);
 182                        new = strbuf_detach(&buf, &newsize);
 183                        size = newsize;
 184                }
 185
 186                fd = open_output_fd(path, ce, to_tempfile);
 187                if (fd < 0) {
 188                        free(new);
 189                        return error("unable to create file %s (%s)",
 190                                path, strerror(errno));
 191                }
 192
 193                wrote = write_in_full(fd, new, size);
 194                if (!to_tempfile)
 195                        fstat_done = fstat_output(fd, state, &st);
 196                close(fd);
 197                free(new);
 198                if (wrote != size)
 199                        return error("unable to write file %s", path);
 200                break;
 201        case S_IFGITLINK:
 202                if (to_tempfile)
 203                        return error("cannot create temporary submodule %s", path);
 204                if (mkdir(path, 0777) < 0)
 205                        return error("cannot create submodule directory %s", path);
 206                break;
 207        default:
 208                return error("unknown file mode for %s in index", path);
 209        }
 210
 211finish:
 212        if (state->refresh_cache) {
 213                if (!fstat_done)
 214                        lstat(ce->name, &st);
 215                fill_stat_cache_info(ce, &st);
 216        }
 217        return 0;
 218}
 219
 220/*
 221 * This is like 'lstat()', except it refuses to follow symlinks
 222 * in the path, after skipping "skiplen".
 223 */
 224static int check_path(const char *path, int len, struct stat *st, int skiplen)
 225{
 226        const char *slash = path + len;
 227
 228        while (path < slash && *slash != '/')
 229                slash--;
 230        if (!has_dirs_only_path(path, slash - path, skiplen)) {
 231                errno = ENOENT;
 232                return -1;
 233        }
 234        return lstat(path, st);
 235}
 236
 237/*
 238 * Write the contents from ce out to the working tree.
 239 *
 240 * When topath[] is not NULL, instead of writing to the working tree
 241 * file named by ce, a temporary file is created by this function and
 242 * its name is returned in topath[], which must be able to hold at
 243 * least TEMPORARY_FILENAME_LENGTH bytes long.
 244 */
 245int checkout_entry(struct cache_entry *ce,
 246                   const struct checkout *state, char *topath)
 247{
 248        static struct strbuf path = STRBUF_INIT;
 249        struct stat st;
 250
 251        if (topath)
 252                return write_entry(ce, topath, state, 1);
 253
 254        strbuf_reset(&path);
 255        strbuf_add(&path, state->base_dir, state->base_dir_len);
 256        strbuf_add(&path, ce->name, ce_namelen(ce));
 257
 258        if (!check_path(path.buf, path.len, &st, state->base_dir_len)) {
 259                unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
 260                if (!changed)
 261                        return 0;
 262                if (!state->force) {
 263                        if (!state->quiet)
 264                                fprintf(stderr,
 265                                        "%s already exists, no checkout\n",
 266                                        path.buf);
 267                        return -1;
 268                }
 269
 270                /*
 271                 * We unlink the old file, to get the new one with the
 272                 * right permissions (including umask, which is nasty
 273                 * to emulate by hand - much easier to let the system
 274                 * just do the right thing)
 275                 */
 276                if (S_ISDIR(st.st_mode)) {
 277                        /* If it is a gitlink, leave it alone! */
 278                        if (S_ISGITLINK(ce->ce_mode))
 279                                return 0;
 280                        if (!state->force)
 281                                return error("%s is a directory", path.buf);
 282                        remove_subtree(&path);
 283                } else if (unlink(path.buf))
 284                        return error("unable to unlink old '%s' (%s)",
 285                                     path.buf, strerror(errno));
 286        } else if (state->not_new)
 287                return 0;
 288
 289        create_directories(path.buf, path.len, state);
 290        return write_entry(ce, path.buf, state, 0);
 291}