commit bdd334cea6ff98524f22ce5037ba46887398725a from: Omar Polo date: Thu Jul 25 19:07:08 2024 UTC gotwebd: refactor file load in its own routine Split gotweb_load_file() out of gotweb_get_repo_description() and reuse it for gotweb_get_clone_url() too. Will be useful in the future for other tasks too. commit - 9e56bdf9d7deb5c9c510a46a91fb918c1ba4c9d2 commit + bdd334cea6ff98524f22ce5037ba46887398725a blob - 8cc103e1ce5dd08b81df1be04fee1d2d01c8f66c blob + a7cce8ea218018fc1805b2e77685728952ffc4c4 --- gotwebd/gotweb.c +++ gotwebd/gotweb.c @@ -1131,95 +1131,65 @@ err: } static const struct got_error * +gotweb_load_file(char **str, const char *dir, const char *file, int dirfd) +{ + const struct got_error *error = NULL; + struct stat sb; + off_t len; + int fd; + + *str = NULL; + + fd = openat(dirfd, file, O_RDONLY); + if (fd == -1) { + if (errno == ENOENT || errno == EACCES) + return NULL; + return got_error_from_errno_fmt("openat %s/%s", dir, file); + } + + if (fstat(fd, &sb) == -1) { + error = got_error_from_errno_fmt("fstat %s/%s", dir, file); + goto done; + } + + len = sb.st_size; + if (len > GOTWEBD_MAXDESCRSZ - 1) + len = GOTWEBD_MAXDESCRSZ - 1; + + *str = calloc(len + 1, 1); + if (*str == NULL) { + error = got_error_from_errno("calloc"); + goto done; + } + + if (read(fd, *str, len) == -1) + error = got_error_from_errno("read"); + done: + if (fd != -1 && close(fd) == -1 && error == NULL) + error = got_error_from_errno("close"); + return error; +} + +static const struct got_error * gotweb_get_repo_description(char **description, struct server *srv, const char *dirpath, int dir) { - const struct got_error *error = NULL; - struct stat sb; - int fd = -1; - off_t len; - *description = NULL; if (srv->show_repo_description == 0) return NULL; - fd = openat(dir, "description", O_RDONLY); - if (fd == -1) { - if (errno != ENOENT && errno != EACCES) { - error = got_error_from_errno_fmt("openat %s/%s", - dirpath, "description"); - } - goto done; - } - - if (fstat(fd, &sb) == -1) { - error = got_error_from_errno_fmt("fstat %s/%s", - dirpath, "description"); - goto done; - } - - len = sb.st_size; - if (len > GOTWEBD_MAXDESCRSZ - 1) - len = GOTWEBD_MAXDESCRSZ - 1; - - *description = calloc(len + 1, sizeof(**description)); - if (*description == NULL) { - error = got_error_from_errno("calloc"); - goto done; - } - - if (read(fd, *description, len) == -1) - error = got_error_from_errno("read"); -done: - if (fd != -1 && close(fd) == -1 && error == NULL) - error = got_error_from_errno("close"); - return error; + return gotweb_load_file(description, dirpath, "description", dir); } static const struct got_error * gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath, int dir) { - const struct got_error *error = NULL; - struct stat sb; - int fd = -1; - off_t len; - *url = NULL; if (srv->show_repo_cloneurl == 0) return NULL; - fd = openat(dir, "cloneurl", O_RDONLY); - if (fd == -1) { - if (errno != ENOENT && errno != EACCES) { - error = got_error_from_errno_fmt("openat %s/%s", - dirpath, "cloneurl"); - } - goto done; - } - - if (fstat(fd, &sb) == -1) { - error = got_error_from_errno_fmt("fstat %s/%s", - dirpath, "cloneurl"); - goto done; - } - - len = sb.st_size; - if (len > GOTWEBD_MAXCLONEURLSZ - 1) - len = GOTWEBD_MAXCLONEURLSZ - 1; - - *url = calloc(len + 1, sizeof(**url)); - if (*url == NULL) { - error = got_error_from_errno("calloc"); - goto done; - } - - if (read(fd, *url, len) == -1) - error = got_error_from_errno("read"); -done: - if (fd != -1 && close(fd) == -1 && error == NULL) - error = got_error_from_errno("close"); - return error; + return gotweb_load_file(url, dirpath, "cloneurl", dir); } int