towards adding mq destruction notification
[oweals/gnunet.git] / src / util / disk.c
index 04bed6cc91288e131091b332706b761dfb429e12..100b312a47be9f91968c98b07b50e1b594617ed8 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2001--2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2001--2013 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -527,14 +527,18 @@ char *
 GNUNET_DISK_mkdtemp (const char *t)
 {
   char *fn;
+  mode_t omask;
 
+  omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
   fn = mktemp_name (t);
   if (fn != mkdtemp (fn))
   {
-    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn);
+    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdtemp", fn);
     GNUNET_free (fn);
+    umask (omask);
     return NULL;
   }
+  umask (omask);
   return fn;
 }
 
@@ -587,14 +591,18 @@ GNUNET_DISK_mktemp (const char *t)
 {
   int fd;
   char *fn;
+  mode_t omask;
 
+  omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
   fn = mktemp_name (t);
   if (-1 == (fd = mkstemp (fn)))
   {
     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn);
     GNUNET_free (fn);
+    umask (omask);
     return NULL;
   }
+  umask (omask);
   if (0 != CLOSE (fd))
     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn);
   return fn;
@@ -800,8 +808,7 @@ GNUNET_DISK_directory_create (const char *dir)
 
 
 /**
- * Create the directory structure for storing
- * a file.
+ * Create the directory structure for storing a file.
  *
  * @param filename name of a file in the directory
  * @returns #GNUNET_OK on success,
@@ -815,18 +822,30 @@ GNUNET_DISK_directory_create_for_file (const char *filename)
   char *rdir;
   size_t len;
   int ret;
+  int eno;
 
   rdir = GNUNET_STRINGS_filename_expand (filename);
-  if (rdir == NULL)
+  if (NULL == rdir)
+  {
+    errno = EINVAL;
     return GNUNET_SYSERR;
+  }
   len = strlen (rdir);
   while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
     len--;
   rdir[len] = '\0';
+  /* The empty path is invalid and in this case refers to / */
+  if (0 == len)
+  {
+    GNUNET_free (rdir);
+    rdir = GNUNET_strdup ("/");
+  }
   ret = GNUNET_DISK_directory_create (rdir);
-  if ((ret == GNUNET_OK) && (0 != ACCESS (rdir, W_OK)))
+  if ((GNUNET_OK == ret) && (0 != ACCESS (rdir, W_OK)))
     ret = GNUNET_NO;
+  eno = errno;
   GNUNET_free (rdir);
+  errno = eno;
   return ret;
 }
 
@@ -1161,7 +1180,9 @@ GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h,
  * @return number of bytes written on success, #GNUNET_SYSERR on error
  */
 ssize_t
-GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n,
+GNUNET_DISK_fn_write (const char *fn,
+                      const void *buffer,
+                      size_t n,
                       enum GNUNET_DISK_AccessPermissions mode)
 {
   struct GNUNET_DISK_FileHandle *fh;