debug: factor out logfile size check
[sfrench/samba-autobuild/.git] / lib / util / debug.c
index 4318bffa732a46b555a8694ab0bda8fc27600b39..2174add2a6c817ecbe8ebf9ec4c9d083f811fa37 100644 (file)
@@ -1174,10 +1174,61 @@ bool need_to_check_log_size(void)
  Check to see if the log has grown to be too big.
  **************************************************************************/
 
-void check_log_size( void )
+static void do_one_check_log_size(off_t maxlog, int *_fd, const char *logfile)
 {
-       int         maxlog;
+       char name[strlen(logfile) + 5];
        struct stat st;
+       int fd = *_fd;
+       int ret;
+       bool ok;
+
+       if (maxlog == 0) {
+               return;
+       }
+
+       ret = fstat(fd, &st);
+       if (ret != 0) {
+               return;
+       }
+       if (st.st_size < maxlog ) {
+               return;
+       }
+
+       /* reopen_logs_internal() modifies *_fd */
+       (void)reopen_logs_internal();
+       fd = *_fd;
+
+       if (fd <= 2) {
+               return;
+       }
+       ret = fstat(fd, &st);
+       if (ret != 0) {
+               return;
+       }
+       if (st.st_size < maxlog) {
+               return;
+       }
+
+       snprintf(name, sizeof(name), "%s.old", logfile);
+
+       (void)rename(logfile, name);
+
+       ok = reopen_logs_internal();
+       if (ok) {
+               return;
+       }
+       /* We failed to reopen a log - continue using the old name. */
+       (void)rename(name, logfile);
+}
+
+static void do_check_log_size(off_t maxlog)
+{
+       do_one_check_log_size(maxlog, &state.fd, state.debugf);
+}
+
+void check_log_size( void )
+{
+       off_t maxlog;
 
        /*
         *  We need to be root to check/change log-file, skip this and let the main
@@ -1207,23 +1258,7 @@ void check_log_size( void )
                (void)reopen_logs_internal();
        }
 
-       if (maxlog && (fstat(state.fd, &st) == 0
-           && st.st_size > maxlog )) {
-               (void)reopen_logs_internal();
-               if (state.fd > 2 && (fstat(state.fd, &st) == 0
-                                    && st.st_size > maxlog)) {
-                       char name[strlen(state.debugf) + 5];
-
-                       snprintf(name, sizeof(name), "%s.old", state.debugf);
-
-                       (void)rename(state.debugf, name);
-
-                       if (!reopen_logs_internal()) {
-                               /* We failed to reopen a log - continue using the old name. */
-                               (void)rename(name, state.debugf);
-                       }
-               }
-       }
+       do_check_log_size(maxlog);
 
        /*
         * Here's where we need to panic if state.fd == 0 or -1 (invalid values)