[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Logging with subdirectories (patch)



Hi,
On Mon, Jul 09, 2001 at 06:06:12PM +1000, Peter Zelezny wrote:

>[...]
>
> A bit of a late reply, but better late then never.
> 
> There's a couple problems with the patch, the first one, I think it leaks
> the "sep". It gets set to NULL, so effectively it will be doing free(0).
> Free(0) crashes on some unix's, but is ignored on linux.

ok, fixed now.

> 
> The second thing, you don't need to use "\" for win32, access/open etc
> handle forward slashes just fine, I think it's better if this was removed
> so it doesn't create a path with some "\" and some "/".

ok, fixed now.

> 
> Besides that, I don't have a problem with putting this in. How confident
> are you that the code is safe and can be put in "stable" 1.8.1?

Hmmm I've been using the patch since i wrote it, and it didn't cause any 
problems. I checked it with the default logmask ("%s,%c.xchatlog") and my 
favourite ("%c/%y.log").

And I don't think it will segfault in any situation because the length of 
the filename is limited by pathlen. The only problem is, the code looks a 
bit ugly :>

> Also, please use diff -u in future, so the patches are readable :)

ok, fixed now ;)

cu at IRCNet #xchat
tobias
-- 
Tobias v. Koch # Mail: tvk@weltcharts.de        # Linux is not better
    tvk        # WWW : http://probiers.net/     # than Windows! Or at
     @         # PGP : 0x3FE1548C, get it from  # least not for
   IRCNet      #       http://key.probiers.net/ # everyone.
diff -Pdr -U 6 xchat-orig/src/common/text.c xchat-1.8.0/src/common/text.c
--- xchat-orig/src/common/text.c	Sun Jun 24 08:38:46 2001
+++ xchat-1.8.0/src/common/text.c	Mon Jul  9 16:16:33 2001
@@ -197,13 +197,14 @@
 }
 
 static int
 open_log_file (char *servname, char *channame)
 {
 	char buf[512], fn[256];
-	int fd;
+	char *dir, *sep;
+	int fd, pathlen=510, c=0;
 	time_t currenttime;
 
 	if (!strcasecmp (channame, servname))
 	{
 		channame = strdup ("server");
 	} else
@@ -219,12 +220,55 @@
 #else
 		mkdir (buf, S_IRUSR | S_IWUSR | S_IXUSR);
 #endif
 	auto_insert (fn, prefs.logmask, NULL, NULL, "", channame, "", "", "", servname);
 
 	snprintf (buf, 510, "%s/xchatlogs/%s", get_xdir (), fn);
+
+ 	/* The following code handles subdirectories in logpath and creates
+ 	 * them, if they don't exist. Useful with logmasks like "%c/%y.log" in
+ 	 * ~/.xchat/xchat.conf.
+ 	 *                       -- patch by tvk <tvk@von-koch.com>
+ 	 */
+	
+ 	if (access (buf, F_OK) != 0) {
+ 		snprintf (buf, 510, "%s/xchatlogs/", get_xdir ());
+ 		pathlen -= strlen(buf);
+ 
+ 		/* how many sub-directories do we have? */
+ 		sep = fn;
+ 		while (*sep++) if (*sep=='/') c++;
+			
+ 		if (c) {
+ 			/* create each sub-directory */
+ 			sep = strdup(fn);
+			dir = strtok(sep, "/");
+		
+			while (c-- && dir != NULL) {
+				strncat (buf, dir, pathlen);
+ 				strcat (buf, "/");
+ 				pathlen -= strlen(dir);
+ 		
+ 				if (access (buf, F_OK) != 0)
+#ifdef WIN32	
+ 					mkdir (buf);
+#else	
+				        mkdir (buf, S_IRUSR | S_IWUSR | S_IXUSR);
+#endif	
+				dir = strtok(NULL, "/");
+			}
+ 			
+			/* append the filename */
+ 			strncat (buf, strrchr(fn, '/')+1, pathlen);
+ 			free (sep);
+ 		} else {
+ 			strncat (buf, fn, pathlen);
+ 		}
+ 	
+ 	}
+
 	free (channame);
 	fd = open (buf, O_CREAT | O_APPEND | O_WRONLY | OFLAGS, 0644);
 	if (fd < 0)
 		return -1;
 	currenttime = time (NULL);
 	write (fd, buf,