]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/log/ModStdio.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / log / ModStdio.cc
index efd7a4b061f4837c3e6226c4584b535410580b04..fc95307ec6ed9462966838fcf559edaedd60482b 100644 (file)
@@ -1,39 +1,24 @@
 /*
- * DEBUG: section 50    Log file handling
- * AUTHOR: Duane Wessels
- *
- * SQUID Web Proxy Cache          http://www.squid-cache.org/
- * ----------------------------------------------------------
- *
- *  Squid is the result of efforts by numerous individuals from
- *  the Internet community; see the CONTRIBUTORS file for full
- *  details.   Many organizations have provided support for Squid's
- *  development; see the SPONSORS file for full details.  Squid is
- *  Copyrighted (C) 2001 by the Regents of the University of
- *  California; see the COPYRIGHT file for full details.  Squid
- *  incorporates software developed and/or copyrighted by other
- *  sources; see the CREDITS file for full details.
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
+ * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
  *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
-#include "squid-old.h"
+/* DEBUG: section 50    Log file handling */
+
+#include "squid.h"
+#include "fatal.h"
+#include "fd.h"
 #include "fde.h"
+#include "fs_io.h"
+#include "globals.h"
 #include "log/File.h"
 #include "log/ModStdio.h"
+#include "SquidConfig.h"
+
+#include <cerrno>
 
 typedef struct {
     int fd;
@@ -52,6 +37,7 @@ logfileWriteWrapper(Logfile * lf, const void *buf, size_t len)
     l_stdio_t *ll = (l_stdio_t *) lf->data;
     size_t s;
     s = FD_WRITE_METHOD(ll->fd, (char const *) buf, len);
+    int xerrno = errno;
     fd_bytes(ll->fd, s, FD_WRITE);
 
     if (s == len)
@@ -60,7 +46,7 @@ logfileWriteWrapper(Logfile * lf, const void *buf, size_t len)
     if (!lf->flags.fatal)
         return;
 
-    fatalf("logfileWrite: %s: %s\n", lf->path, xstrerror());
+    fatalf("logfileWrite: %s: %s\n", lf->path, xstrerr(xerrno));
 }
 
 static void
@@ -92,7 +78,7 @@ logfile_mod_stdio_writeline(Logfile * lf, const char *buf, size_t len)
 }
 
 static void
-logfile_mod_stdio_linestart(Logfile * lf)
+logfile_mod_stdio_linestart(Logfile *)
 {
 }
 
@@ -113,18 +99,14 @@ logfile_mod_stdio_flush(Logfile * lf)
 }
 
 static void
-logfile_mod_stdio_rotate(Logfile * lf)
+logfile_mod_stdio_rotate(Logfile * lf, const int16_t nRotate)
 {
 #ifdef S_ISREG
 
     struct stat sb;
 #endif
 
-    int i;
-    char from[MAXPATHLEN];
-    char to[MAXPATHLEN];
     l_stdio_t *ll = (l_stdio_t *) lf->data;
-    assert(lf->path);
     const char *realpath = lf->path+6; // skip 'stdio:' prefix.
     assert(realpath);
 
@@ -138,29 +120,37 @@ logfile_mod_stdio_rotate(Logfile * lf)
 
     debugs(0, DBG_IMPORTANT, "Rotate log file " << lf->path);
 
+    SBuf basePath(realpath);
+
     /* Rotate numbers 0 through N up one */
-    for (i = Config.Log.rotateNumber; i > 1;) {
-        i--;
-        snprintf(from, MAXPATHLEN, "%s.%d", realpath, i - 1);
-        snprintf(to, MAXPATHLEN, "%s.%d", realpath, i);
-        xrename(from, to);
+    for (int16_t i = nRotate; i > 1;) {
+        --i;
+        SBuf from(basePath);
+        from.appendf(".%d", i-1);
+        SBuf to(basePath);
+        to.appendf(".%d", i);
+        FileRename(from, to);
+        // TODO handle rename errors
     }
 
     /* Rotate the current log to .0 */
     logfileFlush(lf);
 
-    file_close(ll->fd);                /* always close */
+    file_close(ll->fd);     /* always close */
 
-    if (Config.Log.rotateNumber > 0) {
-        snprintf(to, MAXPATHLEN, "%s.%d", realpath, 0);
-        xrename(realpath, to);
+    if (nRotate > 0) {
+        SBuf to(basePath);
+        to.appendf(".0");
+        FileRename(basePath, to);
+        // TODO handle rename errors
     }
     /* Reopen the log.  It may have been renamed "manually" */
     ll->fd = file_open(realpath, O_WRONLY | O_CREAT | O_TEXT);
 
     if (DISK_ERROR == ll->fd && lf->flags.fatal) {
-        debugs(50, DBG_CRITICAL, "ERROR: logfileRotate: " << lf->path << ": " << xstrerror());
-        fatalf("Cannot open %s: %s", lf->path, xstrerror());
+        int xerrno = errno;
+        debugs(50, DBG_CRITICAL, MYNAME << "ERROR: " << lf->path << ": " << xstrerr(xerrno));
+        fatalf("Cannot open %s: %s", lf->path, xstrerr(xerrno));
     }
 }
 
@@ -199,19 +189,20 @@ logfile_mod_stdio_open(Logfile * lf, const char *path, size_t bufsz, int fatal_f
     ll->fd = file_open(path, O_WRONLY | O_CREAT | O_TEXT);
 
     if (DISK_ERROR == ll->fd) {
-        if (ENOENT == errno && fatal_flag) {
+        int xerrno = errno;
+        if (ENOENT == xerrno && fatal_flag) {
             fatalf("Cannot open '%s' because\n"
                    "\tthe parent directory does not exist.\n"
                    "\tPlease create the directory.\n", path);
-        } else if (EACCES == errno && fatal_flag) {
+        } else if (EACCES == xerrno && fatal_flag) {
             fatalf("Cannot open '%s' for writing.\n"
                    "\tThe parent directory must be writeable by the\n"
                    "\tuser '%s', which is the cache_effective_user\n"
                    "\tset in squid.conf.", path, Config.effectiveUser);
-        } else if (EISDIR == errno && fatal_flag) {
+        } else if (EISDIR == xerrno && fatal_flag) {
             fatalf("Cannot open '%s' because it is a directory, not a file.\n", path);
         } else {
-            debugs(50, DBG_IMPORTANT, "ERROR: logfileOpen " << lf->path << ": " << xstrerror());
+            debugs(50, DBG_IMPORTANT, MYNAME << "ERROR: " << lf->path << ": " << xstrerr(xerrno));
             return 0;
         }
     }
@@ -221,3 +212,4 @@ logfile_mod_stdio_open(Logfile * lf, const char *path, size_t bufsz, int fatal_f
     }
     return 1;
 }
+