]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-insert.c
cli/insert: rename file copy function
[notmuch] / notmuch-insert.c
index 8dfc8bb9c2bdb6592ec306deda9642237866aee2..5d478064540cce70940d21df729926710d20a48a 100644 (file)
@@ -83,10 +83,13 @@ sync_dir (const char *dir)
     return ret;
 }
 
-/* Check the specified folder name does not contain a directory
- * component ".." to prevent writes outside of the Maildir hierarchy. */
+/*
+ * Check the specified folder name does not contain a directory
+ * component ".." to prevent writes outside of the Maildir
+ * hierarchy. Return TRUE on valid folder name, FALSE otherwise.
+ */
 static notmuch_bool_t
-check_folder_name (const char *folder)
+is_valid_folder_name (const char *folder)
 {
     const char *p = folder;
 
@@ -248,11 +251,12 @@ maildir_open_tmp_file (void *ctx, const char *dir,
     return fd;
 }
 
-/* Copy the contents of standard input (fdin) into fdout.
- * Returns TRUE if a non-empty file was written successfully.
- * Otherwise, return FALSE. */
+/*
+ * Copy fdin to fdout, return TRUE on success, and FALSE on errors and
+ * empty input.
+ */
 static notmuch_bool_t
-copy_stdin (int fdin, int fdout)
+copy_fd (int fdout, int fdin)
 {
     notmuch_bool_t empty = TRUE;
 
@@ -291,52 +295,6 @@ copy_stdin (int fdin, int fdout)
     return (!interrupted && !empty);
 }
 
-/* Add the specified message file to the notmuch database, applying tags.
- * The file is renamed to encode notmuch tags as maildir flags. */
-static void
-add_file_to_database (notmuch_database_t *notmuch, const char *path,
-                     tag_op_list_t *tag_ops, notmuch_bool_t synchronize_flags)
-{
-    notmuch_message_t *message;
-    notmuch_status_t status;
-
-    status = notmuch_database_add_message (notmuch, path, &message);
-    switch (status) {
-    case NOTMUCH_STATUS_SUCCESS:
-    case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
-       break;
-    default:
-    case NOTMUCH_STATUS_FILE_NOT_EMAIL:
-    case NOTMUCH_STATUS_READ_ONLY_DATABASE:
-    case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
-    case NOTMUCH_STATUS_OUT_OF_MEMORY:
-    case NOTMUCH_STATUS_FILE_ERROR:
-    case NOTMUCH_STATUS_NULL_POINTER:
-    case NOTMUCH_STATUS_TAG_TOO_LONG:
-    case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
-    case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
-    case NOTMUCH_STATUS_LAST_STATUS:
-       fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n",
-                path, notmuch_status_to_string (status));
-       return;
-    }
-
-    if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
-       /* Don't change tags of an existing message. */
-       if (synchronize_flags) {
-           status = notmuch_message_tags_to_maildir_flags (message);
-           if (status != NOTMUCH_STATUS_SUCCESS)
-               fprintf (stderr, "Error: failed to sync tags to maildir flags\n");
-       }
-    } else {
-       tag_op_flag_t flags = synchronize_flags ? TAG_FLAG_MAILDIR_SYNC : 0;
-
-       tag_op_list_apply (message, tag_ops, flags);
-    }
-
-    notmuch_message_destroy (message);
-}
-
 static notmuch_bool_t
 write_message (void *ctx, int fdin, const char *dir, char **newpath)
 {
@@ -351,7 +309,7 @@ write_message (void *ctx, int fdin, const char *dir, char **newpath)
 
     cleanup_path = tmppath;
 
-    if (! copy_stdin (fdin, fdout))
+    if (! copy_fd (fdout, fdin))
        goto FAIL;
 
     if (fsync (fdout) != 0) {
@@ -386,6 +344,52 @@ write_message (void *ctx, int fdin, const char *dir, char **newpath)
     return FALSE;
 }
 
+/* Add the specified message file to the notmuch database, applying tags.
+ * The file is renamed to encode notmuch tags as maildir flags. */
+static void
+add_file_to_database (notmuch_database_t *notmuch, const char *path,
+                     tag_op_list_t *tag_ops, notmuch_bool_t synchronize_flags)
+{
+    notmuch_message_t *message;
+    notmuch_status_t status;
+
+    status = notmuch_database_add_message (notmuch, path, &message);
+    switch (status) {
+    case NOTMUCH_STATUS_SUCCESS:
+    case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
+       break;
+    default:
+    case NOTMUCH_STATUS_FILE_NOT_EMAIL:
+    case NOTMUCH_STATUS_READ_ONLY_DATABASE:
+    case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
+    case NOTMUCH_STATUS_OUT_OF_MEMORY:
+    case NOTMUCH_STATUS_FILE_ERROR:
+    case NOTMUCH_STATUS_NULL_POINTER:
+    case NOTMUCH_STATUS_TAG_TOO_LONG:
+    case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
+    case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
+    case NOTMUCH_STATUS_LAST_STATUS:
+       fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n",
+                path, notmuch_status_to_string (status));
+       return;
+    }
+
+    if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
+       /* Don't change tags of an existing message. */
+       if (synchronize_flags) {
+           status = notmuch_message_tags_to_maildir_flags (message);
+           if (status != NOTMUCH_STATUS_SUCCESS)
+               fprintf (stderr, "Error: failed to sync tags to maildir flags\n");
+       }
+    } else {
+       tag_op_flag_t flags = synchronize_flags ? TAG_FLAG_MAILDIR_SYNC : 0;
+
+       tag_op_list_apply (message, tag_ops, flags);
+    }
+
+    notmuch_message_destroy (message);
+}
+
 int
 notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
 {
@@ -449,8 +453,8 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
     if (folder == NULL) {
        maildir = db_path;
     } else {
-       if (! check_folder_name (folder)) {
-           fprintf (stderr, "Error: bad folder name: %s\n", folder);
+       if (! is_valid_folder_name (folder)) {
+           fprintf (stderr, "Error: invalid folder name: '%s'\n", folder);
            return EXIT_FAILURE;
        }
        maildir = talloc_asprintf (config, "%s/%s", db_path, folder);