give bad symlinks a dedicated file type in the 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
diff --git a/lib/fileindex.c b/lib/fileindex.c
index f15a1b2..13820ca 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -178,6 +178,29 @@ got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
}
int
+got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
+{
+ return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE);
+}
+
+const struct got_error *
+got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
+{
+ switch (type) {
+ case GOT_FILEIDX_MODE_REGULAR_FILE:
+ case GOT_FILEIDX_MODE_SYMLINK:
+ case GOT_FILEIDX_MODE_BAD_SYMLINK:
+ break;
+ default:
+ return got_error(GOT_ERR_BAD_FILETYPE);
+ }
+
+ ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE;
+ ie->mode |= type;
+ return NULL;
+}
+
+int
got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
{
return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
diff --git a/lib/got_lib_fileindex.h b/lib/got_lib_fileindex.h
index af6efe8..31f1e04 100644
--- a/lib/got_lib_fileindex.h
+++ b/lib/got_lib_fileindex.h
@@ -39,6 +39,7 @@ struct got_fileindex_entry {
#define GOT_FILEIDX_MODE_FILE_TYPE 0x000f
#define GOT_FILEIDX_MODE_REGULAR_FILE 1
#define GOT_FILEIDX_MODE_SYMLINK 2
+#define GOT_FILEIDX_MODE_BAD_SYMLINK 3
#define GOT_FILEIDX_MODE_PERMS 0xfff0
#define GOT_FILEIDX_MODE_PERMS_SHIFT 4
@@ -161,5 +162,8 @@ int got_fileindex_entry_has_commit(struct got_fileindex_entry *);
int got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *);
uint32_t got_fileindex_entry_stage_get(const struct got_fileindex_entry *);
void got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t);
+int got_fileindex_entry_filetype_get(struct got_fileindex_entry *);
+const struct got_error *got_fileindex_entry_filetype_set(
+ struct got_fileindex_entry *, int);
void got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *);
diff --git a/lib/worktree.c b/lib/worktree.c
index 7bb6476..5abfa0d 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1197,10 +1197,10 @@ replace_existing_symlink(const char *ondisk_path, const char *target_path,
}
static const struct got_error *
-install_symlink(struct got_worktree *worktree, const char *ondisk_path,
- const char *path, mode_t te_mode, mode_t st_mode,
- struct got_blob_object *blob, int restoring_missing_file,
- int reverting_versioned_file, struct got_repository *repo,
+install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
+ const char *ondisk_path, const char *path, struct got_blob_object *blob,
+ int restoring_missing_file, int reverting_versioned_file,
+ struct got_repository *repo,
got_worktree_checkout_cb progress_cb, void *progress_arg)
{
const struct got_error *err = NULL;
@@ -1211,6 +1211,8 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
const uint8_t *buf = got_object_blob_get_read_buf(blob);
size_t hdrlen = got_object_blob_get_hdrlen(blob);
+ *is_bad_symlink = 0;
+
/*
* Blob object content specifies the target path of the link.
* If a symbolic link cannot be installed we instead create
@@ -1221,6 +1223,7 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
err = got_object_blob_read_block(&len, blob);
if (len + target_len >= sizeof(target_path)) {
/* Path too long; install as a regular file. */
+ *is_bad_symlink = 1;
got_object_blob_rewind(blob);
return install_blob(worktree, ondisk_path, path,
GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
@@ -1271,6 +1274,7 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
abspath : target_path), worktree->root_path,
strlen(worktree->root_path))) {
/* install as a regular file */
+ *is_bad_symlink = 1;
got_object_blob_rewind(blob);
err = install_blob(worktree, ondisk_path, path,
GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
@@ -1288,6 +1292,7 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
abspath : target_path), path_got, strlen(path_got))) {
/* install as a regular file */
+ *is_bad_symlink = 1;
got_object_blob_rewind(blob);
err = install_blob(worktree, ondisk_path, path,
GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
@@ -1332,6 +1337,7 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
/* Handle errors from first or second creation attempt. */
if (errno == ENAMETOOLONG) {
/* bad target path; install as a regular file */
+ *is_bad_symlink = 1;
got_object_blob_rewind(blob);
err = install_blob(worktree, ondisk_path, path,
GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
@@ -1367,11 +1373,6 @@ install_blob(struct got_worktree *worktree, const char *ondisk_path,
int update = 0;
char *tmppath = NULL;
- if (S_ISLNK(te_mode))
- return install_symlink(worktree, ondisk_path, path, te_mode,
- st_mode, blob, restoring_missing_file,
- reverting_versioned_file, repo, progress_cb, progress_arg);
-
fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
GOT_DEFAULT_FILE_MODE);
if (fd == -1) {
@@ -1883,9 +1884,18 @@ update_blob(struct got_worktree *worktree,
if (err)
goto done;
} else {
- err = install_blob(worktree, ondisk_path, path, te->mode,
- sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
- repo, progress_cb, progress_arg);
+ int is_bad_symlink = 0;
+ if (S_ISLNK(te->mode)) {
+ err = install_symlink(&is_bad_symlink, worktree,
+ ondisk_path, path, blob,
+ status == GOT_STATUS_MISSING, 0, repo,
+ progress_cb, progress_arg);
+ } else {
+ err = install_blob(worktree, ondisk_path, path,
+ te->mode, sb.st_mode, blob,
+ status == GOT_STATUS_MISSING, 0, 0, repo,
+ progress_cb, progress_arg);
+ }
if (err)
goto done;
if (ie) {
@@ -1898,6 +1908,15 @@ update_blob(struct got_worktree *worktree,
}
if (err)
goto done;
+ if (is_bad_symlink) {
+ if (ie == NULL)
+ ie = got_fileindex_entry_get(fileindex, path,
+ strlen(path));
+ err = got_fileindex_entry_filetype_set(ie,
+ GOT_FILEIDX_MODE_BAD_SYMLINK);
+ if (err)
+ goto done;
+ }
}
got_object_blob_close(blob);
done:
@@ -2742,10 +2761,17 @@ merge_file_cb(void *arg, struct got_blob_object *blob1,
goto done;
}
} else {
+ int is_bad_symlink = 0;
sb.st_mode = GOT_DEFAULT_FILE_MODE;
- err = install_blob(a->worktree, ondisk_path, path2,
- mode2, sb.st_mode, blob2, 0, 0, 0, repo,
- a->progress_cb, a->progress_arg);
+ if (S_ISLNK(mode2)) {
+ err = install_symlink(&is_bad_symlink,
+ a->worktree, ondisk_path, path2, blob2, 0,
+ 0, repo, a->progress_cb, a->progress_arg);
+ } else {
+ err = install_blob(a->worktree, ondisk_path, path2,
+ mode2, sb.st_mode, blob2, 0, 0, 0, repo,
+ a->progress_cb, a->progress_arg);
+ }
if (err)
goto done;
err = got_fileindex_entry_alloc(&ie, path2);
@@ -2762,6 +2788,12 @@ merge_file_cb(void *arg, struct got_blob_object *blob1,
got_fileindex_entry_free(ie);
goto done;
}
+ if (is_bad_symlink) {
+ err = got_fileindex_entry_filetype_set(ie,
+ GOT_FILEIDX_MODE_BAD_SYMLINK);
+ if (err)
+ goto done;
+ }
}
}
done:
@@ -4256,10 +4288,19 @@ revert_file(void *arg, unsigned char status, unsigned char staged_status,
goto done;
}
} else {
- err = install_blob(a->worktree, ondisk_path, ie->path,
- te ? te->mode : GOT_DEFAULT_FILE_MODE,
- got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
- a->repo, a->progress_cb, a->progress_arg);
+ int is_bad_symlink = 0;
+ if (te && S_ISLNK(te->mode)) {
+ err = install_symlink(&is_bad_symlink,
+ a->worktree, ondisk_path, ie->path,
+ blob, 0, 1, a->repo,
+ a->progress_cb, a->progress_arg);
+ } else {
+ err = install_blob(a->worktree, ondisk_path,
+ ie->path,
+ te ? te->mode : GOT_DEFAULT_FILE_MODE,
+ got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
+ a->repo, a->progress_cb, a->progress_arg);
+ }
if (err)
goto done;
if (status == GOT_STATUS_DELETE ||
@@ -4270,6 +4311,12 @@ revert_file(void *arg, unsigned char status, unsigned char staged_status,
if (err)
goto done;
}
+ if (is_bad_symlink) {
+ err = got_fileindex_entry_filetype_set(ie,
+ GOT_FILEIDX_MODE_BAD_SYMLINK);
+ if (err)
+ goto done;
+ }
}
break;
}
@@ -4919,6 +4966,106 @@ done:
}
static const struct got_error *
+reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
+ struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
+ struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ struct got_blob_object *blob = NULL;
+ struct got_object_id *tree_id = NULL;
+ char *tree_path = NULL;
+ struct got_tree_object *tree = NULL;
+ struct got_tree_entry *te;
+ char *entry_name;
+
+ err = got_object_open_as_blob(&blob, repo, ct->blob_id, PATH_MAX);
+ if (err)
+ return err;
+
+ err = got_path_dirname(&tree_path, ct->in_repo_path);
+ if (err) {
+ if (err->code != GOT_ERR_BAD_PATH)
+ goto done;
+ err = got_object_id_by_path(&tree_id, repo,
+ new_base_commit_id, "");
+ if (err)
+ goto done;
+ } else {
+ err = got_object_id_by_path(&tree_id, repo,
+ new_base_commit_id, tree_path);
+ if (err)
+ goto done;
+ }
+
+ err = got_object_open_as_tree(&tree, repo, tree_id);
+ if (err)
+ goto done;
+
+ entry_name = basename(ct->path);
+ if (entry_name == NULL) {
+ err = got_error_from_errno2("basename", ct->path);
+ goto done;
+ }
+
+ te = got_object_tree_find_entry(tree, entry_name);
+ if (te == NULL) {
+ err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
+ goto done;
+ }
+
+ err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
+ ct->path, blob, 0, 0, repo, NULL, NULL);
+done:
+ if (blob)
+ got_object_blob_close(blob);
+ if (tree)
+ got_object_tree_close(tree);
+ free(tree_id);
+ free(tree_path);
+ return err;
+}
+
+/*
+ * After comitting a symlink we have a chance to convert "bad" symlinks
+ * (those which point outside the work tree or into .got) to regular files.
+ * This way, the post-commit work tree state matches a fresh checkout of
+ * the tree which was just committed. We also mark such newly committed
+ * symlinks as "bad" in the work tree's fileindex.
+ */
+static const struct got_error *
+reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
+ struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
+ struct got_worktree *worktree, struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ struct got_pathlist_entry *pe;
+
+ TAILQ_FOREACH(pe, commitable_paths, entry) {
+ struct got_commitable *ct = pe->data;
+ struct got_fileindex_entry *ie;
+ int is_bad_symlink = 0;
+
+ if (!S_ISLNK(get_ct_file_mode(ct)))
+ continue;
+
+ err = reinstall_symlink_after_commit(&is_bad_symlink,
+ ct, new_base_commit_id, worktree, repo);
+ if (err)
+ break;
+ ie = got_fileindex_entry_get(fileindex, ct->path,
+ strlen(ct->path));
+ if (ie && is_bad_symlink) {
+ err = got_fileindex_entry_filetype_set(ie,
+ GOT_FILEIDX_MODE_BAD_SYMLINK);
+ if (err)
+ break;
+ }
+ }
+
+ return err;
+}
+
+static const struct got_error *
update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
int have_staged_files)
@@ -5074,35 +5221,9 @@ commit_worktree(struct got_object_id **new_commit_id,
goto done;
}
err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
- if (err) {
- free(ondisk_path);
- goto done;
- }
-
- /*
- * When comitting a symlink we convert "bad" symlinks (those
- * which point outside the work tree or into .got) to regular
- * files. This way, the post-commit work tree state matches
- * a fresh checkout of the tree which was committed.
- */
- if (S_ISLNK(get_ct_file_mode(ct))) {
- struct got_blob_object *blob;
- err = got_object_open_as_blob(&blob, repo, ct->blob_id,
- PATH_MAX);
- if (err) {
- free(ondisk_path);
- goto done;
- }
- err = install_symlink(worktree, ondisk_path, ct->path,
- get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
- 0, 0, repo, NULL, NULL);
- got_object_blob_close(blob);
- if (err) {
- free(ondisk_path);
- goto done;
- }
- }
free(ondisk_path);
+ if (err)
+ goto done;
}
/* Recursively write new tree objects. */
@@ -5321,6 +5442,10 @@ got_worktree_commit(struct got_object_id **new_commit_id,
err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
fileindex, have_staged_files);
+ if (err == NULL) {
+ err = reinstall_symlinks_after_commit(&commitable_paths,
+ *new_commit_id, fileindex, worktree, repo);
+ }
sync_err = sync_fileindex(fileindex, fileindex_path);
if (sync_err && err == NULL)
err = sync_err;