]> git.notmuchmail.org Git - notmuch/commitdiff
cli/insert: abstract temporary filename generation
authorJani Nikula <jani@nikula.org>
Mon, 22 Sep 2014 09:54:58 +0000 (11:54 +0200)
committerDavid Bremner <david@tethera.net>
Wed, 24 Sep 2014 18:28:42 +0000 (20:28 +0200)
This will clean up the usage. There's the slight functional change of
potentially ending up doing extra gethostname and getpid calls, but
this is neglible.

notmuch-insert.c

index cdeeb415ca3b3417f5903ac5e325a22579c0062f..a1d564c78cd1eb0f52b126de7c7c4dd257c87888 100644 (file)
@@ -179,6 +179,31 @@ maildir_create_folder (const void *ctx, const char *maildir)
     return TRUE;
 }
 
     return TRUE;
 }
 
+/*
+ * Generate a temporary file basename, no path, do not create an
+ * actual file. Return the basename, or NULL on errors.
+ */
+static char *
+tempfilename (const void *ctx)
+{
+    char *filename;
+    char hostname[256];
+    struct timeval tv;
+    pid_t pid;
+
+    /* We follow the Dovecot file name generation algorithm. */
+    pid = getpid ();
+    safe_gethostname (hostname, sizeof (hostname));
+    gettimeofday (&tv, NULL);
+
+    filename = talloc_asprintf (ctx, "%ld.M%ldP%d.%s",
+                               tv.tv_sec, tv.tv_usec, pid, hostname);
+    if (! filename)
+       fprintf (stderr, "Error: %s\n", strerror (ENOMEM));
+
+    return filename;
+}
+
 /* Open a unique file in the 'tmp' sub-directory of dir.
  * Returns the file descriptor on success, or -1 on failure.
  * On success, file paths for the message in the 'tmp' and 'new'
 /* Open a unique file in the 'tmp' sub-directory of dir.
  * Returns the file descriptor on success, or -1 on failure.
  * On success, file paths for the message in the 'tmp' and 'new'
@@ -188,23 +213,13 @@ static int
 maildir_open_tmp_file (void *ctx, const char *dir,
                       char **tmppath, char **newpath, char **newdir)
 {
 maildir_open_tmp_file (void *ctx, const char *dir,
                       char **tmppath, char **newpath, char **newdir)
 {
-    pid_t pid;
-    char hostname[256];
-    struct timeval tv;
     char *filename;
     int fd = -1;
 
     char *filename;
     int fd = -1;
 
-    /* We follow the Dovecot file name generation algorithm. */
-    pid = getpid ();
-    safe_gethostname (hostname, sizeof (hostname));
     do {
     do {
-       gettimeofday (&tv, NULL);
-       filename = talloc_asprintf (ctx, "%ld.M%ldP%d.%s",
-                                   tv.tv_sec, tv.tv_usec, pid, hostname);
-       if (! filename) {
-           fprintf (stderr, "Out of memory\n");
+       filename = tempfilename (ctx);
+       if (! filename)
            return -1;
            return -1;
-       }
 
        *tmppath = talloc_asprintf (ctx, "%s/tmp/%s", dir, filename);
        if (! *tmppath) {
 
        *tmppath = talloc_asprintf (ctx, "%s/tmp/%s", dir, filename);
        if (! *tmppath) {