preparation for idempotent checkout: read exisiting file index
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
diff --git a/lib/fileindex.c b/lib/fileindex.c
index a6bdde1..0eda1d0 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -28,16 +28,40 @@
#include "got_lib_fileindex.h"
const struct got_error *
-got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
- const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
- uint8_t *commit_sha1)
+got_fileindex_entry_update(struct got_fileindex_entry *entry,
+ const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1)
{
struct stat sb;
- size_t len;
if (lstat(ondisk_path, &sb) != 0)
return got_error_from_errno();
+ entry->ctime_sec = sb.st_ctime;
+ entry->ctime_nsec = sb.st_ctimensec;
+ entry->mtime_sec = sb.st_mtime;
+ entry->mtime_nsec = sb.st_mtimensec;
+ entry->uid = sb.st_uid;
+ entry->gid = sb.st_gid;
+ entry->size = (sb.st_size & 0xffffffff);
+ if (sb.st_mode & S_IFLNK)
+ entry->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
+ else
+ entry->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
+ entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
+ GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
+ memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
+ memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
+
+ return NULL;
+}
+
+const struct got_error *
+got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
+ const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
+ uint8_t *commit_sha1)
+{
+ size_t len;
+
*entry = calloc(1, sizeof(**entry));
if (*entry == NULL)
return got_error_from_errno();
@@ -49,28 +73,14 @@ got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
*entry = NULL;
return err;
}
-
- (*entry)->ctime_sec = sb.st_ctime;
- (*entry)->ctime_nsec = sb.st_ctimensec;
- (*entry)->mtime_sec = sb.st_mtime;
- (*entry)->mtime_nsec = sb.st_mtimensec;
- (*entry)->uid = sb.st_uid;
- (*entry)->gid = sb.st_gid;
- (*entry)->size = (sb.st_size & 0xffffffff);
- if (sb.st_mode & S_IFLNK)
- (*entry)->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
- else
- (*entry)->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
- (*entry)->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
- GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
- memcpy((*entry)->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
- memcpy((*entry)->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
+
len = strlen(relpath);
if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
len = GOT_INDEX_ENTRY_F_PATH_LEN;
(*entry)->flags |= len;
- return NULL;
+ return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
+ commit_sha1);
}
void
@@ -90,6 +100,18 @@ got_fileindex_entry_add(struct got_fileindex *fileindex,
return NULL;
}
+struct got_fileindex_entry *
+got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
+{
+ struct got_fileindex_entry *entry;
+ TAILQ_FOREACH(entry, &fileindex->entries, entry) {
+ if (strcmp(entry->path, path) == 0)
+ return entry;
+ }
+
+ return NULL;
+}
+
struct got_fileindex *
got_fileindex_alloc(void)
{
@@ -428,8 +450,11 @@ got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
SHA1Init(&ctx);
n = fread(buf, 1, len, infile);
- if (n != len)
+ if (n != len) {
+ if (n == 0) /* EOF */
+ return NULL;
return got_ferror(infile, GOT_ERR_IO);
+ }
SHA1Update(&ctx, buf, len);
diff --git a/lib/got_lib_fileindex.h b/lib/got_lib_fileindex.h
index e8716e8..78770a1 100644
--- a/lib/got_lib_fileindex.h
+++ b/lib/got_lib_fileindex.h
@@ -85,6 +85,8 @@ struct got_fileindex_hdr {
uint8_t sha1[SHA1_DIGEST_LENGTH]; /* checksum of above on-disk data */
};
+const struct got_error *got_fileindex_entry_update(struct got_fileindex_entry *,
+ const char *, uint8_t *, uint8_t *);
const struct got_error *got_fileindex_entry_alloc(struct got_fileindex_entry **,
const char *, const char *, uint8_t *, uint8_t *);
void got_fileindex_entry_free(struct got_fileindex_entry *);
@@ -93,4 +95,6 @@ void got_fileindex_free(struct got_fileindex *);
const struct got_error *got_fileindex_write(struct got_fileindex *, FILE *);
const struct got_error *got_fileindex_entry_add(struct got_fileindex *,
struct got_fileindex_entry *);
+struct got_fileindex_entry *got_fileindex_entry_get(struct got_fileindex *,
+ const char *);
const struct got_error *got_fileindex_read(struct got_fileindex *, FILE *);
diff --git a/lib/worktree.c b/lib/worktree.c
index 65f2e53..f6afe82 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -463,13 +463,19 @@ add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
fsync(fd);
- err = got_fileindex_entry_alloc(&entry, ondisk_path,
- apply_path_prefix(worktree, path), blob->id.sha1,
- worktree->base_commit_id->sha1);
- if (err)
- goto done;
-
- err = got_fileindex_entry_add(fileindex, entry);
+ entry = got_fileindex_entry_get(fileindex,
+ apply_path_prefix(worktree, path));
+ if (entry)
+ err = got_fileindex_entry_update(entry, ondisk_path,
+ blob->id.sha1, worktree->base_commit_id->sha1);
+ else {
+ err = got_fileindex_entry_alloc(&entry, ondisk_path,
+ apply_path_prefix(worktree, path), blob->id.sha1,
+ worktree->base_commit_id->sha1);
+ if (err)
+ goto done;
+ err = got_fileindex_entry_add(fileindex, entry);
+ }
if (err)
goto done;
done:
@@ -635,7 +641,7 @@ got_worktree_checkout_files(struct got_worktree *worktree,
struct got_tree_object *tree = NULL;
char *fileindex_path = NULL, *new_fileindex_path = NULL;
struct got_fileindex *fileindex = NULL;
- FILE *new_index = NULL;
+ FILE *index = NULL, *new_index = NULL;
err = lock_worktree(worktree, LOCK_EX);
if (err)
@@ -654,6 +660,21 @@ got_worktree_checkout_files(struct got_worktree *worktree,
goto done;
}
+ /*
+ * Read the file index.
+ * Checking out files is supposed to be an idempotent operation.
+ * If the on-disk file index is incomplete we will try to complete it.
+ */
+ index = fopen(fileindex_path, "rb");
+ if (index == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ err = got_fileindex_read(fileindex, index);
+ fclose(index);
+ if (err)
+ goto done;
+
err = got_opentemp_named(&new_fileindex_path, &new_index,
fileindex_path);
if (err)