entry.con commit Fix some "git ls-files -o" fallout from gitlinks (5698454)
   1#include "cache.h"
   2#include "blob.h"
   3
   4static void create_directories(const char *path, struct checkout *state)
   5{
   6        int len = strlen(path);
   7        char *buf = xmalloc(len + 1);
   8        const char *slash = path;
   9
  10        while ((slash = strchr(slash+1, '/')) != NULL) {
  11                len = slash - path;
  12                memcpy(buf, path, len);
  13                buf[len] = 0;
  14                if (mkdir(buf, 0777)) {
  15                        if (errno == EEXIST) {
  16                                struct stat st;
  17                                if (len > state->base_dir_len && state->force && !unlink(buf) && !mkdir(buf, 0777))
  18                                        continue;
  19                                if (!stat(buf, &st) && S_ISDIR(st.st_mode))
  20                                        continue; /* ok */
  21                        }
  22                        die("cannot create directory at %s", buf);
  23                }
  24        }
  25        free(buf);
  26}
  27
  28static void remove_subtree(const char *path)
  29{
  30        DIR *dir = opendir(path);
  31        struct dirent *de;
  32        char pathbuf[PATH_MAX];
  33        char *name;
  34        
  35        if (!dir)
  36                die("cannot opendir %s", path);
  37        strcpy(pathbuf, path);
  38        name = pathbuf + strlen(path);
  39        *name++ = '/';
  40        while ((de = readdir(dir)) != NULL) {
  41                struct stat st;
  42                if ((de->d_name[0] == '.') &&
  43                    ((de->d_name[1] == 0) ||
  44                     ((de->d_name[1] == '.') && de->d_name[2] == 0)))
  45                        continue;
  46                strcpy(name, de->d_name);
  47                if (lstat(pathbuf, &st))
  48                        die("cannot lstat %s", pathbuf);
  49                if (S_ISDIR(st.st_mode))
  50                        remove_subtree(pathbuf);
  51                else if (unlink(pathbuf))
  52                        die("cannot unlink %s", pathbuf);
  53        }
  54        closedir(dir);
  55        if (rmdir(path))
  56                die("cannot rmdir %s", path);
  57}
  58
  59static int create_file(const char *path, unsigned int mode)
  60{
  61        mode = (mode & 0100) ? 0777 : 0666;
  62        return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
  63}
  64
  65static void *read_blob_entry(struct cache_entry *ce, const char *path, unsigned long *size)
  66{
  67        enum object_type type;
  68        void *new = read_sha1_file(ce->sha1, &type, size);
  69
  70        if (new) {
  71                if (type == OBJ_BLOB)
  72                        return new;
  73                free(new);
  74        }
  75        return NULL;
  76}
  77
  78static int write_entry(struct cache_entry *ce, char *path, struct checkout *state, int to_tempfile)
  79{
  80        int fd;
  81        long wrote;
  82
  83        switch (ntohl(ce->ce_mode) & S_IFMT) {
  84                char *buf, *new;
  85                unsigned long size, nsize;
  86
  87        case S_IFREG:
  88                new = read_blob_entry(ce, path, &size);
  89                if (!new)
  90                        return error("git-checkout-index: unable to read sha1 file of %s (%s)",
  91                                path, sha1_to_hex(ce->sha1));
  92                if (to_tempfile) {
  93                        strcpy(path, ".merge_file_XXXXXX");
  94                        fd = mkstemp(path);
  95                } else
  96                        fd = create_file(path, ntohl(ce->ce_mode));
  97                if (fd < 0) {
  98                        free(new);
  99                        return error("git-checkout-index: unable to create file %s (%s)",
 100                                path, strerror(errno));
 101                }
 102
 103                /*
 104                 * Convert from git internal format to working tree format
 105                 */
 106                buf = new;
 107                nsize = size;
 108                if (convert_to_working_tree(ce->name, &buf, &nsize)) {
 109                        free(new);
 110                        new = buf;
 111                        size = nsize;
 112                }
 113
 114                wrote = write_in_full(fd, new, size);
 115                close(fd);
 116                free(new);
 117                if (wrote != size)
 118                        return error("git-checkout-index: unable to write file %s", path);
 119                break;
 120        case S_IFLNK:
 121                new = read_blob_entry(ce, path, &size);
 122                if (!new)
 123                        return error("git-checkout-index: unable to read sha1 file of %s (%s)",
 124                                path, sha1_to_hex(ce->sha1));
 125                if (to_tempfile || !has_symlinks) {
 126                        if (to_tempfile) {
 127                                strcpy(path, ".merge_link_XXXXXX");
 128                                fd = mkstemp(path);
 129                        } else
 130                                fd = create_file(path, 0666);
 131                        if (fd < 0) {
 132                                free(new);
 133                                return error("git-checkout-index: unable to create "
 134                                                 "file %s (%s)", path, strerror(errno));
 135                        }
 136                        wrote = write_in_full(fd, new, size);
 137                        close(fd);
 138                        free(new);
 139                        if (wrote != size)
 140                                return error("git-checkout-index: unable to write file %s",
 141                                        path);
 142                } else {
 143                        wrote = symlink(new, path);
 144                        free(new);
 145                        if (wrote)
 146                                return error("git-checkout-index: unable to create "
 147                                                 "symlink %s (%s)", path, strerror(errno));
 148                }
 149                break;
 150        case S_IFDIRLNK:
 151                if (to_tempfile)
 152                        return error("git-checkout-index: cannot create temporary subproject %s", path);
 153                if (mkdir(path, 0777) < 0)
 154                        return error("git-checkout-index: cannot create subproject directory %s", path);
 155                break;
 156        default:
 157                return error("git-checkout-index: unknown file mode for %s", path);
 158        }
 159
 160        if (state->refresh_cache) {
 161                struct stat st;
 162                lstat(ce->name, &st);
 163                fill_stat_cache_info(ce, &st);
 164        }
 165        return 0;
 166}
 167
 168int checkout_entry(struct cache_entry *ce, struct checkout *state, char *topath)
 169{
 170        static char path[PATH_MAX + 1];
 171        struct stat st;
 172        int len = state->base_dir_len;
 173
 174        if (topath)
 175                return write_entry(ce, topath, state, 1);
 176
 177        memcpy(path, state->base_dir, len);
 178        strcpy(path + len, ce->name);
 179
 180        if (!lstat(path, &st)) {
 181                unsigned changed = ce_match_stat(ce, &st, 1);
 182                if (!changed)
 183                        return 0;
 184                if (!state->force) {
 185                        if (!state->quiet)
 186                                fprintf(stderr, "git-checkout-index: %s already exists\n", path);
 187                        return -1;
 188                }
 189
 190                /*
 191                 * We unlink the old file, to get the new one with the
 192                 * right permissions (including umask, which is nasty
 193                 * to emulate by hand - much easier to let the system
 194                 * just do the right thing)
 195                 */
 196                unlink(path);
 197                if (S_ISDIR(st.st_mode)) {
 198                        /* If it is a gitlink, leave it alone! */
 199                        if (S_ISDIRLNK(ntohl(ce->ce_mode)))
 200                                return 0;
 201                        if (!state->force)
 202                                return error("%s is a directory", path);
 203                        remove_subtree(path);
 204                }
 205        } else if (state->not_new)
 206                return 0;
 207        create_directories(path, state);
 208        return write_entry(ce, path, state, 0);
 209}