add function to read worktree meta data file
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
diff --git a/include/got_error.h b/include/got_error.h
index 128e60e..cd99056 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -40,6 +40,7 @@
#define GOT_ERR_COMPRESSION 22
#define GOT_ERR_BAD_OBJ_ID_STR 23
#define GOT_ERR_WORKTREE_EXISTS 26
+#define GOT_ERR_WORKTREE_META 27
static const struct got_error {
int code;
@@ -70,6 +71,7 @@ static const struct got_error {
{ GOT_ERR_COMPRESSION, "compression failed" },
{ GOT_ERR_BAD_OBJ_ID_STR,"bad object id string" },
{ GOT_ERR_WORKTREE_EXISTS,"worktree already exists" },
+ { GOT_ERR_WORKTREE_META,"bad worktree meta data" },
};
const struct got_error * got_error(int code);
diff --git a/lib/worktree.c b/lib/worktree.c
index 986dd17..b05a2c2 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -76,6 +76,58 @@ done:
return err;
}
+static const struct got_error *
+read_meta_file(char **content, const char *gotpath, const char *name)
+{
+ const struct got_error *err = NULL;
+ char *path;
+ int fd = -1;
+ ssize_t n;
+ struct stat sb;
+
+ *content = NULL;
+
+ if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
+ err = got_error(GOT_ERR_NO_MEM);
+ path = NULL;
+ goto done;
+ }
+
+ fd = open(path, O_RDONLY | O_EXCL | O_EXLOCK | O_NOFOLLOW);
+ if (fd == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ stat(path, &sb);
+ *content = calloc(1, sb.st_size);
+ if (*content == NULL) {
+ err = got_error(GOT_ERR_NO_MEM);
+ goto done;
+ }
+
+ n = read(fd, *content, sb.st_size);
+ if (n != sb.st_size) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ if ((*content)[sb.st_size - 1] != '\n') {
+ err = got_error(GOT_ERR_WORKTREE_META);
+ goto done;
+ }
+ (*content)[sb.st_size - 1] = '\0';
+
+done:
+ if (fd != -1 && close(fd) == -1 && err == NULL)
+ err = got_error_from_errno();
+ free(path);
+ if (err) {
+ free(*content);
+ *content = NULL;
+ }
+ return err;
+}
+
const struct got_error *
got_worktree_init(const char *path, struct got_reference *head_ref,
const char *prefix, struct got_repository *repo)