struct GNUNET_DISK_FileHandle;
/**
- * Opaque handle used to manage a pipe.
+ * Handle used to manage a pipe.
*/
struct GNUNET_DISK_PipeHandle;
};
-// FIXME: use enum here!
-#define GNUNET_DISK_PERM_USER_READ 1
-#define GNUNET_DISK_PERM_USER_WRITE 2
-#define GNUNET_DISK_PERM_USER_EXEC 4
-#define GNUNET_DISK_PERM_GROUP_READ 8
-#define GNUNET_DISK_PERM_GROUP_WRITE 16
-#define GNUNET_DISK_PERM_GROUP_EXEC 32
-#define GNUNET_DISK_PERM_OTHER_READ 64
-#define GNUNET_DISK_PERM_OTHER_WRITE 128
-#define GNUNET_DISK_PERM_OTHER_EXEC 256
+/**
+ * File access permissions, UNIX-style.
+ */
+enum GNUNET_DISK_AccessPermissions
+ {
+ /**
+ * Nobody is allowed to do anything to the file.
+ */
+ GNUNET_DISK_PERM_NONE = 0,
+
+ /**
+ * Owner can read.
+ */
+ GNUNET_DISK_PERM_USER_READ = 1,
+
+ /**
+ * Owner can write.
+ */
+ GNUNET_DISK_PERM_USER_WRITE = 2,
+
+ /**
+ * Owner can execute.
+ */
+ GNUNET_DISK_PERM_USER_EXEC = 4,
+
+ /**
+ * Group can read.
+ */
+ GNUNET_DISK_PERM_GROUP_READ = 8,
+
+ /**
+ * Group can write.
+ */
+ GNUNET_DISK_PERM_GROUP_WRITE = 16,
+
+ /**
+ * Group can execute.
+ */
+ GNUNET_DISK_PERM_GROUP_EXEC = 32,
+
+ /**
+ * Everybody can read.
+ */
+ GNUNET_DISK_PERM_OTHER_READ = 64,
+
+ /**
+ * Everybody can write.
+ */
+ GNUNET_DISK_PERM_OTHER_WRITE = 128,
+
+ /**
+ * Everybody can execute.
+ */
+ GNUNET_DISK_PERM_OTHER_EXEC = 256
+ };
+
/**
* Constants for specifying how to seek.
GNUNET_DISK_SEEK_END
};
+
+/**
+ * Enumeration identifying the two ends of a pipe.
+ */
+enum GNUNET_DISK_PipeEnd
+ {
+ /**
+ * The reading-end of a pipe.
+ */
+ GNUNET_DISK_PIPE_END_READ = 0,
+
+ /**
+ * The writing-end of a pipe.
+ */
+ GNUNET_DISK_PIPE_END_WRITE = 1
+ };
+
+
/**
* Get the number of blocks that are left on the partition that
* contains the given file (for normal users).
/**
* Checks whether a handle is invalid
+ *
* @param h handle to check
* @return GNUNET_YES if invalid, GNUNET_NO if valid
*/
* of all sizes of files in the directory)
* @param includeSymLinks should symbolic links be
* included?
- *
* @return GNUNET_OK on success, GNUNET_SYSERR on error
*/
int GNUNET_DISK_file_size (const char *filename,
/**
* Create an (empty) temporary file on disk.
*
- * @param tmpl component to use for the name;
+ * @param t component to use for the name;
* does NOT contain "XXXXXX" or "/tmp/".
* @return NULL on error, otherwise name of fresh
* file on disk in directory for temporary files
*/
char *
-GNUNET_DISK_mktemp (const char *tmpl);
+GNUNET_DISK_mktemp (const char *t);
/**
- * Open a file.
+ * Open a file. Note that the access permissions will only be
+ * used if a new file is created and if the underlying operating
+ * system supports the given permissions.
*
* @param fn file name to be opened
* @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
- * @param ... permissions for the newly created file (only required if creation is possible)
+ * @param perm permissions for the newly created file, use
+ * GNUNET_DISK_PERM_USER_NONE if a file could not be created by this
+ * call (because of flags)
* @return IO handle on success, NULL on error
*/
struct GNUNET_DISK_FileHandle *GNUNET_DISK_file_open (const char *fn,
enum GNUNET_DISK_OpenFlags flags,
- ...);
+ enum GNUNET_DISK_AccessPermissions perm);
/**
* Creates an interprocess channel
/**
* Get the handle to a particular pipe end
* @param p pipe
- * @param n number of the end (0 or 1); FIXME: use enum here!
+ * @param n end to access
* @return handle for the respective end
*/
-const struct GNUNET_DISK_FileHandle *GNUNET_DISK_pipe_handle (const struct
- GNUNET_DISK_PipeHandle
- *p, int n);
+const struct GNUNET_DISK_FileHandle *
+GNUNET_DISK_pipe_handle (const struct
+ GNUNET_DISK_PipeHandle
+ *p,
+ enum GNUNET_DISK_PipeEnd n);
/**
* Read the contents of a binary file into a buffer.
/**
* Read the contents of a binary file into a buffer.
+ *
* @param fn file name
* @param result the buffer to write the result to
* @param len the maximum number of bytes to read
* @return number of bytes read, GNUNET_SYSERR on failure
*/
-ssize_t GNUNET_DISK_fn_read (const char * const fn, void *result,
+ssize_t GNUNET_DISK_fn_read (const char *fn,
+ void *result,
size_t len);
* @param fn file name
* @param buffer the data to write
* @param n number of bytes to write
+ * @param mode file permissions
* @return number of bytes written on success, GNUNET_SYSERR on error
*/
-ssize_t GNUNET_DISK_fn_write (const char * fn,
+ssize_t GNUNET_DISK_fn_write (const char *fn,
const void *buffer,
size_t n,
- int mode);
+ enum GNUNET_DISK_AccessPermissions mode);
/**
* Copy a file.
+ *
+ * @param src file to copy
+ * @param dst destination file name
* @return GNUNET_OK on success, GNUNET_SYSERR on error
*/
int GNUNET_DISK_file_copy (const char *src, const char *dst);
/**
- * Scan a directory for files. The name of the directory
- * must be expanded first (!).
+ * Scan a directory for files.
*
* @param dirName the name of the directory
* @param callback the method to call for each file
- * @param data argument to pass to callback
+ * @param callback_cls closure for callback
* @return the number of files found, -1 on error
*/
int GNUNET_DISK_directory_scan (const char *dirName,
GNUNET_FileNameCallback callback,
- void *data);
+ void *callback_cls);
/**
/**
- * Test if fil is a directory that can be accessed.
+ * Test if "fil" is a directory that can be accessed.
* Will not print an error message if the directory
* does not exist. Will log errors if GNUNET_SYSERR is
* returned.
#include "disk.h"
+/**
+ * Block size for IO for copying files.
+ */
+#define COPY_BLK_SIZE 65536
+
+
+
#if LINUX || CYGWIN
#include <sys/vfs.h>
#else
#endif
#endif
#endif
+#if LINUX
+#include <sys/statvfs.h>
+#endif
+
-typedef struct
+/**
+ * Handle used to manage a pipe.
+ */
+struct GNUNET_DISK_PipeHandle
{
+ /**
+ * File descriptors for the pipe.
+ */
+ struct GNUNET_DISK_FileHandle * fd[2];
+};
+
+
+/**
+ * Closure for the recursion to determine the file size
+ * of a directory.
+ */
+struct GetFileSizeData
+{
+ /**
+ * Set to the total file size.
+ */
uint64_t total;
- int include_sym_links;
-} GetFileSizeData;
+ /**
+ * GNUNET_YES if symbolic links should be included.
+ */
+ int include_sym_links;
+};
+/**
+ * Iterate over all files in the given directory and
+ * accumulate their size.
+ *
+ * @param cls closure of type "struct GetFileSizeData"
+ * @param fn current filename we are looking at
+ * @return GNUNET_SYSERR on serious errors, otherwise GNUNET_OK
+ */
static int
-getSizeRec (void *ptr, const char *fn)
+getSizeRec (void *cls, const char *fn)
{
- GetFileSizeData *gfsd = ptr;
+ struct GetFileSizeData *gfsd = cls;
#ifdef HAVE_STAT64
struct stat64 buf;
#else
return GNUNET_OK;
}
+
/**
* Checks whether a handle is invalid
+ *
* @param h handle to check
* @return GNUNET_YES if invalid, GNUNET_NO if valid
*/
GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
{
#ifdef MINGW
- return !h || h->h == INVALID_HANDLE_VALUE ? GNUNET_YES : GNUNET_NO;
+ return ((!h) || (h->h == INVALID_HANDLE_VALUE)) ? GNUNET_YES : GNUNET_NO;
#else
- return !h || h->fd == -1 ? GNUNET_YES : GNUNET_NO;
+ return ((!h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO;
#endif
}
/**
* Move the read/write pointer in a file
+ *
* @param h handle of an open file
* @param offset position to move to
* @param whence specification to which position the offset parameter relates to
*/
off_t
GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
- enum GNUNET_DISK_Seek whence)
+ enum GNUNET_DISK_Seek whence)
{
if (h == NULL)
{
#endif
}
+
/**
- * Get the size of the file (or directory)
- * of the given file (in bytes).
+ * Get the size of the file (or directory) of the given file (in
+ * bytes).
*
+ * @param filename name of the file or directory
+ * @param size set to the size of the file (or,
+ * in the case of directories, the sum
+ * of all sizes of files in the directory)
+ * @param includeSymLinks should symbolic links be
+ * included?
* @return GNUNET_SYSERR on error, GNUNET_OK on success
*/
int
uint64_t *size,
int includeSymLinks)
{
- GetFileSizeData gfsd;
+ struct GetFileSizeData gfsd;
int ret;
GNUNET_assert (size != NULL);
}
-
-#if LINUX
-#include <sys/statvfs.h>
-#endif
-
-
-
/**
- * FIXME.
+ * Obtain some unique identifiers for the given file
+ * that can be used to identify it in the local system.
+ * This function is used between GNUnet processes to
+ * quickly check if two files with the same absolute path
+ * are actually identical. The two processes represent
+ * the same peer but may communicate over the network
+ * (and the file may be on an NFS volume). This function
+ * may not be supported on all operating systems.
*
* @param filename name of the file
* @param dev set to the device ID
#endif
}
+
/**
- * Test if fil is a directory.
+ * Test if "fil" is a directory.
+ * Will not print an error message if the directory
+ * does not exist. Will log errors if GNUNET_SYSERR is
+ * returned (i.e., a file exists with the same name).
*
+ * @param fil filename to test
* @return GNUNET_YES if yes, GNUNET_NO if not, GNUNET_SYSERR if it
* does not exist
*/
/**
* Check that fil corresponds to a filename
* (of a file that exists and that is not a directory).
- * @returns GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
+ *
+ * @param fil filename to check
+ * @return GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
* else (will print an error message in that case, too).
*/
int
return GNUNET_YES;
}
+
/**
* Implementation of "mkdir -p"
* @param dir the directory to create
return ret;
}
+
/**
* Read the contents of a binary file into a buffer.
* @param h handle to an open file
* @return number of bytes read, GNUNET_SYSERR on failure
*/
ssize_t
-GNUNET_DISK_fn_read (const char * const fn,
+GNUNET_DISK_fn_read (const char * fn,
void *result,
size_t len)
{
struct GNUNET_DISK_FileHandle *fh;
ssize_t ret;
- fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ);
+ fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
+ GNUNET_DISK_PERM_NONE);
if (!fh)
return GNUNET_SYSERR;
ret = GNUNET_DISK_file_read (fh, result, len);
/**
* Write a buffer to a file. If the file is longer than the
- * number of bytes that will be written, iit will be truncated.
+ * number of bytes that will be written, it will be truncated.
*
* @param fn file name
* @param buffer the data to write
* @param n number of bytes to write
+ * @param mode file permissions
* @return GNUNET_OK on success, GNUNET_SYSERR on error
*/
ssize_t
-GNUNET_DISK_fn_write (const char * const fn, const void *buffer,
- size_t n, int mode)
+GNUNET_DISK_fn_write (const char * fn, const void *buffer,
+ size_t n,
+ enum GNUNET_DISK_AccessPermissions mode)
{
struct GNUNET_DISK_FileHandle *fh;
int ret;
}
/**
- * Scan a directory for files. The name of the directory
- * must be expanded first (!).
+ * Scan a directory for files.
+ *
* @param dirName the name of the directory
* @param callback the method to call for each file,
* can be NULL, in that case, we only count
- * @param data argument to pass to callback
+ * @param callback_cls closure for callback
* @return the number of files found, GNUNET_SYSERR on error or
* ieration aborted by callback returning GNUNET_SYSERR
*/
int
GNUNET_DISK_directory_scan (const char *dirName,
- GNUNET_FileNameCallback callback, void *data)
+ GNUNET_FileNameCallback callback,
+ void *callback_cls)
{
DIR *dinfo;
struct dirent *finfo;
dname,
(strcmp (dname, DIR_SEPARATOR_STR) ==
0) ? "" : DIR_SEPARATOR_STR, finfo->d_name);
- if (GNUNET_OK != callback (data, name))
+ if (GNUNET_OK != callback (callback_cls, name))
{
closedir (dinfo);
GNUNET_free (name);
}
+/**
+ * Function that removes the given directory by calling
+ * "GNUNET_DISK_directory_remove".
+ *
+ * @param unused not used
+ * @param fn directory to remove
+ * @return GNUNET_OK
+ */
static int
remove_helper (void *unused, const char *fn)
{
return GNUNET_OK;
}
+
/**
* Remove all files in a directory (rm -rf). Call with
* caution.
return GNUNET_SYSERR;
}
if (GNUNET_SYSERR ==
- GNUNET_DISK_directory_scan (fileName, remove_helper, NULL))
+ GNUNET_DISK_directory_scan (fileName,
+ &remove_helper,
+ NULL))
return GNUNET_SYSERR;
if (0 != RMDIR (fileName))
{
return GNUNET_OK;
}
-#define COPY_BLK_SIZE 65536
/**
* Copy a file.
+ *
+ * @param src file to copy
+ * @param dst destination file name
* @return GNUNET_OK on success, GNUNET_SYSERR on error
*/
int
if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES))
return GNUNET_SYSERR;
pos = 0;
- in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ);
+ in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ,
+ GNUNET_DISK_PERM_NONE);
if (!in)
return GNUNET_SYSERR;
out = GNUNET_DISK_file_open (dst, GNUNET_DISK_OPEN_WRITE
/**
* @brief Change owner of a file
+ *
+ * @param filename name of file to change the owner of
+ * @param user name of the new owner
+ * @return GNUNET_OK on success, GNUNET_SYSERR on failure
*/
int
GNUNET_DISK_file_change_owner (const char *filename, const char *user)
/**
- * Open a file
+ * Open a file. Note that the access permissions will only be
+ * used if a new file is created and if the underlying operating
+ * system supports the given permissions.
+ *
* @param fn file name to be opened
* @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
- * @param ... permissions for the newly created file
+ * @param perm permissions for the newly created file, use
+ * GNUNET_DISK_PERM_USER_NONE if a file could not be created by this
+ * call (because of flags)
* @return IO handle on success, NULL on error
*/
struct GNUNET_DISK_FileHandle *
GNUNET_DISK_file_open (const char *fn,
enum GNUNET_DISK_OpenFlags flags,
- ...)
+ enum GNUNET_DISK_AccessPermissions perm)
{
char *expfn;
struct GNUNET_DISK_FileHandle *ret;
oflags |= O_APPEND;
if (flags & GNUNET_DISK_OPEN_CREATE)
{
- int perm;
-
oflags |= O_CREAT;
-
- va_list arg;
- va_start (arg, flags);
- perm = va_arg (arg, int);
- va_end (arg);
-
if (perm & GNUNET_DISK_PERM_USER_READ)
mode |= S_IRUSR;
if (perm & GNUNET_DISK_PERM_USER_WRITE)
return ret;
}
+
/**
* Close an open file
* @param h file handle
return GNUNET_OK;
}
+
/**
* Construct full path to a file inside of the private
* directory used by GNUnet. Also creates the corresponding
return ret;
}
+
+/**
+ * Handle for a memory-mapping operation.
+ */
struct GNUNET_DISK_MapHandle
{
#ifdef MINGW
+ /**
+ * Underlying OS handle.
+ */
HANDLE h;
#else
+ /**
+ * Address where the map is in memory.
+ */
void *addr;
+
+ /**
+ * Number of bytes mapped.
+ */
size_t len;
#endif
};
/**
* Map a file into memory
+ *
* @param h open file handle
* @param m handle to the new mapping
* @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
GNUNET_DISK_pipe (int blocking)
{
struct GNUNET_DISK_PipeHandle *p;
+ struct GNUNET_DISK_FileHandle *fds;
int err;
err = GNUNET_NO;
- p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle));
-
+ p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle) + 2 * sizeof (struct GNUNET_DISK_FileHandle));
+ fds = (struct GNUNET_DISK_FileHandle *) &p[1];
+ p->fd[0] = &fds[0];
+ p->fd[1] = &fds[1];
#ifndef MINGW
int fd[2];
int ret;
int eno;
ret = pipe (fd);
- if (ret != -1)
+ if (ret == -1)
{
- p->fd[0] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
- p->fd[1] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
- p->fd[0]->fd = fd[0];
- p->fd[1]->fd = fd[1];
-
- if (!blocking)
- {
- flags = fcntl (fd[0], F_GETFL);
- flags |= O_NONBLOCK;
- ret = fcntl (fd[0], F_SETFL, flags);
- if (ret != -1)
- {
- flags = fcntl (fd[1], F_GETFL);
- flags |= O_NONBLOCK;
- ret = fcntl (fd[1], F_SETFL, flags);
- }
- if (ret == -1)
- {
- eno = errno;
- GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "fcntl");
- GNUNET_DISK_file_close (p->fd[0]);
- GNUNET_DISK_file_close (p->fd[1]);
- p->fd[0] = NULL;
- p->fd[1] = NULL;
- err = GNUNET_YES;
- errno = eno;
- }
- }
+ err = errno;
+ GNUNET_free (p);
+ errno = err;
+ return NULL;
+ }
+ p->fd[0]->fd = fd[0];
+ p->fd[1]->fd = fd[1];
+ if (!blocking)
+ {
+ flags = fcntl (fd[0], F_GETFL);
+ flags |= O_NONBLOCK;
+ ret = fcntl (fd[0], F_SETFL, flags);
+ if (ret != -1)
+ {
+ flags = fcntl (fd[1], F_GETFL);
+ flags |= O_NONBLOCK;
+ ret = fcntl (fd[1], F_SETFL, flags);
+ }
+ if (ret == -1)
+ {
+ eno = errno;
+ GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "fcntl");
+ close (p->fd[0]->fd);
+ close (p->fd[1]->fd);
+ err = GNUNET_YES;
+ GNUNET_free (p);
+ errno = eno;
+ return NULL;
+ }
}
- else
- err = GNUNET_YES;
#else
BOOL ret;
- ret = CreatePipe (&p->fd[0].h, &p->fd[1].h, NULL, 0);
- if (ret)
+ ret = CreatePipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0);
+ if (! ret)
{
- if (!blocking)
- {
- DWORD mode;
-
- mode = PIPE_NOWAIT;
- p->fd[0] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
- p->fd[1] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
- SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL);
- SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL);
- /* this always fails on Windows 95, so we don't care about error handling */
- }
+ GNUNET_free (p);
+ SetErrnoFromWinError (GetLastError ());
+ return NULL;
}
- else
- err = GNUNET_YES;
-#endif
-
- if (GNUNET_YES == err)
+ if (!blocking)
{
- GNUNET_free (p);
- p = NULL;
+ DWORD mode;
+
+ mode = PIPE_NOWAIT;
+ p->fd[0] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
+ p->fd[1] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle));
+ SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL);
+ SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL);
+ /* this always fails on Windows 95, so we don't care about error handling */
}
-
+#endif
return p;
}
GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
{
int ret = GNUNET_OK;
+ int save;
+
#ifdef MINGW
if (!CloseHandle (p->fd[0]->h))
{
SetErrnoFromWinError (GetLastError ());
ret = GNUNET_SYSERR;
}
-
if (!CloseHandle (p->fd[1]->h))
{
SetErrnoFromWinError (GetLastError ());
ret = GNUNET_SYSERR;
}
+ save = errno;
#else
- int save;
-
+ save = 0;
if (0 != close (p->fd[0]->fd))
{
ret = GNUNET_SYSERR;
save = errno;
}
- else
- save = 0;
-
if (0 != close (p->fd[1]->fd))
- ret = GNUNET_SYSERR;
- else
- errno = save;
+ {
+ ret = GNUNET_SYSERR;
+ save = errno;
+ }
#endif
- GNUNET_free (p->fd[0]);
- GNUNET_free (p->fd[1]);
GNUNET_free (p);
+ errno = save;
return ret;
}
/**
* Get the handle to a particular pipe end
* @param p pipe
- * @param n number of the end
+ * @param n end to access
*/
const struct GNUNET_DISK_FileHandle *
-GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, int n)
+GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
+ enum GNUNET_DISK_PipeEnd n)
{
- return p->fd[n];
+ switch (n)
+ {
+ case GNUNET_DISK_PIPE_END_READ:
+ case GNUNET_DISK_PIPE_END_WRITE:
+ return p->fd[n];
+ default:
+ GNUNET_break (0);
+ return NULL;
+ }
}
+
/**
* Retrieve OS file handle
* @internal