chiark / gitweb /
Be more careful when checking for empty files
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 16 Jul 2014 22:59:49 +0000 (18:59 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 16 Jul 2014 23:00:03 +0000 (19:00 -0400)
If we want to avoid reading a totally empty file, it seems better
to check after we have opened the file, not before.

src/network/networkd-netdev.c
src/network/networkd-network.c
src/shared/util.c
src/shared/util.h
src/udev/net/link-config.c
src/udev/udev-rules.c

index 9974913f49b364abbc5e405eed20b0d3a7d68d35..8b96d60dcab6a98d3ec15dc8bfec810ac0de04d0 100644 (file)
@@ -495,11 +495,6 @@ static int netdev_load_one(Manager *manager, const char *filename) {
         assert(manager);
         assert(filename);
 
-        if (null_or_empty_path(filename)) {
-                log_debug("skipping empty file: %s", filename);
-                return 0;
-        }
-
         file = fopen(filename, "re");
         if (!file) {
                 if (errno == ENOENT)
@@ -508,6 +503,11 @@ static int netdev_load_one(Manager *manager, const char *filename) {
                         return -errno;
         }
 
+        if (null_or_empty_fd(fileno(file))) {
+                log_debug("Skipping empty file: %s", filename);
+                return 0;
+        }
+
         netdev = new0(NetDev, 1);
         if (!netdev)
                 return log_oom();
index 700577fccfa5be2d8bfd2e3ac1f0509e35e51028..3e46a1a80efe82d944bf39d1b7294e4e5d95d81f 100644 (file)
@@ -48,8 +48,8 @@ static int network_load_one(Manager *manager, const char *filename) {
                         return -errno;
         }
 
-        if (null_or_empty_path(filename)) {
-                log_debug("skipping empty file: %s", filename);
+        if (null_or_empty_fd(fileno(file))) {
+                log_debug("Skipping empty file: %s", filename);
                 return 0;
         }
 
index 75dc58b63db2edeceab89b3c279a53924a2094de..4fda31c83832de5cd34efd99800e0da0f35dbeaf 100644 (file)
@@ -3604,6 +3604,17 @@ int null_or_empty_path(const char *fn) {
         return null_or_empty(&st);
 }
 
+int null_or_empty_fd(int fd) {
+        struct stat st;
+
+        assert(fd >= 0);
+
+        if (fstat(fd, &st) < 0)
+                return -errno;
+
+        return null_or_empty(&st);
+}
+
 DIR *xopendirat(int fd, const char *name, int flags) {
         int nfd;
         DIR *d;
index b3187a9ea1d708e789b22862d8544c381526bb22..d9d525e8a51215eedcb815d5f6c8de924602faae 100644 (file)
@@ -499,6 +499,7 @@ noreturn void freeze(void);
 
 bool null_or_empty(struct stat *st) _pure_;
 int null_or_empty_path(const char *fn);
+int null_or_empty_fd(int fd);
 
 DIR *xopendirat(int dirfd, const char *name, int flags);
 
index 73243fa107b1fe87ebcdc2e8254e9d8cbd62e6fb..512885f9c8604ced0757f4dd476133e7b3ab9dc8 100644 (file)
@@ -153,11 +153,6 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
         assert(ctx);
         assert(filename);
 
-        if (null_or_empty_path(filename)) {
-                log_debug("skipping empty file: %s", filename);
-                return 0;
-        }
-
         file = fopen(filename, "re");
         if (!file) {
                 if (errno == ENOENT)
@@ -166,6 +161,11 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
                         return -errno;
         }
 
+        if (null_or_empty_fd(fileno(file))) {
+                log_debug("Skipping empty file: %s", filename);
+                return 0;
+        }
+
         link = new0(link_config, 1);
         if (!link)
                 return log_oom();
index 85f78bcdefcd19bf9effbdf2eb28926a766d6470..9864016d1447c1ab041ead6ed0d515d22f73d5a8 100644 (file)
@@ -1533,15 +1533,19 @@ static int parse_file(struct udev_rules *rules, const char *filename)
         int line_nr = 0;
         unsigned int i;
 
-        if (null_or_empty_path(filename)) {
-                log_debug("skip empty file: %s", filename);
-                return 0;
+        f = fopen(filename, "re");
+        if (!f) {
+                if (errno == ENOENT)
+                        return 0;
+                else
+                        return -errno;
         }
-        log_debug("read rules file: %s", filename);
 
-        f = fopen(filename, "re");
-        if (f == NULL)
-                return -1;
+        if (null_or_empty_fd(fileno(f))) {
+                log_debug("Skipping empty file: %s", filename);
+                return 0;
+        } else
+                log_debug("Reading rules file: %s", filename);
 
         first_token = rules->token_cur;
         filename_off = rules_add_string(rules, filename);