adding new GNUNET_HELPER_ API for communication with (SUID) helper binaries via stdin...
[oweals/gnunet.git] / src / util / disk.c
index 9e64a3a60c3fec00da11b3412b891fbd89cdaeee..e6b542adfeaa608e27e0456243b34142fa8518d8 100644 (file)
@@ -204,7 +204,6 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
 #endif
 }
 
-
 /**
  * Get the size of an open file.
  *
@@ -214,7 +213,7 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
  */
 int
 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
-                             off_t *size)
+                             OFF_T *size)
 {
 #if WINDOWS
   BOOL b;
@@ -225,7 +224,7 @@ GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
     SetErrnoFromWinError (GetLastError ());
     return GNUNET_SYSERR;
   }
-  *size = (off_t) li.QuadPart;
+  *size = (OFF_T) li.QuadPart;
 #else
   struct stat sbuf;
 
@@ -245,8 +244,8 @@ GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
  * @param whence specification to which position the offset parameter relates to
  * @return the new position on success, GNUNET_SYSERR otherwise
  */
-off_t
-GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset,
+OFF_T
+GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, OFF_T offset,
                        enum GNUNET_DISK_Seek whence)
 {
   if (h == NULL)
@@ -256,19 +255,21 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, off_t offset,
   }
 
 #ifdef MINGW
-  DWORD ret;
+  LARGE_INTEGER li, new_pos;
+  BOOL b;
 
   static DWORD t[] = {[GNUNET_DISK_SEEK_SET] = FILE_BEGIN,
     [GNUNET_DISK_SEEK_CUR] = FILE_CURRENT,[GNUNET_DISK_SEEK_END] = FILE_END
   };
+  li.QuadPart = offset;
 
-  ret = SetFilePointer (h->h, offset, NULL, t[whence]);
-  if (ret == INVALID_SET_FILE_POINTER)
+  b = SetFilePointerEx (h->h, li, &new_pos, t[whence]);
+  if (b == 0)
   {
     SetErrnoFromWinError (GetLastError ());
     return GNUNET_SYSERR;
   }
-  return ret;
+  return (OFF_T) new_pos.QuadPart;
 #else
   static int t[] = {[GNUNET_DISK_SEEK_SET] = SEEK_SET,
     [GNUNET_DISK_SEEK_CUR] = SEEK_CUR,[GNUNET_DISK_SEEK_END] = SEEK_END
@@ -361,7 +362,7 @@ GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
   if (succ)
   {
     *dev = info.dwVolumeSerialNumber;
-    *ino = ((info.nFileIndexHigh << sizeof (DWORD)) | info.nFileIndexLow);
+    *ino = ((((uint64_t) info.nFileIndexHigh) << (sizeof (DWORD) * 8)) | info.nFileIndexLow);
     return GNUNET_OK;
   }
   else
@@ -1251,8 +1252,8 @@ GNUNET_DISK_file_change_owner (const char *filename, const char *user)
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 int
-GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
-                       off_t lockEnd, int excl)
+GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, OFF_T lockStart,
+                       OFF_T lockEnd, int excl)
 {
   if (fh == NULL)
   {
@@ -1272,13 +1273,18 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
   return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
 #else
   OVERLAPPED o;
+  OFF_T diff = lockEnd - lockStart;
+  DWORD diff_low, diff_high;
+  diff_low = (DWORD) (diff & 0xFFFFFFFF);
+  diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
 
   memset (&o, 0, sizeof (OVERLAPPED));
-  o.Offset = lockStart;
+  o.Offset = (DWORD) (lockStart & 0xFFFFFFFF);;
+  o.OffsetHigh = (DWORD) (((lockStart & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
 
   if (!LockFileEx
       (fh->h, (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | LOCKFILE_FAIL_IMMEDIATELY,
-       0, lockEnd - lockStart, 0, &o))
+       0, diff_low, diff_high, &o))
   {
     SetErrnoFromWinError (GetLastError ());
     return GNUNET_SYSERR;
@@ -1297,8 +1303,8 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 int
-GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
-                         off_t unlockEnd)
+GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, OFF_T unlockStart,
+                         OFF_T unlockEnd)
 {
   if (fh == NULL)
   {
@@ -1318,11 +1324,16 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
   return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
 #else
   OVERLAPPED o;
+  OFF_T diff = unlockEnd - unlockStart;
+  DWORD diff_low, diff_high;
+  diff_low = (DWORD) (diff & 0xFFFFFFFF);
+  diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
 
   memset (&o, 0, sizeof (OVERLAPPED));
-  o.Offset = unlockStart;
+  o.Offset = (DWORD) (unlockStart & 0xFFFFFFFF);;
+  o.OffsetHigh = (DWORD) (((unlockStart & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF);
 
-  if (!UnlockFileEx (fh->h, 0, unlockEnd - unlockStart, 0, &o))
+  if (!UnlockFileEx (fh->h, 0, diff_low, diff_high, &o))
   {
     SetErrnoFromWinError (GetLastError ());
     return GNUNET_SYSERR;