- test for external iterator
[oweals/gnunet.git] / src / util / disk.c
index 53e8867e2d6b814c04e15284549cd0ab70b4ed1c..e815afb2b6c2f8fe6e7c7f353557ec5289046e35 100644 (file)
@@ -1,10 +1,10 @@
 /*
      This file is part of GNUnet.
-     (C) 2001--2012 Christian Grothoff (and other contributing authors)
+     (C) 2001--2013 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
  */
 #define COPY_BLK_SIZE 65536
 
-
-
-#if defined(LINUX) || defined(CYGWIN) || defined(GNU)
+#include <sys/types.h>
+#if HAVE_SYS_VFS_H
 #include <sys/vfs.h>
-#else
-#if defined(SOMEBSD) || defined(DARWIN)
+#endif
+#if HAVE_SYS_PARAM_H
 #include <sys/param.h>
+#endif
+#if HAVE_SYS_MOUNT_H
 #include <sys/mount.h>
-#else
-#ifdef SOLARIS
-#include <sys/types.h>
+#endif
+#if HAVE_SYS_STATVFS_H
 #include <sys/statvfs.h>
-#else
-#ifdef MINGW
-#ifndef PIPE_BUF
-#define PIPE_BUF        512
-ULONG PipeSerialNumber;
 #endif
+
+#ifndef S_ISLNK
 #define        _IFMT           0170000 /* type of file */
 #define        _IFLNK          0120000 /* symbolic link */
 #define  S_ISLNK(m)    (((m)&_IFMT) == _IFLNK)
-#else
-#error PORT-ME: need to port statfs (how much space is left on the drive?)
-#endif
-#endif
-#endif
-#endif
-
-#if !defined(SOMEBSD) && !defined(DARWIN) && !defined(WINDOWS)
-#include <wordexp.h>
-#endif
-#if LINUX
-#include <sys/statvfs.h>
 #endif
 
 
@@ -343,49 +328,65 @@ int
 GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
                                   uint64_t * ino)
 {
-#if LINUX
-  struct stat sbuf;
-  struct statvfs fbuf;
+#if WINDOWS
+  {
+    // FIXME NILS: test this
+    struct GNUNET_DISK_FileHandle *fh;
+    BY_HANDLE_FILE_INFORMATION info;
+    int succ;
 
-  if ((0 == stat (filename, &sbuf)) && (0 == statvfs (filename, &fbuf)))
+    fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, 0);
+    if (fh == NULL)
+      return GNUNET_SYSERR;
+    succ = GetFileInformationByHandle (fh->h, &info);
+    GNUNET_DISK_file_close (fh);
+    if (!succ)
+    {
+      return GNUNET_SYSERR;
+    }
+    *dev = info.dwVolumeSerialNumber;
+    *ino = ((((uint64_t) info.nFileIndexHigh) << (sizeof (DWORD) * 8)) | info.nFileIndexLow);
+  }
+#else /* !WINDOWS */
+#if HAVE_STAT
   {
-    *dev = (uint64_t) fbuf.f_fsid;
+    struct stat sbuf;
+
+    if (0 != stat (filename, &sbuf))
+    {
+      return GNUNET_SYSERR;
+    }
     *ino = (uint64_t) sbuf.st_ino;
-    return GNUNET_OK;
   }
-#elif SOMEBSD
-  struct stat sbuf;
-  struct statfs fbuf;
+#else
+  *ino = 0;
+#endif
+#if HAVE_STATVFS
+  {
+    struct statvfs fbuf;
 
-  if ((0 == stat (filename, &sbuf)) && (0 == statfs (filename, &fbuf)))
+    if (0 != statvfs (filename, &fbuf))
+    {
+      return GNUNET_SYSERR;
+    }
+    *dev = (uint64_t) fbuf.f_fsid;
+  }
+#elif HAVE_STATFS
   {
+    struct statfs fbuf;
+
+    if (0 != statfs (filename, &fbuf))
+    {
+      return GNUNET_SYSERR;
+    }
     *dev = ((uint64_t) fbuf.f_fsid.val[0]) << 32 ||
         ((uint64_t) fbuf.f_fsid.val[1]);
-    *ino = (uint64_t) sbuf.st_ino;
-    return GNUNET_OK;
-  }
-#elif WINDOWS
-  // FIXME NILS: test this
-  struct GNUNET_DISK_FileHandle *fh;
-  BY_HANDLE_FILE_INFORMATION info;
-  int succ;
-
-  fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, 0);
-  if (fh == NULL)
-    return GNUNET_SYSERR;
-  succ = GetFileInformationByHandle (fh->h, &info);
-  GNUNET_DISK_file_close (fh);
-  if (succ)
-  {
-    *dev = info.dwVolumeSerialNumber;
-    *ino = ((((uint64_t) info.nFileIndexHigh) << (sizeof (DWORD) * 8)) | info.nFileIndexLow);
-    return GNUNET_OK;
   }
-  else
-    return GNUNET_SYSERR;
-
+#else
+  *dev = 0;
 #endif
-  return GNUNET_SYSERR;
+#endif /* !WINDOWS */
+  return GNUNET_OK;
 }
 
 
@@ -559,65 +560,6 @@ GNUNET_DISK_mktemp (const char *t)
 }
 
 
-/**
- * Get the number of blocks that are left on the partition that
- * contains the given file (for normal users).
- *
- * @param part a file on the partition to check
- * @return -1 on errors, otherwise the number of free blocks
- */
-long
-GNUNET_DISK_get_blocks_available (const char *part)
-{
-#ifdef SOLARIS
-  struct statvfs buf;
-
-  if (0 != statvfs (part, &buf))
-  {
-    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
-    return -1;
-  }
-  return buf.f_bavail;
-#elif MINGW
-  DWORD dwDummy;
-  DWORD dwBlocks;
-  wchar_t szDrive[4];
-  wchar_t wpath[MAX_PATH + 1];
-  char *path;
-
-  path = GNUNET_STRINGS_filename_expand (part);
-  if (path == NULL)
-    return -1;
-  /* "part" was in UTF-8, and so is "path" */
-  if (ERROR_SUCCESS != plibc_conv_to_win_pathwconv(path, wpath))
-  {
-    GNUNET_free (path);
-    return -1;
-  }
-  GNUNET_free (path);
-  wcsncpy (szDrive, wpath, 3);
-  szDrive[3] = 0;
-  if (!GetDiskFreeSpaceW (szDrive, &dwDummy, &dwDummy, &dwBlocks, &dwDummy))
-  {
-    LOG (GNUNET_ERROR_TYPE_WARNING, _("`%s' failed for drive `%S': %u\n"),
-         "GetDiskFreeSpace", szDrive, GetLastError ());
-
-    return -1;
-  }
-  return dwBlocks;
-#else
-  struct statfs s;
-
-  if (0 != statfs (part, &s))
-  {
-    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "statfs", part);
-    return -1;
-  }
-  return s.f_bavail;
-#endif
-}
-
-
 /**
  * Test if "fil" is a directory and listable. Optionally, also check if the
  * directory is readable.  Will not print an error message if the directory does
@@ -645,7 +587,7 @@ GNUNET_DISK_directory_test (const char *fil, int is_readable)
   }
   if (!S_ISDIR (filestat.st_mode))
   {
-    LOG (GNUNET_ERROR_TYPE_WARNING,
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
          "A file already exits with the same name %s\n", fil);
     return GNUNET_NO;
   }
@@ -1184,6 +1126,7 @@ GNUNET_DISK_directory_scan (const char *dirName,
   struct dirent *finfo;
   struct stat istat;
   int count = 0;
+  int ret;
   char *name;
   char *dname;
   unsigned int name_len;
@@ -1241,11 +1184,14 @@ GNUNET_DISK_directory_scan (const char *dirName,
       GNUNET_snprintf (name, n_size, "%s%s%s", dname,
                        (strcmp (dname, DIR_SEPARATOR_STR) ==
                         0) ? "" : DIR_SEPARATOR_STR, finfo->d_name);
-      if (GNUNET_OK != callback (callback_cls, name))
+      ret = callback (callback_cls, name);
+      if (GNUNET_OK != ret)
       {
         CLOSEDIR (dinfo);
         GNUNET_free (name);
         GNUNET_free (dname);
+        if (GNUNET_NO == ret)
+          return count;
         return GNUNET_SYSERR;
       }
     }
@@ -1729,7 +1675,11 @@ GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
     mode = translate_unix_perms (perm);
   }
 
-  fd = open (expfn, oflags | O_LARGEFILE, mode);
+  fd = open (expfn, oflags 
+#if O_CLOEXEC
+            | O_CLOEXEC
+#endif
+            | O_LARGEFILE, mode);
   if (fd == -1)
   {
     if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))
@@ -1866,7 +1816,7 @@ GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
   return ret;
 }
 
-#ifndef WINDOWS
+#ifdef WINDOWS
 /**
  * Get a GNUnet file handle from a W32 handle.
  *
@@ -1921,14 +1871,18 @@ GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh)
 /**
  * Get a handle from a native integer FD.
  *
- * @param fd native integer file descriptor
- * @return file handle corresponding to the descriptor
+ * @param fno native integer file descriptor
+ * @return file handle corresponding to the descriptor, NULL on error
  */
 struct GNUNET_DISK_FileHandle *
 GNUNET_DISK_get_handle_from_int_fd (int fno)
 {
   struct GNUNET_DISK_FileHandle *fh;
 
+  if ( (((off_t) -1) == lseek (fno, 0, SEEK_CUR)) &&
+       (EBADF == errno) )
+    return NULL; /* invalid FD */
+
 #ifndef WINDOWS
   fh = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
 
@@ -2213,6 +2167,9 @@ GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
 
 
 #if WINDOWS
+#ifndef PIPE_BUF
+#define PIPE_BUF        512
+#endif
 /* Copyright Bob Byrnes  <byrnes <at> curl.com>
    http://permalink.gmane.org/gmane.os.cygwin.patches/2121
 */