mirror of
https://git.zx2c4.com/cgit
synced 2024-11-09 10:08:42 +00:00
Add and use a common readfile() function
This function is used to read the full content of a textfile into a newly allocated buffer (with zerotermination). It replaces the earlier readfile() in scan-tree.c (which was rather error-prone[1]), and is reused by read_agefile() in ui-repolist.c. 1: No checks for EINTR and EAGAIN, fixed-size buffer Signed-off-by: Lars Hjemli <hjemli@gmail.com>
This commit is contained in:
parent
523c133e2e
commit
e16f178334
1
cgit.h
1
cgit.h
@ -283,5 +283,6 @@ extern int cgit_parse_snapshots_mask(const char *str);
|
||||
extern int cgit_open_filter(struct cgit_filter *filter);
|
||||
extern int cgit_close_filter(struct cgit_filter *filter);
|
||||
|
||||
extern int readfile(const char *path, char **buf, size_t *size);
|
||||
|
||||
#endif /* CGIT_H */
|
||||
|
16
scan-tree.c
16
scan-tree.c
@ -35,25 +35,13 @@ static int is_git_dir(const char *path)
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *readfile(const char *path)
|
||||
{
|
||||
FILE *f;
|
||||
static char buf[MAX_PATH];
|
||||
|
||||
if (!(f = fopen(path, "r")))
|
||||
return NULL;
|
||||
buf[0] = 0;
|
||||
fgets(buf, MAX_PATH, f);
|
||||
fclose(f);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static void add_repo(const char *base, const char *path)
|
||||
{
|
||||
struct cgit_repo *repo;
|
||||
struct stat st;
|
||||
struct passwd *pwd;
|
||||
char *p;
|
||||
size_t size;
|
||||
|
||||
if (stat(path, &st)) {
|
||||
fprintf(stderr, "Error accessing %s: %s (%d)\n",
|
||||
@ -80,7 +68,7 @@ static void add_repo(const char *base, const char *path)
|
||||
|
||||
p = fmt("%s/description", path);
|
||||
if (!stat(p, &st))
|
||||
repo->desc = xstrdup(readfile(p));
|
||||
readfile(p, &repo->desc, &size);
|
||||
|
||||
p = fmt("%s/README.html", path);
|
||||
if (!stat(p, &st))
|
||||
|
21
shared.c
21
shared.c
@ -393,3 +393,24 @@ int cgit_close_filter(struct cgit_filter *filter)
|
||||
return 0;
|
||||
die("Subprocess %s exited abnormally", filter->cmd);
|
||||
}
|
||||
|
||||
/* Read the content of the specified file into a newly allocated buffer,
|
||||
* zeroterminate the buffer and return 0 on success, errno otherwise.
|
||||
*/
|
||||
int readfile(const char *path, char **buf, size_t *size)
|
||||
{
|
||||
int fd;
|
||||
struct stat st;
|
||||
|
||||
fd = open(path, O_RDONLY);
|
||||
if (fd == -1)
|
||||
return errno;
|
||||
if (fstat(fd, &st))
|
||||
return errno;
|
||||
if (!S_ISREG(st.st_mode))
|
||||
return EISDIR;
|
||||
*buf = xmalloc(st.st_size + 1);
|
||||
*size = read_in_full(fd, *buf, st.st_size);
|
||||
(*buf)[*size] = '\0';
|
||||
return (*size == st.st_size ? 0 : errno);
|
||||
}
|
||||
|
@ -18,19 +18,20 @@
|
||||
|
||||
time_t read_agefile(char *path)
|
||||
{
|
||||
FILE *f;
|
||||
static char buf[64], buf2[64];
|
||||
time_t result;
|
||||
size_t size;
|
||||
char *buf;
|
||||
static char buf2[64];
|
||||
|
||||
if (!(f = fopen(path, "r")))
|
||||
if (readfile(path, &buf, &size))
|
||||
return -1;
|
||||
buf[0] = 0;
|
||||
if (fgets(buf, sizeof(buf), f) == NULL)
|
||||
return -1;
|
||||
fclose(f);
|
||||
|
||||
if (parse_date(buf, buf2, sizeof(buf2)))
|
||||
return strtoul(buf2, NULL, 10);
|
||||
result = strtoul(buf2, NULL, 10);
|
||||
else
|
||||
return 0;
|
||||
result = 0;
|
||||
free(buf);
|
||||
return result;
|
||||
}
|
||||
|
||||
static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
|
||||
|
Loading…
Reference in New Issue
Block a user