implement fileindex entry open/close
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
diff --git a/lib/fileindex.c b/lib/fileindex.c
new file mode 100644
index 0000000..653a6b1
--- /dev/null
+++ b/lib/fileindex.c
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/queue.h>
+#include <sys/stat.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sha1.h>
+
+#include "got_error.h"
+
+#include "got_fileindex_priv.h"
+
+const struct got_error *
+got_fileindex_entry_open(struct got_fileindex_entry **entry, const char *path,
+ uint8_t *blob_sha1)
+{
+ struct stat sb;
+ size_t len;
+
+ if (lstat(path, &sb) != 0)
+ return got_error_from_errno();
+
+ *entry = calloc(1, sizeof(**entry));
+ if (*entry == NULL)
+ return got_error(GOT_ERR_NO_MEM);
+
+ (*entry)->path = strdup(path);
+ if ((*entry)->path == NULL) {
+ free(*entry);
+ *entry = NULL;
+ return got_error(GOT_ERR_NO_MEM);
+ }
+
+ (*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);
+ len = strlen(path);
+ if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
+ len = GOT_INDEX_ENTRY_F_PATH_LEN;
+ (*entry)->flags |= len;
+
+ return NULL;
+}
+
+void
+got_fileindex_entry_close(struct got_fileindex_entry *entry)
+{
+ free(entry->path);
+ free(entry);
+}
diff --git a/lib/got_fileindex_priv.h b/lib/got_fileindex_priv.h
index 67b91d8..732e8af 100644
--- a/lib/got_fileindex_priv.h
+++ b/lib/got_fileindex_priv.h
@@ -22,7 +22,7 @@
* applied back to the filesystem.
*/
struct got_fileindex_entry {
- TAILQ_ENTRY(, got_fileindex_entry) entry;
+ TAILQ_ENTRY(got_fileindex_entry) entry;
uint64_t ctime_sec;
uint64_t ctime_nsec;
uint64_t mtime_sec;
@@ -34,9 +34,13 @@ struct got_fileindex_entry {
* The value is only used to check for modifications anyway.
*/
uint32_t size;
+
uint16_t mode;
-#define GOT_INDEX_ENTRY_MODE_OBJ_TYPE 0x000f
-#define GOT_INDEX_ENTRY_MODE_PERMS 0xff10
+#define GOT_INDEX_ENTRY_MODE_FILE_TYPE 0x000f
+#define GOT_INDEX_ENTRY_MODE_REGULAR_FILE 1
+#define GOT_INDEX_ENTRY_MODE_SYMLINK 2
+#define GOT_INDEX_ENTRY_MODE_PERMS 0xff10
+#define GOT_INDEX_ENTRY_MODE_PERMS_SHIFT 4
/* SHA1 of corresponding blob in repository. */
uint8_t blob_sha1[SHA1_DIGEST_LENGTH];
@@ -51,7 +55,7 @@ struct got_fileindex_entry {
* UNIX-style path, relative to work tree root.
* Variable length, and NUL-padded to a multiple of 8 on disk.
*/
- const char *path;
+ char *path;
};
/* "Stages" of a file afflicted by a 3-way merge conflict. */
@@ -64,6 +68,7 @@ struct got_fileindex_entry {
struct got_fileindex_hdr {
uint32_t signature; /* big-endian on disk */
uint32_t version; /* big-endian on disk */
+#define GOT_FILE_INDEX_VERSION 1
uint32_t nentries; /* big-endian on disk */
TAILQ_HEAD(, got_fileindex_entry) entries;
uint8_t sha1[SHA1_DIGEST_LENGTH]; /* checksum of above on-disk data */
diff --git a/regress/worktree/Makefile b/regress/worktree/Makefile
index 5d751ac..cb4b123 100644
--- a/regress/worktree/Makefile
+++ b/regress/worktree/Makefile
@@ -2,7 +2,7 @@
PROG = worktree_test
SRCS = worktree.c repository.c object.c path.c error.c refs.c sha1.c pack.c \
- delta.c zbuf.c worktree_test.c
+ delta.c zbuf.c fileindex.c worktree_test.c
CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib
LDADD = -lutil -lz