Skip to content

Commit

Permalink
Merge remote-tracking branch 'matze/for-marius' into fixes
Browse files Browse the repository at this point in the history
* matze/for-marius:
  bee-cache-inventory: get symlink destination with readlink() if missing
  • Loading branch information
mariux committed Aug 13, 2012
2 parents 610774f + 67289f4 commit 4e35e34
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/bee-cache-inventory.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct item {
char *type;
char *filename;
char *destination;
char free_destination;
};

struct inventory_meta {
Expand Down Expand Up @@ -146,6 +147,28 @@ int substitute(char *data, char from, char to)
return 1;
}

char *read_symlink(const char *filename)
{
ssize_t ret;
char buffer[PATH_MAX + 1] = {0};
char *copy;

ret = readlink(filename, buffer, PATH_MAX);

if (ret == -1) {
fprintf(stderr, "bee-cache-inventory: warning: "
"cannot restore empty symlink destination for file '%s': %m\n", filename);
return NULL;
}

copy = strdup(buffer);

if (!copy)
fprintf(stderr, "bee-cache-inventory: read_symlink: strdup: %m\n");

return copy;
}

BEE_STATIC_INLINE char *_extract_pattern(struct item *item, char **dest, char *hint, char *pattern, char size, int failok)
{
char *p;
Expand Down Expand Up @@ -182,12 +205,17 @@ int do_separation(char *line, struct item *item)
/* get possible symlink destination */
q = strstr(item->filename, "//");
if (q) {
*q = '\0';
item->destination = q + 2;

if (!*item->destination) {
fprintf (stderr, "bee-cache-inventory: syntax error: empty destination for file '%s'\n", item->filename);
return 0;
item->destination = read_symlink(item->filename);

if (!item->destination)
item->destination = item->filename;
else
item->free_destination = 1;
}
*q = '\0';
}

*(p-6) = 0;
Expand Down Expand Up @@ -344,6 +372,9 @@ int inventory_fhfh(FILE *infh, FILE *outfh, struct inventory_meta meta)

print_item(outfh, item, meta);

if (item.free_destination)
/* symlink destination was generated by read_symlink */
free(item.destination);
}

return 1;
Expand Down

0 comments on commit 4e35e34

Please sign in to comment.