Fixed fs tar mount to strip trailing slash before hashing filename

This commit is contained in:
rxi
2016-10-01 15:04:24 +01:00
parent 82d7561265
commit 253f058391

View File

@@ -94,7 +94,7 @@ static int concat_and_get_file_type(const char *dir, const char *filename) {
} }
static unsigned hashstr(const char *str) { static unsigned hash_string(const char *str) {
unsigned hash = 5381; unsigned hash = 5381;
while (*str) { while (*str) {
hash = ((hash << 5) + hash) ^ *str++; hash = ((hash << 5) + hash) ^ *str++;
@@ -103,6 +103,14 @@ static unsigned hashstr(const char *str) {
} }
static void strip_trailing_slash(char *str) {
int len = strlen(str);
if (len > 0 && str[len - 1] == '/') {
str[len - 1] = '\0';
}
}
/*==================*/ /*==================*/
/* Directory Mount */ /* Directory Mount */
/*==================*/ /*==================*/
@@ -191,7 +199,7 @@ static int tar_find(Mount *mnt, const char *filename, mtar_header_t *h) {
/* Hash filename and linear search map for matching hash, read header and /* Hash filename and linear search map for matching hash, read header and
* check against filename if the hashes match */ * check against filename if the hashes match */
TarMount *tm = mnt->udata; TarMount *tm = mnt->udata;
unsigned hash = hashstr(filename); unsigned hash = hash_string(filename);
int i; int i;
for (i = 0; i < tm->nfiles; i++) { for (i = 0; i < tm->nfiles; i++) {
@@ -199,18 +207,13 @@ static int tar_find(Mount *mnt, const char *filename, mtar_header_t *h) {
/* Seek to and load header */ /* Seek to and load header */
mtar_seek(&tm->tar, tm->map[i].pos); mtar_seek(&tm->tar, tm->map[i].pos);
mtar_read_header(&tm->tar, h); mtar_read_header(&tm->tar, h);
/* Strip trailing `/` */
int len = strlen(h->name);
if (len > 0 && h->name[len - 1] == '/') {
h->name[len - 1] = '\0';
}
/* Compare names */ /* Compare names */
strip_trailing_slash(h->name);
if ( !strcmp(h->name, filename) ) { if ( !strcmp(h->name, filename) ) {
return FILESYSTEM_ESUCCESS; return FILESYSTEM_ESUCCESS;
} }
} }
} }
return FILESYSTEM_EFAILURE; return FILESYSTEM_EFAILURE;
} }
@@ -352,7 +355,8 @@ static int tar_mount(Mount *mnt, const char *path) {
tm->map = dmt_realloc(tm->map, cap * sizeof(*tm->map)); tm->map = dmt_realloc(tm->map, cap * sizeof(*tm->map));
} }
/* Store entry */ /* Store entry */
tm->map[n].hash = hashstr(h.name); strip_trailing_slash(h.name);
tm->map[n].hash = hash_string(h.name);
tm->map[n].pos = tar->pos; tm->map[n].pos = tar->pos;
/* Next */ /* Next */
mtar_next(tar); mtar_next(tar);