entry.con commit clarify some error messages wrt unknown object types (08a19d8)
   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 int write_entry(struct cache_entry *ce, char *path, struct checkout *state, int to_tempfile)
  66{
  67        int fd;
  68        void *new;
  69        unsigned long size;
  70        long wrote;
  71        char type[20];
  72
  73        new = read_sha1_file(ce->sha1, type, &size);
  74        if (!new || strcmp(type, blob_type)) {
  75                if (new)
  76                        free(new);
  77                return error("git-checkout-index: unable to read sha1 file of %s (%s)",
  78                        path, sha1_to_hex(ce->sha1));
  79        }
  80        switch (ntohl(ce->ce_mode) & S_IFMT) {
  81        case S_IFREG:
  82                if (to_tempfile) {
  83                        strcpy(path, ".merge_file_XXXXXX");
  84                        fd = mkstemp(path);
  85                } else
  86                        fd = create_file(path, ntohl(ce->ce_mode));
  87                if (fd < 0) {
  88                        free(new);
  89                        return error("git-checkout-index: unable to create file %s (%s)",
  90                                path, strerror(errno));
  91                }
  92                wrote = write(fd, new, size);
  93                close(fd);
  94                free(new);
  95                if (wrote != size)
  96                        return error("git-checkout-index: unable to write file %s", path);
  97                break;
  98        case S_IFLNK:
  99                if (to_tempfile) {
 100                        strcpy(path, ".merge_link_XXXXXX");
 101                        fd = mkstemp(path);
 102                        if (fd < 0) {
 103                                free(new);
 104                                return error("git-checkout-index: unable to create "
 105                                                 "file %s (%s)", path, strerror(errno));
 106                        }
 107                        wrote = write(fd, new, size);
 108                        close(fd);
 109                        free(new);
 110                        if (wrote != size)
 111                                return error("git-checkout-index: unable to write file %s",
 112                                        path);
 113                } else {
 114                        wrote = symlink(new, path);
 115                        free(new);
 116                        if (wrote)
 117                                return error("git-checkout-index: unable to create "
 118                                                 "symlink %s (%s)", path, strerror(errno));
 119                }
 120                break;
 121        default:
 122                free(new);
 123                return error("git-checkout-index: unknown file mode for %s", path);
 124        }
 125
 126        if (state->refresh_cache) {
 127                struct stat st;
 128                lstat(ce->name, &st);
 129                fill_stat_cache_info(ce, &st);
 130        }
 131        return 0;
 132}
 133
 134int checkout_entry(struct cache_entry *ce, struct checkout *state, char *topath)
 135{
 136        static char path[PATH_MAX + 1];
 137        struct stat st;
 138        int len = state->base_dir_len;
 139
 140        if (topath)
 141                return write_entry(ce, topath, state, 1);
 142
 143        memcpy(path, state->base_dir, len);
 144        strcpy(path + len, ce->name);
 145
 146        if (!lstat(path, &st)) {
 147                unsigned changed = ce_match_stat(ce, &st, 1);
 148                if (!changed)
 149                        return 0;
 150                if (!state->force) {
 151                        if (!state->quiet)
 152                                fprintf(stderr, "git-checkout-index: %s already exists\n", path);
 153                        return -1;
 154                }
 155
 156                /*
 157                 * We unlink the old file, to get the new one with the
 158                 * right permissions (including umask, which is nasty
 159                 * to emulate by hand - much easier to let the system
 160                 * just do the right thing)
 161                 */
 162                unlink(path);
 163                if (S_ISDIR(st.st_mode)) {
 164                        if (!state->force)
 165                                return error("%s is a directory", path);
 166                        remove_subtree(path);
 167                }
 168        } else if (state->not_new)
 169                return 0;
 170        create_directories(path, state);
 171        return write_entry(ce, path, state, 0);
 172}