Skip to content

Commit

Permalink
LinkTable now saves the refresh time
Browse files Browse the repository at this point in the history
  • Loading branch information
fangfufu committed May 2, 2024
1 parent 1a3f36a commit d50c668
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions src/link.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,14 @@ static void LinkTable_disk_delete(const char *dirn)
FREE(metadirn);
}

/* This is necessary to get the compiler on some platforms to stop
complaining about the fact that we're not using the return value of
fread, when we know we aren't and that's fine. */
static inline void ignore_value(int i)
{
(void) i;
}

int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
{
char *metadirn = path_append(META_DIR, dirn);
Expand All @@ -673,16 +681,19 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
FREE(path);
return -1;
}
FREE(path);

fwrite(&linktbl->num, sizeof(int), 1, fp);
if (fwrite(&linktbl->num, sizeof(int), 1, fp) != 1 ||
fwrite(&linktbl->index_time, sizeof(time), 1, fp) != 1) {
lprintf(error, "Failed to save the header of %s!\n", path);
}
FREE(path);
for (int i = 0; i < linktbl->num; i++) {
fwrite(linktbl->links[i]->linkname, sizeof(char),
MAX_FILENAME_LEN, fp);
fwrite(linktbl->links[i]->f_url, sizeof(char), MAX_PATH_LEN, fp);
fwrite(&linktbl->links[i]->type, sizeof(LinkType), 1, fp);
fwrite(&linktbl->links[i]->content_length, sizeof(size_t), 1, fp);
fwrite(&linktbl->links[i]->time, sizeof(long), 1, fp);
ignore_value(fwrite(linktbl->links[i]->linkname, sizeof(char),
MAX_FILENAME_LEN, fp));
ignore_value(fwrite(linktbl->links[i]->f_url, sizeof(char), MAX_PATH_LEN, fp));
ignore_value(fwrite(&linktbl->links[i]->type, sizeof(LinkType), 1, fp));
ignore_value(fwrite(&linktbl->links[i]->content_length, sizeof(size_t), 1, fp));
ignore_value(fwrite(&linktbl->links[i]->time, sizeof(long), 1, fp));
}

int res = 0;
Expand All @@ -701,14 +712,6 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
return res;
}

/* This is necessary to get the compiler on some platforms to stop
complaining about the fact that we're not using the return value of
fread, when we know we aren't and that's fine. */
static inline void ignore_value(int i)
{
(void) i;
}

LinkTable *LinkTable_disk_open(const char *dirn)
{
char *metadirn = path_append(META_DIR, dirn);
Expand All @@ -729,10 +732,12 @@ LinkTable *LinkTable_disk_open(const char *dirn)

LinkTable *linktbl = CALLOC(1, sizeof(LinkTable));

if (fread(&linktbl->num, sizeof(int), 1, fp) != 1) {
lprintf(error, "Failed to read the first int of %s!\n", path);
if (fread(&linktbl->num, sizeof(int), 1, fp) != 1 ||
fread(&linktbl->index_time, sizeof(time), 1, fp) != 1) {
lprintf(error, "Failed to read the header of %s!\n", path);
LinkTable_free(linktbl);
LinkTable_disk_delete(dirn);
FREE(path);
return NULL;
}
linktbl->links = CALLOC(linktbl->num, sizeof(Link *));
Expand All @@ -749,16 +754,13 @@ LinkTable *LinkTable_disk_open(const char *dirn)
sizeof(size_t), 1, fp));
ignore_value(fread(&linktbl->links[i]->time, sizeof(long), 1, fp));
if (feof(fp)) {
/*
* reached EOF
*/
lprintf(error, "reached EOF!\n");
lprintf(error, "Corrupted LinkTable!\n");
LinkTable_free(linktbl);
LinkTable_disk_delete(dirn);
return NULL;
}
if (ferror(fp)) {
lprintf(error, "encountered ferror!\n");
lprintf(error, "Encountered ferror!\n");
LinkTable_free(linktbl);
LinkTable_disk_delete(dirn);
return NULL;
Expand All @@ -769,6 +771,7 @@ LinkTable *LinkTable_disk_open(const char *dirn)
"cannot close the file pointer, %s\n", strerror(errno));
}
return linktbl;
FREE(path);
}

LinkTable *path_to_Link_LinkTable_new(const char *path)
Expand All @@ -790,6 +793,9 @@ LinkTable *path_to_Link_LinkTable_new(const char *path)
time_t time_now = time(NULL);
if (time_now - next_table->index_time > CONFIG.refresh_timeout) {
/* refresh directory contents */
lprintf(info, "%d, %d\n", time_now, next_table->index_time)
lprintf(info, "%d, %d\n", time_now - next_table->index_time, CONFIG.refresh_timeout);
lprintf(info, "Refreshing LinkTable for %s\n", path);
LinkTable_free(next_table);
next_table = NULL;
if (link) {
Expand Down

0 comments on commit d50c668

Please sign in to comment.