libbb: introduce and use xrename and rename_or_warn.
authorDenis Vlasenko <vda.linux@googlemail.com>
Sun, 17 Feb 2008 14:28:53 +0000 (14:28 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Sun, 17 Feb 2008 14:28:53 +0000 (14:28 -0000)
editors/patch.c
editors/sed.c
include/libbb.h
libbb/vfork_daemon_rexec.c
libbb/xfuncs.c
miscutils/crond.c
networking/sendmail.c
runit/runsv.c
sysklogd/syslogd.c
util-linux/mdev.c

index 07fa5cfaf6f4a61e58f9c09b67d26b9f7b4984ac..1b283a1332d078adf33101972a829e2e70207e29 100644 (file)
@@ -150,10 +150,7 @@ int patch_main(int argc, char **argv)
                        backup_filename = xmalloc(strlen(new_filename) + 6);
                        strcpy(backup_filename, new_filename);
                        strcat(backup_filename, ".orig");
-                       if (rename(new_filename, backup_filename) == -1) {
-                               bb_perror_msg_and_die("cannot create file %s",
-                                               backup_filename);
-                       }
+                       xrename(new_filename, backup_filename);
                        dst_stream = xfopen(new_filename, "w");
                }
 
index 433418253f0db7042367712c2bd2da9bd513b2c5..e55bcafc441e595f61c0ac39292cd672b7f97c46 100644 (file)
@@ -1340,8 +1340,7 @@ int sed_main(int argc, char **argv)
 
                        G.nonstdout = stdout;
                        /* unlink(argv[i]); */
-                       // FIXME: error check / message?
-                       rename(G.outname, argv[i]);
+                       xrename(G.outname, argv[i]);
                        free(G.outname);
                        G.outname = NULL;
                }
index 67afcdf94ae8713c07dce0fdb2ce998272f405dd..2af89df95888272a048f343d680c0d983f5084e3 100644 (file)
@@ -298,6 +298,8 @@ int xopen(const char *pathname, int flags);
 int xopen3(const char *pathname, int flags, int mode);
 int open_or_warn(const char *pathname, int flags);
 int open3_or_warn(const char *pathname, int flags, int mode);
+void xrename(const char *oldpath, const char *newpath);
+int rename_or_warn(const char *oldpath, const char *newpath);
 off_t xlseek(int fd, off_t offset, int whence);
 off_t fdlength(int fd);
 
index 98339c930ca169761f6d1302ecd6a982bd00e355..1567d89be66b6bdaae1c7b7a9596c629da19bc20 100644 (file)
@@ -98,11 +98,6 @@ int wait4pid(int pid)
        if (WIFSIGNALED(status))
                return WTERMSIG(status) + 1000;
        return 0;
-       if (WIFEXITED(status))
-               return WEXITSTATUS(status);
-       if (WIFSIGNALED(status))
-               return WTERMSIG(status) + 1000;
-       return 0;
 }
 
 #if ENABLE_FEATURE_PREFER_APPLETS
index 8dd414d6a9de3ec078e6ffb213a1f71db6db6623..b4c059f2084463a3a0c0fe1503bd43517579767e 100644 (file)
@@ -146,18 +146,32 @@ int open_or_warn(const char *pathname, int flags)
        return open3_or_warn(pathname, flags, 0666);
 }
 
-void xpipe(int filedes[2])
-{
-       if (pipe(filedes))
-               bb_perror_msg_and_die("can't create pipe");
-}
-
 void xunlink(const char *pathname)
 {
        if (unlink(pathname))
                bb_perror_msg_and_die("can't remove file '%s'", pathname);
 }
 
+void xrename(const char *oldpath, const char *newpath)
+{
+       if (rename(oldpath, newpath))
+               bb_perror_msg_and_die("can't move '%s' to '%s'", oldpath, newpath);
+}
+
+int rename_or_warn(const char *oldpath, const char *newpath)
+{
+       int n = rename(oldpath, newpath);
+       if (n)
+               bb_perror_msg("can't move '%s' to '%s'", oldpath, newpath);
+       return n;
+}
+
+void xpipe(int filedes[2])
+{
+       if (pipe(filedes))
+               bb_perror_msg_and_die("can't create pipe");
+}
+
 // Turn on nonblocking I/O on a fd
 int ndelay_on(int fd)
 {
index 0df143ae7556f76b7ec10c166dcd521fac898cdb..9721a8a9ae29aca53e792aee843382c13fb0231b 100644 (file)
@@ -548,7 +548,7 @@ static void CheckUpdates(void)
 
        fi = fopen(CRONUPDATE, "r");
        if (fi != NULL) {
-               remove(CRONUPDATE);
+               unlink(CRONUPDATE);
                while (fgets(buf, sizeof(buf), fi) != NULL) {
                        SynchronizeFile(strtok(buf, " \t\r\n"));
                }
@@ -579,7 +579,7 @@ static void SynchronizeDir(void)
         * scan directory and add associated users
         */
 
-       remove(CRONUPDATE);
+       unlink(CRONUPDATE);
        if (chdir(CDir) < 0) {
                crondlog("\311cannot find %s\n", CDir);
        }
@@ -814,7 +814,7 @@ ForkJob(const char *user, CronLine * line, int mailFd,
                crondlog("\024cannot fork\n");
                line->cl_Pid = 0;
                if (mail_filename) {
-                       remove(mail_filename);
+                       unlink(mail_filename);
                }
        } else if (mail_filename) {
                /* PARENT, FORK SUCCESS
@@ -823,7 +823,7 @@ ForkJob(const char *user, CronLine * line, int mailFd,
                char mailFile2[128];
 
                snprintf(mailFile2, sizeof(mailFile2), TMPDIR "/cron.%s.%d", user, pid);
-               rename(mail_filename, mailFile2);
+               rename(mail_filename, mailFile2); // TODO: xrename?
        }
        /*
         * Close the mail file descriptor.. we can't just leave it open in
@@ -896,7 +896,7 @@ static void EndJob(const char *user, CronLine * line)
         */
 
        mailFd = open(mailFile, O_RDONLY);
-       remove(mailFile);
+       unlink(mailFile);
        if (mailFd < 0) {
                return;
        }
index fa995abf4211d83b37f55e25ce494740ea02ac56..b2fbc5a7af1bea863c4b4da221288b77144285f7 100644 (file)
@@ -512,10 +512,7 @@ int sendgetmail_main(int argc, char **argv)
                                if (fd < 0)
                                        bb_perror_msg_and_die("cannot create unique file");
                                close(fd);
-                               if (rename(tmp_name, new_name) < 0) {
-                                       // something is very wrong
-                                       bb_perror_msg_and_die("cannot move %s to %s", tmp_name, new_name);
-                               }
+                               xrename(tmp_name, new_name);
                        }
 
                        // delete message from server
index 02271d68b1a604b6d66483c1e248169ac6d3d425..02dcf50cae94619e62b1d3bd75ca04fc1a5d22cf 100644 (file)
@@ -157,16 +157,6 @@ static int open_trunc_or_warn(const char *name)
        return fd;
 }
 
-static int rename_or_warn(const char *old, const char *new)
-{
-       if (rename(old, new) == -1) {
-               bb_perror_msg("%s: warning: cannot rename %s to %s",
-                               dir, old, new);
-               return -1;
-       }
-       return 0;
-}
-
 static void update_status(struct svdir *s)
 {
        ssize_t sz;
index c6e0571383bb633ce7ed2a4cbffc47cf2091e378..dc5e6250a2b7e6fb514311438ca1d6a671a8a297 100644 (file)
@@ -344,10 +344,10 @@ static void log_locally(time_t now, char *msg)
                                sprintf(newFile, "%s.%d", G.logFilePath, i);
                                if (i == 0) break;
                                sprintf(oldFile, "%s.%d", G.logFilePath, --i);
-                               rename(oldFile, newFile);
+                               xrename(oldFile, newFile);
                        }
                        /* newFile == "f.0" now */
-                       rename(G.logFilePath, newFile);
+                       xrename(G.logFilePath, newFile);
                        fl.l_type = F_UNLCK;
                        fcntl(G.logFD, F_SETLKW, &fl);
                        close(G.logFD);
index a18fc0916dd7fa8c83d2863d90b6b3cba4ea94ab..2bb022476fae9f9aa9af82b856f86d401b5a7405 100644 (file)
@@ -209,7 +209,7 @@ static void make_device(char *path, int delete)
                                } else
                                        dest = alias;
 
-                               rename(device_name, dest);
+                               rename(device_name, dest); // TODO: xrename?
                                symlink(dest, device_name);
 
                                if (alias != dest)