libbb: add xfstat function
authorDenys Vlasenko <dvlasenk@redhat.com>
Tue, 31 Aug 2010 10:42:06 +0000 (12:42 +0200)
committerDenys Vlasenko <dvlasenk@redhat.com>
Tue, 31 Aug 2010 10:42:06 +0000 (12:42 +0200)
function                                             old     new   delta
xfstat                                                 -      25     +25
mkfs_ext2_main                                      2421    2423      +2
mkfs_reiser_main                                    1197    1194      -3
next                                                 312     307      -5
ar_main                                              533     522     -11
mkfs_minix_main                                     2938    2924     -14
mkfs_vfat_main                                      1511    1495     -16
writeTarFile                                         272     255     -17
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/6 up/down: 27/-66)            Total: -39 bytes

Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
13 files changed:
archival/ar.c
archival/tar.c
include/libbb.h
libbb/dump.c
libbb/update_passwd.c
libbb/xfuncs_printf.c
miscutils/ubi_attach_detach.c
shell/ash.c
util-linux/mkfs_ext2.c
util-linux/mkfs_minix.c
util-linux/mkfs_reiser.c
util-linux/mkfs_vfat.c
util-linux/mkswap.c

index bce62f7..05556c6 100644 (file)
@@ -123,8 +123,7 @@ static int write_ar_archive(archive_handle_t *handle)
        struct stat st;
        archive_handle_t *out_handle;
 
-       if (fstat(handle->src_fd, &st) == -1)
-               bb_simple_perror_msg_and_die(handle->ar__name);
+       xfstat(handle->src_fd, &st, handle->ar__name);
 
        /* if archive exists, create a new handle for output.
         * we create it in place of the old one.
index b5cbf41..2176ad2 100644 (file)
@@ -572,8 +572,7 @@ static NOINLINE int writeTarFile(int tar_fd, int verboseFlag,
 
        /* Store the stat info for the tarball's file, so
         * can avoid including the tarball into itself....  */
-       if (fstat(tbInfo.tarFd, &tbInfo.tarFileStatBuf) < 0)
-               bb_perror_msg_and_die("can't stat tar file");
+       xfstat(tbInfo.tarFd, &tbInfo.tarFileStatBuf, "can't stat tar file");
 
 #if ENABLE_FEATURE_SEAMLESS_GZ || ENABLE_FEATURE_SEAMLESS_BZ2
        if (gzip)
index 43e525c..6fb0438 100644 (file)
@@ -411,6 +411,7 @@ void bb_unsetenv(const char *key) FAST_FUNC;
 void bb_unsetenv_and_free(char *key) FAST_FUNC;
 void xunlink(const char *pathname) FAST_FUNC;
 void xstat(const char *pathname, struct stat *buf) FAST_FUNC;
+void xfstat(int fd, struct stat *buf, const char *errmsg) FAST_FUNC;
 int xopen(const char *pathname, int flags) FAST_FUNC;
 int xopen_nonblocking(const char *pathname) FAST_FUNC;
 int xopen3(const char *pathname, int flags, int mode) FAST_FUNC;
index a739ff6..4db3f06 100644 (file)
@@ -323,9 +323,7 @@ static void do_skip(priv_dumper_t *dumper, const char *fname, int statok)
        struct stat sbuf;
 
        if (statok) {
-               if (fstat(STDIN_FILENO, &sbuf)) {
-                       bb_simple_perror_msg_and_die(fname);
-               }
+               xfstat(STDIN_FILENO, &sbuf, fname);
                if (!(S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode) || S_ISFIFO(sbuf.st_mode))
                 && dumper->pub.dump_skip >= sbuf.st_size
                ) {
index e050dfc..a2be0f1 100644 (file)
@@ -133,7 +133,7 @@ int FAST_FUNC update_passwd(const char *filename,
        goto close_old_fp;
 
  created:
-       if (!fstat(old_fd, &sb)) {
+       if (fstat(old_fd, &sb) == 0) {
                fchmod(new_fd, sb.st_mode & 0777); /* ignore errors */
                fchown(new_fd, sb.st_uid, sb.st_gid);
        }
index b99f906..c6db38d 100644 (file)
@@ -436,6 +436,16 @@ void FAST_FUNC xstat(const char *name, struct stat *stat_buf)
                bb_perror_msg_and_die("can't stat '%s'", name);
 }
 
+void FAST_FUNC xfstat(int fd, struct stat *stat_buf, const char *errmsg)
+{
+       /* errmsg is usually a file name, but not always:
+        * xfstat may be called in a spot where file name is no longer
+        * available, and caller may give e.g. "can't stat input file" string.
+        */
+       if (fstat(fd, stat_buf))
+               bb_simple_perror_msg_and_die(errmsg);
+}
+
 // selinux_or_die() - die if SELinux is disabled.
 void FAST_FUNC selinux_or_die(void)
 {
index 15377aa..18ffd4d 100644 (file)
@@ -63,9 +63,9 @@ int ubi_attach_detach_main(int argc UNUSED_PARAM, char **argv)
        ubi_ctrl = argv[optind];
 
        fd = xopen(ubi_ctrl, O_RDWR);
-       //fstat(fd, &st);
+       //xfstat(fd, &st, ubi_ctrl);
        //if (!S_ISCHR(st.st_mode))
-       //      bb_error_msg_and_die("'%s' is not a char device", ubi_ctrl);
+       //      bb_error_msg_and_die("%s: not a char device", ubi_ctrl);
 
        if (do_attach) {
                if (!(opts & OPTION_M))
index 921367b..28a8bb6 100644 (file)
@@ -4989,9 +4989,13 @@ noclobberopen(const char *fname)
         * revealed that it was a regular file, and the file has not been
         * replaced, return the file descriptor.
         */
-       if (fstat(fd, &finfo2) == 0 && !S_ISREG(finfo2.st_mode)
-        && finfo.st_dev == finfo2.st_dev && finfo.st_ino == finfo2.st_ino)
+       if (fstat(fd, &finfo2) == 0
+        && !S_ISREG(finfo2.st_mode)
+        && finfo.st_dev == finfo2.st_dev
+        && finfo.st_ino == finfo2.st_ino
+       ) {
                return fd;
+       }
 
        /* The file has been replaced.  badness. */
        close(fd);
index 1c2b3b2..6dccd3a 100644 (file)
@@ -221,7 +221,7 @@ int mkfs_ext2_main(int argc UNUSED_PARAM, char **argv)
 
        // open the device, check the device is a block device
        xmove_fd(xopen(argv[0], O_WRONLY), fd);
-       fstat(fd, &st);
+       xfstat(fd, &st, argv[0]);
        if (!S_ISBLK(st.st_mode) && !(option_mask32 & OPT_F))
                bb_error_msg_and_die("%s: not a block device", argv[0]);
 
index 9e826ae..95499ba 100644 (file)
@@ -686,8 +686,7 @@ int mkfs_minix_main(int argc UNUSED_PARAM, char **argv)
                bb_error_msg_and_die("can't format mounted filesystem");
 
        xmove_fd(xopen(G.device_name, O_RDWR), dev_fd);
-       if (fstat(dev_fd, &statbuf) < 0)
-               bb_error_msg_and_die("can't stat '%s'", G.device_name);
+       xfstat(dev_fd, &statbuf, G.device_name);
        if (!S_ISBLK(statbuf.st_mode))
                opt &= ~1; // clear -c (check)
 
index 6e172d6..00ce8f1 100644 (file)
@@ -168,9 +168,9 @@ int mkfs_reiser_main(int argc UNUSED_PARAM, char **argv)
 
        // check the device is a block device
        fd = xopen(argv[0], O_WRONLY | O_EXCL);
-       fstat(fd, &st);
+       xfstat(fd, &st, argv[0]);
        if (!S_ISBLK(st.st_mode) && !(option_mask32 & OPT_f))
-               bb_error_msg_and_die("not a block device");
+               bb_error_msg_and_die("%s: not a block device", argv[0]);
 
        // check if it is mounted
        // N.B. what if we format a file? find_mount_point will return false negative since
index 211e67e..45760f7 100644 (file)
@@ -245,8 +245,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
        volume_id = time(NULL);
 
        dev = xopen(device_name, O_RDWR);
-       if (fstat(dev, &st) < 0)
-               bb_simple_perror_msg_and_die(device_name);
+       xfstat(dev, &st, device_name);
 
        //
        // Get image size and sector size
index 61a786e..53537fc 100644 (file)
@@ -15,8 +15,7 @@ static void mkswap_selinux_setcontext(int fd, const char *path)
        if (!is_selinux_enabled())
                return;
 
-       if (fstat(fd, &stbuf) < 0)
-               bb_perror_msg_and_die("fstat failed");
+       xfstat(fd, &stbuf, argv[0]);
        if (S_ISREG(stbuf.st_mode)) {
                security_context_t newcon;
                security_context_t oldcon = NULL;