dir: make untracked cache extension hash size independent
Instead of using a struct with a flex array member to read and write the untracked cache extension, use a shorter, fixed-length struct and add the name and hash data explicitly. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Feb 19, 2019 at 00:05 UTC
3899b88b49c03ae76f8834a277cbd45bc6bde830
1 file changed
+14
-14
dir.c
+14
-14
@@ -2545,13 +2545,9 @@ struct ondisk_untracked_cache {
2545
struct stat_data info_exclude_stat;
2546
struct stat_data excludes_file_stat;
2547
uint32_t dir_flags;
2548
- unsigned char info_exclude_sha1[20];
2549
- unsigned char excludes_file_sha1[20];
2550
- char exclude_per_dir[FLEX_ARRAY];
2548
};
2549
2550
#define ouc_offset(x) offsetof(struct ondisk_untracked_cache, x)
2554
-#define ouc_size(len) (ouc_offset(exclude_per_dir) + len + 1)
2551
2552
struct write_data {
2553
int index; /* number of written untracked_cache_dir */
@@ -2634,20 +2630,21 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untra
2630
struct write_data wd;
2631
unsigned char varbuf[16];
2632
int varint_len;
2637
- size_t len = strlen(untracked->exclude_per_dir);
2633
+ const unsigned hashsz = the_hash_algo->rawsz;
2634
2639
- FLEX_ALLOC_MEM(ouc, exclude_per_dir, untracked->exclude_per_dir, len);
2635
+ ouc = xcalloc(1, sizeof(*ouc));
2636
stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);
2637
stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);
2642
- hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.oid.hash);
2643
- hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.oid.hash);
2638
ouc->dir_flags = htonl(untracked->dir_flags);
2639
2640
varint_len = encode_varint(untracked->ident.len, varbuf);
2641
strbuf_add(out, varbuf, varint_len);
2642
strbuf_addbuf(out, &untracked->ident);
2643
2650
- strbuf_add(out, ouc, ouc_size(len));
2644
+ strbuf_add(out, ouc, sizeof(*ouc));
2645
+ strbuf_add(out, untracked->ss_info_exclude.oid.hash, hashsz);
2646
+ strbuf_add(out, untracked->ss_excludes_file.oid.hash, hashsz);
2647
+ strbuf_add(out, untracked->exclude_per_dir, strlen(untracked->exclude_per_dir) + 1);
2648
FREE_AND_NULL(ouc);
2649
2650
if (!untracked->root) {
@@ -2834,6 +2831,9 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
2831
int ident_len;
2832
ssize_t len;
2833
const char *exclude_per_dir;
2834
+ const unsigned hashsz = the_hash_algo->rawsz;
2835
+ const unsigned offset = sizeof(struct ondisk_untracked_cache);
2836
+ const unsigned exclude_per_dir_offset = offset + 2 * hashsz;
2837
2838
if (sz <= 1 || end[-1] != '\0')
2839
return NULL;
@@ -2845,7 +2845,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
2845
ident = (const char *)next;
2846
next += ident_len;
2847
2848
- if (next + ouc_size(0) > end)
2848
+ if (next + exclude_per_dir_offset + 1 > end)
2849
return NULL;
2850
2851
uc = xcalloc(1, sizeof(*uc));
@@ -2853,15 +2853,15 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
2853
strbuf_add(&uc->ident, ident, ident_len);
2854
load_oid_stat(&uc->ss_info_exclude,
2855
next + ouc_offset(info_exclude_stat),
2856
- next + ouc_offset(info_exclude_sha1));
2856
+ next + offset);
2857
load_oid_stat(&uc->ss_excludes_file,
2858
next + ouc_offset(excludes_file_stat),
2859
- next + ouc_offset(excludes_file_sha1));
2859
+ next + offset + hashsz);
2860
uc->dir_flags = get_be32(next + ouc_offset(dir_flags));
2861
- exclude_per_dir = (const char *)next + ouc_offset(exclude_per_dir);
2861
+ exclude_per_dir = (const char *)next + exclude_per_dir_offset;
2862
uc->exclude_per_dir = xstrdup(exclude_per_dir);
2863
/* NUL after exclude_per_dir is covered by sizeof(*ouc) */
2864
- next += ouc_size(strlen(exclude_per_dir));
2864
+ next += exclude_per_dir_offset + strlen(exclude_per_dir) + 1;
2865
if (next >= end)
2866
goto done2;
2867