Commit 26a7fe28be950a17dccf0bc3f8bd8f4ad057468c

Stefan Sperling 2019-07-27T16:42:04

add conversion functions for fileindex/stat permission bits

diff --git a/lib/fileindex.c b/lib/fileindex.c
index f3934b9..54d3549 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -51,6 +51,20 @@ struct got_fileindex {
 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
 };
 
+uint16_t
+got_fileindex_perms_from_st(struct stat *sb)
+{
+	uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
+	return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
+}
+
+mode_t
+got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
+{
+	mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
+	return (perms & (S_IRWXU | S_IRWXG | S_IRWXO));
+}
+
 const struct got_error *
 got_fileindex_entry_update(struct got_fileindex_entry *entry,
     const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
@@ -82,8 +96,7 @@ got_fileindex_entry_update(struct got_fileindex_entry *entry,
 			entry->mode = GOT_FILEIDX_MODE_SYMLINK;
 		else
 			entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
-		entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
-		    GOT_FILEIDX_MODE_PERMS_SHIFT);
+		entry->mode |= got_fileindex_perms_from_st(&sb);
 	}
 
 	if (blob_sha1) {
diff --git a/lib/got_lib_fileindex.h b/lib/got_lib_fileindex.h
index c3528c7..3e7fc83 100644
--- a/lib/got_lib_fileindex.h
+++ b/lib/got_lib_fileindex.h
@@ -89,6 +89,9 @@ struct got_fileindex_hdr {
 	uint8_t sha1[SHA1_DIGEST_LENGTH]; /* checksum of above on-disk data */
 };
 
+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);
 const struct got_error *got_fileindex_entry_alloc(struct got_fileindex_entry **,
diff --git a/lib/worktree.c b/lib/worktree.c
index 08d2a40..5368b2c 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1055,9 +1055,7 @@ get_file_status(unsigned char *status, struct stat *sb,
 					*status = GOT_STATUS_MISSING;
 				else
 					*status = GOT_STATUS_DELETE;
-				sb->st_mode =
-				    ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
-				    & (S_IRWXU | S_IRWXG | S_IRWXO));
+				sb->st_mode = got_fileindex_perms_to_st(ie);
 			} else
 				sb->st_mode = GOT_DEFAULT_FILE_MODE;
 			return NULL;