tReplace fgets() with in-house lread() - pm - barely a pack manager
HTML git clone git://z3bra.org/pm
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
DIR commit d66413a9f2f5c91fb4f7e7b5e147e71f6478a881
DIR parent 166af82b0e517d8d199c9d63ef09739729b3c007
HTML Author: z3bra <willyatmailoodotorg>
Date: Sat, 30 Jan 2016 10:27:16 +0100
Replace fgets() with in-house lread()
Diffstat:
M pm.c | 36 +++++++++++++++++++++++--------
1 file changed, 27 insertions(+), 9 deletions(-)
---
DIR diff --git a/pm.c b/pm.c
t@@ -49,6 +49,7 @@ void usage(char *name);
int is_empty(char *dir);
int mkdir_parents(char *dir, mode_t mode);
char *base_name(char *path);
+char *lread(int fd, char *buf, size_t len);
struct pack *pack_load(char *path);
void pack_free(struct pack *p);
t@@ -62,7 +63,7 @@ int write_metadata(char *datadir, struct pack *pack);
int write_entry(struct archive *a, struct archive *w);
int unpack(char *rootfs, char *datadir, struct pack *p);
int install(char *rootfs, char *datadir, struct pack *p);
-int delete_content(FILE *metafile);
+int delete_content(int fd);
int delete(const char *rootfs, const char *datadir, const char *name);
char *argv0;
t@@ -140,6 +141,23 @@ base_name(char *path)
/*
+ * Reads a line which is at least 'len' bytes long. It will load
+ * the line into "buf". The user has to ensure the addressed
+ * pointed to by 'buf' is big enough to contain the line.
+ */
+char *
+lread(int fd, char *buf, size_t len)
+{
+ size_t i;
+ int r, c = 0;
+ while ((r = read(fd, &c, 1)) > 0 && c != '\n' && i < len) {
+ buf[i++] = c;
+ }
+ buf[i] = '\0';
+ return r > 0 ? buf : NULL;
+}
+
+/*
* Check for collisions between the filesystem and the tarball
*/
int
t@@ -436,15 +454,15 @@ install(char *rootfs, char *datadir, struct pack *p)
* if the entry doesn't exist, it will be ignored
*/
int
-delete_content(FILE *f)
+delete_content(int fd)
{
int r = 0;
char file[PATH_MAX] = "";
struct stat st;
size_t len = 0;
- if (fgets(file, PATH_MAX, f))
- if ((r = delete_content(f)) < 0)
+ if (lread(fd, file, PATH_MAX))
+ if ((r = delete_content(fd)) < 0)
return r;
/* remove trailing '\n' */
t@@ -488,7 +506,7 @@ delete_content(FILE *f)
int
delete(const char *rootfs, const char *datadir, const char *packname)
{
- FILE *f;
+ int fd;
char tmp[PATH_MAX];
struct stat st;
t@@ -499,7 +517,7 @@ delete(const char *rootfs, const char *datadir, const char *packname)
}
snprintf(tmp, PATH_MAX, "%s/%s/files", datadir, packname);
- if ((f = fopen(tmp, "r")) == NULL) {
+ if ((fd = open(tmp, O_RDONLY)) < 0) {
perror(tmp);
return ERR_DELETE;
}
t@@ -512,12 +530,12 @@ delete(const char *rootfs, const char *datadir, const char *packname)
}
/* ignore errors so everything gets deleted */
- if (delete_content(f) < 0) {
+ if (delete_content(fd) < 0) {
fprintf(stderr, "%s: cannot remove pack\n", packname);
- fclose(f);
+ close(fd);
return ERR_DELETE;
}
- fclose(f);
+ close(fd);
if (verbose == 1)
printf("%s: removing metadata\n", packname);