[tin-dev] [PATCH] create_path() better error messages

Urs Janßen urs at tin.org
Tue Aug 22 17:59:01 CEST 2023


untested as usual. review welcome.

=== modified file 'src/feed.c'
--- old/src/feed.c	2023-06-27 01:46:29 +0000
+++ new/src/feed.c	2023-08-22 15:52:15 +0000
@@ -705,7 +705,7 @@
 					if (function != FEED_AUTOSAVE && (pproc_func = get_post_proc_type()) == GLOBAL_ABORT)
 						return -1;
 				}
-				if (!create_path(outpath))
+				if (create_path(outpath) != 0)
 					return -1;
 			}
 			break;

=== modified file 'src/save.c'
--- old/src/save.c	2023-08-13 06:19:57 +0000
+++ new/src/save.c	2023-08-22 15:52:03 +0000
@@ -399,12 +399,10 @@
 {
 	FILE *fp;
 	char keyappend[MAXKEYLEN], keyoverwrite[MAXKEYLEN], keyquit[MAXKEYLEN];
-	char mode[3];
+	char mode[3] = { 'a', '+', '\0'};
 	struct stat st;
 	t_function func;
 
-	strcpy(mode, "a+");
-
 	/*
 	 * Mailboxes will always be appended to
 	 */
@@ -429,6 +427,7 @@
 		switch (func) {
 			case SAVE_OVERWRITE_FILE:
 				strcpy(mode, "w");
+				tinrc.default_save_mode = 'o';
 				break;
 
 			case GLOBAL_ABORT:
@@ -437,16 +436,13 @@
 				return NULL;
 
 			default:	/* SAVE_APPEND_FILE */
+				tinrc.default_save_mode = 'a';
 				break;
 		}
-		if (func == SAVE_OVERWRITE_FILE)
-			tinrc.default_save_mode = 'o';
-		else
-			tinrc.default_save_mode = 'a';
 	}
 
 	if ((fp = fopen(path, mode)) == NULL) {
-		error_message(2, _(txt_cannot_open_for_saving), path);
+		perror_message(_(txt_cannot_open_for_saving), path);
 		return NULL;
 	}
 
@@ -579,41 +575,38 @@
  * Create the supplied path. Create intermediate directories as needed
  * Don't create the last component (which would be the filename) unless the
  * path is / terminated.
- * Return FALSE if it somehow fails.
+ * Return errno if it somehow fails.
  */
-t_bool
+int
 create_path(
 	const char *path)
 {
 	char *buf, *p;
+	int pe = 0;
 	struct stat st;
 
-	if (!strlen(path))
-		return FALSE;
-
-	buf = my_strdup(path);
-	p = buf + 1;
-
-	if (!strlen(p)) {
-		free(buf);
-		return FALSE;
-	}
+	if (!*path || !*(path + 1))
+		return ENOTDIR;
+
+	p = buf = my_strdup(path);
+	p++;
 
 	while ((p = strchr(p, '/')) != NULL) {
 		*p = '\0';
 		if (stat(buf, &st) == -1) {
 			if (my_mkdir(buf, (mode_t) (S_IRWXU|S_IRUGO|S_IXUGO)) == -1) {
 				if (errno != EEXIST) {
+					pe = errno;
 					perror_message(_(txt_cannot_create), buf);
 					free(buf);
-					return FALSE;
+					return pe;
 				}
 			}
 		}
 		*p++ = '/';
 	}
 	free(buf);
-	return TRUE;
+	return pe;
 }
 
 
@@ -670,8 +663,8 @@
 		return NULL;
 	}
 
-	if (!(create_path(savepath))) {
-		error_message(2, _(txt_cannot_open_for_saving), savepath);
+	if ((errno = create_path(savepath)) != 0) {
+		perror_message(_(txt_cannot_open_for_saving), savepath);
 		free(savepath);
 		return NULL;
 	}

=== modified file 'include/proto.h'
--- old/include/proto.h	2023-08-10 06:21:49 +0000
+++ new/include/proto.h	2023-08-22 15:53:03 +0000
@@ -623,7 +623,7 @@
 
 /* save.c */
 extern int check_start_save_any_news(int function, t_bool catchup, int num_cmd_line_groups);
-extern t_bool create_path(const char *path);
+extern int create_path(const char *path);
 extern t_bool post_process_files(t_function proc_type_func, t_bool auto_delete);
 extern t_bool save_and_process_art(t_openartinfo *artinfo, t_bool is_mailbox, const char *inpath, int max, t_bool post_process);
 extern t_part *get_part(int n);




More information about the tin-dev mailing list