add fd field to got_worktree, modify got_fileindex_entry_update to use fds These changes are intended to make got more compatible with FreeBSD's Capsicum. ok stsp
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
diff --git a/lib/fileindex.c b/lib/fileindex.c
index 04443aa..5796af5 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -86,15 +86,15 @@ got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
const struct got_error *
got_fileindex_entry_update(struct got_fileindex_entry *ie,
- const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
- int update_timestamps)
+ int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
+ uint8_t *commit_sha1, int update_timestamps)
{
struct stat sb;
- if (lstat(ondisk_path, &sb) != 0) {
+ if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
errno == ENOENT))
- return got_error_from_errno2("lstat", ondisk_path);
+ return got_error_from_errno2("fstatat", ondisk_path);
sb.st_mode = GOT_DEFAULT_FILE_MODE;
} else {
if (sb.st_mode & S_IFDIR)
diff --git a/lib/got_lib_fileindex.h b/lib/got_lib_fileindex.h
index f100583..537374d 100644
--- a/lib/got_lib_fileindex.h
+++ b/lib/got_lib_fileindex.h
@@ -108,7 +108,7 @@ uint16_t got_fileindex_perms_from_st(struct stat *);
mode_t got_fileindex_perms_to_st(struct got_fileindex_entry *);
const struct got_error *got_fileindex_entry_update(struct got_fileindex_entry *,
- const char *, uint8_t *, uint8_t *, int);
+ int, const char *, uint8_t *, uint8_t *, int);
const struct got_error *got_fileindex_entry_alloc(struct got_fileindex_entry **,
const char *);
void got_fileindex_entry_free(struct got_fileindex_entry *);
diff --git a/lib/got_lib_worktree.h b/lib/got_lib_worktree.h
index 067bac2..651bcc8 100644
--- a/lib/got_lib_worktree.h
+++ b/lib/got_lib_worktree.h
@@ -17,6 +17,7 @@
struct got_worktree {
char *root_path;
char *repo_path;
+ int root_fd;
char *path_prefix;
struct got_object_id *base_commit_id;
char *head_ref_name;
diff --git a/lib/worktree.c b/lib/worktree.c
index 971955f..91a87ba 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -429,6 +429,12 @@ open_worktree(struct got_worktree **worktree, const char *path)
err = got_gotconfig_read(&(*worktree)->gotconfig,
(*worktree)->gotconfig_path);
+
+ (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
+ if ((*worktree)->root_fd == -1) {
+ err = got_error_from_errno2("open", (*worktree)->root_path);
+ goto done;
+ }
done:
if (repo)
got_repo_close(repo);
@@ -505,6 +511,7 @@ got_worktree_close(struct got_worktree *worktree)
free(worktree->gotconfig_path);
got_gotconfig_free(worktree->gotconfig);
free(worktree);
+ close(worktree->root_fd);
return err;
}
@@ -1177,7 +1184,7 @@ done:
static const struct got_error *
create_fileindex_entry(struct got_fileindex_entry **new_iep,
struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
- const char *ondisk_path, const char *path, struct got_object_id *blob_id)
+ int wt_fd, const char *path, struct got_object_id *blob_id)
{
const struct got_error *err = NULL;
struct got_fileindex_entry *new_ie;
@@ -1188,7 +1195,7 @@ create_fileindex_entry(struct got_fileindex_entry **new_iep,
if (err)
return err;
- err = got_fileindex_entry_update(new_ie, ondisk_path,
+ err = got_fileindex_entry_update(new_ie, wt_fd, path,
blob_id->sha1, base_commit_id->sha1, 1);
if (err)
goto done;
@@ -1863,11 +1870,11 @@ done:
* we had to run a full content comparison to find out.
*/
static const struct got_error *
-sync_timestamps(char *ondisk_path, unsigned char status,
+sync_timestamps(int wt_fd, const char *path, unsigned char status,
struct got_fileindex_entry *ie, struct stat *sb)
{
if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
- return got_fileindex_entry_update(ie, ondisk_path,
+ return got_fileindex_entry_update(ie, wt_fd, path,
ie->blob_sha1, ie->commit_sha1, 1);
return NULL;
@@ -1920,7 +1927,8 @@ update_blob(struct got_worktree *worktree,
if (got_fileindex_entry_has_commit(ie) &&
memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
SHA1_DIGEST_LENGTH) == 0) {
- err = sync_timestamps(ondisk_path, status, ie, &sb);
+ err = sync_timestamps(worktree->root_fd,
+ path, status, ie, &sb);
if (err)
goto done;
err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
@@ -1930,7 +1938,8 @@ update_blob(struct got_worktree *worktree,
if (got_fileindex_entry_has_blob(ie) &&
memcmp(ie->blob_sha1, te->id.sha1,
SHA1_DIGEST_LENGTH) == 0) {
- err = sync_timestamps(ondisk_path, status, ie, &sb);
+ err = sync_timestamps(worktree->root_fd,
+ path, status, ie, &sb);
goto done;
}
}
@@ -1989,17 +1998,17 @@ update_blob(struct got_worktree *worktree,
* Otherwise, a future status walk would treat them as
* unmodified files again.
*/
- err = got_fileindex_entry_update(ie, ondisk_path,
+ err = got_fileindex_entry_update(ie, worktree->root_fd, path,
blob->id.sha1, worktree->base_commit_id->sha1,
update_timestamps);
} else if (status == GOT_STATUS_MODE_CHANGE) {
- err = got_fileindex_entry_update(ie, ondisk_path,
+ err = got_fileindex_entry_update(ie, worktree->root_fd, path,
blob->id.sha1, worktree->base_commit_id->sha1, 0);
} else if (status == GOT_STATUS_DELETE) {
err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
if (err)
goto done;
- err = got_fileindex_entry_update(ie, ondisk_path,
+ err = got_fileindex_entry_update(ie, worktree->root_fd, path,
blob->id.sha1, worktree->base_commit_id->sha1, 0);
if (err)
goto done;
@@ -2022,11 +2031,12 @@ update_blob(struct got_worktree *worktree,
goto done;
if (ie) {
- err = got_fileindex_entry_update(ie, ondisk_path,
- blob->id.sha1, worktree->base_commit_id->sha1, 1);
+ err = got_fileindex_entry_update(ie,
+ worktree->root_fd, path, blob->id.sha1,
+ worktree->base_commit_id->sha1, 1);
} else {
err = create_fileindex_entry(&ie, fileindex,
- worktree->base_commit_id, ondisk_path, path,
+ worktree->base_commit_id, worktree->root_fd, path,
&blob->id);
}
if (err)
@@ -2126,8 +2136,8 @@ delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
* Preserve the working file and change the deleted blob's
* entry into a schedule-add entry.
*/
- err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
- 0);
+ err = got_fileindex_entry_update(ie, worktree->root_fd,
+ ie->path, NULL, NULL, 0);
} else {
err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
if (err)
@@ -2935,7 +2945,7 @@ merge_file_cb(void *arg, struct got_blob_object *blob1,
goto done;
if (status == GOT_STATUS_DELETE) {
err = got_fileindex_entry_update(ie,
- ondisk_path, blob2->id.sha1,
+ a->worktree->root_fd, path2, blob2->id.sha1,
a->worktree->base_commit_id->sha1, 0);
if (err)
goto done;
@@ -2957,8 +2967,8 @@ merge_file_cb(void *arg, struct got_blob_object *blob1,
err = got_fileindex_entry_alloc(&ie, path2);
if (err)
goto done;
- err = got_fileindex_entry_update(ie, ondisk_path,
- NULL, NULL, 1);
+ err = got_fileindex_entry_update(ie,
+ a->worktree->root_fd, path2, NULL, NULL, 1);
if (err) {
got_fileindex_entry_free(ie);
goto done;
@@ -3777,7 +3787,8 @@ schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
err = got_fileindex_entry_alloc(&ie, relpath);
if (err)
goto done;
- err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
+ err = got_fileindex_entry_update(ie, a->worktree->root_fd,
+ relpath, NULL, NULL, 1);
if (err) {
got_fileindex_entry_free(ie);
goto done;
@@ -4584,7 +4595,8 @@ revert_file(void *arg, unsigned char status, unsigned char staged_status,
if (status == GOT_STATUS_DELETE ||
status == GOT_STATUS_MODE_CHANGE) {
err = got_fileindex_entry_update(ie,
- ondisk_path, blob->id.sha1,
+ a->worktree->root_fd, relpath,
+ blob->id.sha1,
a->worktree->base_commit_id->sha1, 1);
if (err)
goto done;
@@ -5287,18 +5299,26 @@ done:
}
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)
+update_fileindex_after_commit(struct got_worktree *worktree,
+ struct got_pathlist_head *commitable_paths,
+ struct got_object_id *new_base_commit_id,
+ struct got_fileindex *fileindex, int have_staged_files)
{
const struct got_error *err = NULL;
struct got_pathlist_entry *pe;
+ char *relpath = NULL;
TAILQ_FOREACH(pe, commitable_paths, entry) {
struct got_fileindex_entry *ie;
struct got_commitable *ct = pe->data;
ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
+
+ err = got_path_skip_common_ancestor(&relpath,
+ worktree->root_path, ct->ondisk_path);
+ if (err)
+ goto done;
+
if (ie) {
if (ct->status == GOT_STATUS_DELETE ||
ct->staged_status == GOT_STATUS_DELETE) {
@@ -5308,32 +5328,40 @@ update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
got_fileindex_entry_stage_set(ie,
GOT_FILEIDX_STAGE_NONE);
got_fileindex_entry_staged_filetype_set(ie, 0);
+
err = got_fileindex_entry_update(ie,
- ct->ondisk_path, ct->staged_blob_id->sha1,
+ worktree->root_fd, relpath,
+ ct->staged_blob_id->sha1,
new_base_commit_id->sha1,
!have_staged_files);
} else
err = got_fileindex_entry_update(ie,
- ct->ondisk_path, ct->blob_id->sha1,
+ worktree->root_fd, relpath,
+ ct->blob_id->sha1,
new_base_commit_id->sha1,
!have_staged_files);
} else {
err = got_fileindex_entry_alloc(&ie, pe->path);
if (err)
- break;
- err = got_fileindex_entry_update(ie, ct->ondisk_path,
- ct->blob_id->sha1, new_base_commit_id->sha1, 1);
+ goto done;
+ err = got_fileindex_entry_update(ie,
+ worktree->root_fd, relpath, ct->blob_id->sha1,
+ new_base_commit_id->sha1, 1);
if (err) {
got_fileindex_entry_free(ie);
- break;
+ goto done;
}
err = got_fileindex_entry_add(fileindex, ie);
if (err) {
got_fileindex_entry_free(ie);
- break;
+ goto done;
}
}
+ free(relpath);
+ relpath = NULL;
}
+done:
+ free(relpath);
return err;
}
@@ -5664,8 +5692,8 @@ got_worktree_commit(struct got_object_id **new_commit_id,
if (err)
goto done;
- err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
- fileindex, have_staged_files);
+ err = update_fileindex_after_commit(worktree, &commitable_paths,
+ *new_commit_id, fileindex, have_staged_files);
sync_err = sync_fileindex(fileindex, fileindex_path);
if (sync_err && err == NULL)
err = sync_err;
@@ -6250,8 +6278,8 @@ rebase_commit(struct got_object_id **new_commit_id,
if (err)
goto done;
- err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
- fileindex, 0);
+ err = update_fileindex_after_commit(worktree, &commitable_paths,
+ *new_commit_id, fileindex, 0);
sync_err = sync_fileindex(fileindex, fileindex_path);
if (sync_err && err == NULL)
err = sync_err;