GNUnet  0.10.x
Data Structures | Macros | Functions
disk.c File Reference

disk IO convenience methods More...

#include "platform.h"
#include "disk.h"
#include "gnunet_strings_lib.h"
#include "gnunet_disk_lib.h"
#include <sys/types.h>
Include dependency graph for disk.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_DISK_PipeHandle
 Handle used to manage a pipe. More...
 
struct  GetFileSizeData
 Closure for the recursion to determine the file size of a directory. More...
 
struct  GNUNET_DISK_MapHandle
 Handle for a memory-mapping operation. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-disk", __VA_ARGS__)
 
#define LOG_STRERROR(kind, syscall)   GNUNET_log_from_strerror (kind, "util-disk", syscall)
 
#define LOG_STRERROR_FILE(kind, syscall, filename)   GNUNET_log_from_strerror_file (kind, "util-disk", syscall, filename)
 
#define COPY_BLK_SIZE   65536
 Block size for IO for copying files. More...
 
#define _IFMT   0170000 /* type of file */
 
#define _IFLNK   0120000 /* symbolic link */
 
#define S_ISLNK(m)   (((m)&_IFMT) == _IFLNK)
 
#define MAP_FAILED   ((void *) -1)
 

Functions

static int translate_unix_perms (enum GNUNET_DISK_AccessPermissions perm)
 Translate GNUnet-internal permission bitmap to UNIX file access permission bitmap. More...
 
static int getSizeRec (void *cls, const char *fn)
 Iterate over all files in the given directory and accumulate their size. More...
 
int GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
 Checks whether a handle is invalid. More...
 
int GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, off_t *size)
 Get the size of an open file. More...
 
off_t GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset, enum GNUNET_DISK_Seek whence)
 Move the read/write pointer in a file. More...
 
int GNUNET_DISK_file_size (const char *filename, uint64_t *size, int include_symbolic_links, int single_file_mode)
 Get the size of the file (or directory) of the given file (in bytes). More...
 
int GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t *dev, uint64_t *ino)
 Obtain some unique identifiers for the given file that can be used to identify it in the local system. More...
 
static char * mktemp_name (const char *t)
 Create the name for a temporary file or directory from a template. More...
 
void GNUNET_DISK_fix_permissions (const char *fn, int require_uid_match, int require_gid_match)
 Update POSIX permissions mask of a file on disk. More...
 
char * GNUNET_DISK_mkdtemp (const char *t)
 Create an (empty) temporary directory on disk. More...
 
void GNUNET_DISK_file_backup (const char *fil)
 Move a file out of the way (create a backup) by renaming it to "orig.NUM~" where NUM is the smallest number that is not used yet. More...
 
char * GNUNET_DISK_mktemp (const char *t)
 Create an (empty) temporary file on disk. More...
 
int GNUNET_DISK_directory_test (const char *fil, int is_readable)
 Test if fil is a directory and listable. More...
 
int GNUNET_DISK_file_test (const char *fil)
 Check that fil corresponds to a filename (of a file that exists and that is not a directory). More...
 
int GNUNET_DISK_directory_create (const char *dir)
 Implementation of "mkdir -p". More...
 
int GNUNET_DISK_directory_create_for_file (const char *filename)
 Create the directory structure for storing a file. More...
 
ssize_t GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
 Read the contents of a binary file into a buffer. More...
 
ssize_t GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
 Read the contents of a binary file into a buffer. More...
 
ssize_t GNUNET_DISK_fn_read (const char *fn, void *result, size_t len)
 Read the contents of a binary file into a buffer. More...
 
ssize_t GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
 Write a buffer to a file. More...
 
ssize_t GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
 Write a buffer to a file, blocking, if necessary. More...
 
ssize_t GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n, enum GNUNET_DISK_AccessPermissions mode)
 Write a buffer to a file. More...
 
int GNUNET_DISK_directory_scan (const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
 Scan a directory for files. More...
 
static int remove_helper (void *unused, const char *fn)
 Function that removes the given directory by calling GNUNET_DISK_directory_remove(). More...
 
int GNUNET_DISK_directory_remove (const char *filename)
 Remove all files in a directory (rm -r). More...
 
int GNUNET_DISK_file_copy (const char *src, const char *dst)
 Copy a file. More...
 
void GNUNET_DISK_filename_canonicalize (char *fn)
 Removes special characters as ':' from a filename. More...
 
int GNUNET_DISK_file_change_owner (const char *filename, const char *user)
 Change owner of a file. More...
 
int GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lock_start, off_t lock_end, int excl)
 Lock a part of a file. More...
 
int GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlock_start, off_t unlock_end)
 Unlock a part of a file. More...
 
struct GNUNET_DISK_FileHandleGNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
 Open a file. More...
 
int GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
 Close an open file. More...
 
struct GNUNET_DISK_FileHandleGNUNET_DISK_get_handle_from_int_fd (int fno)
 Get a handle from a native integer FD. More...
 
struct GNUNET_DISK_FileHandleGNUNET_DISK_get_handle_from_native (FILE *fd)
 Get a handle from a native streaming FD. More...
 
void * GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, struct GNUNET_DISK_MapHandle **m, enum GNUNET_DISK_MapType access, size_t len)
 Map a file into memory. More...
 
int GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
 Unmap a file. More...
 
int GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
 Write file changes to disk. More...
 
struct GNUNET_DISK_PipeHandleGNUNET_DISK_pipe (int blocking_read, int blocking_write, int inherit_read, int inherit_write)
 Creates an interprocess channel. More...
 
struct GNUNET_DISK_PipeHandleGNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2])
 Creates a pipe object from a couple of file descriptors. More...
 
int GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd end)
 Closes an interprocess channel. More...
 
struct GNUNET_DISK_FileHandleGNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd end)
 Detaches one of the ends from the pipe. More...
 
int GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
 Closes an interprocess channel. More...
 
const struct GNUNET_DISK_FileHandleGNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd n)
 Get the handle to a particular pipe end. More...
 
int GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh, void *dst, size_t dst_len)
 Retrieve OS file handle. More...
 
static int purge_cfg_dir (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
 Helper function for GNUNET_DISK_purge_cfg_dir. More...
 
void GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, const char *option)
 Remove the directory given under option in section [PATHS] in configuration under cfg_filename. More...
 

Detailed Description

disk IO convenience methods

Author
Christian Grothoff
Nils Durner

Definition in file disk.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util-disk", __VA_ARGS__)

◆ LOG_STRERROR

#define LOG_STRERROR (   kind,
  syscall 
)    GNUNET_log_from_strerror (kind, "util-disk", syscall)

Definition at line 33 of file disk.c.

Referenced by GNUNET_DISK_file_close(), GNUNET_DISK_pipe(), and GNUNET_DISK_pipe_from_fd().

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  syscall,
  filename 
)    GNUNET_log_from_strerror_file (kind, "util-disk", syscall, filename)

◆ COPY_BLK_SIZE

#define COPY_BLK_SIZE   65536

Block size for IO for copying files.

Definition at line 40 of file disk.c.

Referenced by GNUNET_DISK_file_copy().

◆ _IFMT

#define _IFMT   0170000 /* type of file */

Definition at line 57 of file disk.c.

◆ _IFLNK

#define _IFLNK   0120000 /* symbolic link */

Definition at line 58 of file disk.c.

◆ S_ISLNK

#define S_ISLNK (   m)    (((m)&_IFMT) == _IFLNK)

Definition at line 59 of file disk.c.

Referenced by getSizeRec(), and GNUNET_STRINGS_check_filename().

◆ MAP_FAILED

#define MAP_FAILED   ((void *) -1)

Definition at line 2007 of file disk.c.

Referenced by GNUNET_DISK_file_map().

Function Documentation

◆ translate_unix_perms()

static int translate_unix_perms ( enum GNUNET_DISK_AccessPermissions  perm)
static

Translate GNUnet-internal permission bitmap to UNIX file access permission bitmap.

Parameters
permfile permissions, GNUnet style
Returns
file permissions, UNIX style

Definition at line 108 of file disk.c.

References GNUNET_DISK_PERM_GROUP_EXEC, GNUNET_DISK_PERM_GROUP_READ, GNUNET_DISK_PERM_GROUP_WRITE, GNUNET_DISK_PERM_OTHER_EXEC, GNUNET_DISK_PERM_OTHER_READ, GNUNET_DISK_PERM_OTHER_WRITE, GNUNET_DISK_PERM_USER_EXEC, GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, and mode.

Referenced by GNUNET_DISK_file_open().

109 {
110  int mode;
111 
112  mode = 0;
113  if (perm & GNUNET_DISK_PERM_USER_READ)
114  mode |= S_IRUSR;
115  if (perm & GNUNET_DISK_PERM_USER_WRITE)
116  mode |= S_IWUSR;
117  if (perm & GNUNET_DISK_PERM_USER_EXEC)
118  mode |= S_IXUSR;
119  if (perm & GNUNET_DISK_PERM_GROUP_READ)
120  mode |= S_IRGRP;
121  if (perm & GNUNET_DISK_PERM_GROUP_WRITE)
122  mode |= S_IWGRP;
123  if (perm & GNUNET_DISK_PERM_GROUP_EXEC)
124  mode |= S_IXGRP;
125  if (perm & GNUNET_DISK_PERM_OTHER_READ)
126  mode |= S_IROTH;
127  if (perm & GNUNET_DISK_PERM_OTHER_WRITE)
128  mode |= S_IWOTH;
129  if (perm & GNUNET_DISK_PERM_OTHER_EXEC)
130  mode |= S_IXOTH;
131 
132  return mode;
133 }
Everybody can execute.
Everybody can write.
Everybody can read.
static enum @11 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
Here is the caller graph for this function:

◆ getSizeRec()

static int getSizeRec ( void *  cls,
const char *  fn 
)
static

Iterate over all files in the given directory and accumulate their size.

Parameters
clsclosure of type struct GetFileSizeData
fncurrent filename we are looking at
Returns
GNUNET_SYSERR on serious errors, otherwise GNUNET_OK

Definition at line 146 of file disk.c.

References ACCESS, buf, GNUNET_DISK_directory_scan(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, GetFileSizeData::include_sym_links, LOG_STRERROR_FILE, S_ISLNK, GetFileSizeData::single_file_mode, STAT, STAT64, STRUCT_STAT64, and GetFileSizeData::total.

Referenced by GNUNET_DISK_file_size().

147 {
148  struct GetFileSizeData *gfsd = cls;
149 
150 #if defined (HAVE_STAT64) && !(defined (_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64)
152 
153  if (0 != STAT64 (fn, &buf))
154  {
156  return GNUNET_SYSERR;
157  }
158 #else
159  struct stat buf;
160 
161  if (0 != STAT (fn, &buf))
162  {
164  return GNUNET_SYSERR;
165  }
166 #endif
167  if ((S_ISDIR (buf.st_mode)) && (gfsd->single_file_mode == GNUNET_YES))
168  {
169  errno = EISDIR;
170  return GNUNET_SYSERR;
171  }
172  if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
173  gfsd->total += buf.st_size;
174  if ((S_ISDIR (buf.st_mode)) && (0 == ACCESS (fn, X_OK)) &&
175  ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
176  {
178  return GNUNET_SYSERR;
179  }
180  return GNUNET_OK;
181 }
uint64_t total
Set to the total file size.
Definition: disk.c:85
int single_file_mode
GNUNET_YES if mode is file-only (return total == -1 for directories).
Definition: disk.c:95
Closure for the recursion to determine the file size of a directory.
Definition: disk.c:80
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define STAT(p, b)
Definition: plibc.h:663
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition: disk.c:1233
static char * fn
Filename of the unique file.
static char buf[2048]
int include_sym_links
GNUNET_YES if symbolic links should be included.
Definition: disk.c:90
static int getSizeRec(void *cls, const char *fn)
Iterate over all files in the given directory and accumulate their size.
Definition: disk.c:146
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
#define S_ISLNK(m)
Definition: disk.c:59
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:35
#define STRUCT_STAT64
Definition: plibc.h:731
#define STAT64(p, b)
Definition: plibc.h:664
#define GNUNET_YES
Definition: gnunet_common.h:80
#define ACCESS(p, m)
Definition: plibc.h:656
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mktemp_name()

static char* mktemp_name ( const char *  t)
static

Create the name for a temporary file or directory from a template.

Parameters
ttemplate (without XXXXX or "/tmp/")
Returns
name ready for passing to 'mktemp' or 'mkdtemp', NULL on error

Definition at line 397 of file disk.c.

References fn, getenv(), GNUNET_asprintf(), GNUNET_DISK_fix_permissions(), GNUNET_free, GNUNET_malloc, GNUNET_strdup, plibc_conv_to_win_path(), and WINDOWS.

Referenced by GNUNET_DISK_mkdtemp(), and GNUNET_DISK_mktemp().

398 {
399  const char *tmpdir;
400  char *tmpl;
401  char *fn;
402 
403  if ((t[0] != '/') && (t[0] != '\\')
404 #if WINDOWS
405  && !(isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':'))
406 #endif
407  )
408  {
409  /* FIXME: This uses system codepage on W32, not UTF-8 */
410  tmpdir = getenv ("TMPDIR");
411  if (NULL == tmpdir)
412  tmpdir = getenv ("TMP");
413  if (NULL == tmpdir)
414  tmpdir = getenv ("TEMP");
415  if (NULL == tmpdir)
416  tmpdir = "/tmp";
417  GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX");
418  }
419  else
420  {
421  GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX");
422  }
423 #ifdef MINGW
424  fn = (char *) GNUNET_malloc (MAX_PATH + 1);
425  if (ERROR_SUCCESS != plibc_conv_to_win_path (tmpl, fn))
426  {
427  GNUNET_free (fn);
428  GNUNET_free (tmpl);
429  return NULL;
430  }
431  GNUNET_free (tmpl);
432 #else
433  fn = tmpl;
434 #endif
435  return fn;
436 }
#define WINDOWS
Definition: w32nsp.c:44
static struct GNUNET_SCHEDULER_Task * t
Main task.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
static char * fn
Filename of the unique file.
int plibc_conv_to_win_path(const char *pszUnix, char *pszWindows)
char * getenv()
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove_helper()

static int remove_helper ( void *  unused,
const char *  fn 
)
static

Function that removes the given directory by calling GNUNET_DISK_directory_remove().

Parameters
unusednot used
fndirectory to remove
Returns
GNUNET_OK

Definition at line 1342 of file disk.c.

References GNUNET_DISK_directory_remove(), and GNUNET_OK.

Referenced by GNUNET_DISK_directory_remove().

1344 {
1345  (void) unused;
1347  return GNUNET_OK;
1348 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static char * fn
Filename of the unique file.
int GNUNET_DISK_directory_remove(const char *filename)
Remove all files in a directory (rm -r).
Definition: disk.c:1359
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_DISK_internal_file_handle_()

int GNUNET_DISK_internal_file_handle_ ( const struct GNUNET_DISK_FileHandle fh,
void *  dst,
size_t  dst_len 
)

Retrieve OS file handle.

Parameters
fhGNUnet file descriptor
dstdestination buffer
dst_lenlength of dst
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 2665 of file disk.c.

References GNUNET_DISK_FileHandle::fd, GNUNET_OK, and GNUNET_SYSERR.

Referenced by GNUNET_NETWORK_fdset_handle_set(), and start_process().

2667 {
2668  if (NULL == fh)
2669  return GNUNET_SYSERR;
2670 #ifdef MINGW
2671  if (dst_len < sizeof (HANDLE))
2672  return GNUNET_SYSERR;
2673  *((HANDLE *) dst) = fh->h;
2674 #else
2675  if (dst_len < sizeof (int))
2676  return GNUNET_SYSERR;
2677  *((int *) dst) = fh->fd;
2678 #endif
2679 
2680  return GNUNET_OK;
2681 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
int fd
File handle on other OSes.
Here is the caller graph for this function:

◆ purge_cfg_dir()

static int purge_cfg_dir ( void *  cls,
const struct GNUNET_CONFIGURATION_Handle cfg 
)
static

Helper function for GNUNET_DISK_purge_cfg_dir.

Parameters
clsa const char * with the option to purge
cfgour configuration
Returns
GNUNET_OK on success

Definition at line 2692 of file disk.c.

References GNUNET_CONFIGURATION_get_value_filename(), GNUNET_DISK_directory_remove(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log_config_missing(), GNUNET_log_strerror_file, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, and option.

Referenced by GNUNET_DISK_purge_cfg_dir().

2694 {
2695  const char *option = cls;
2696  char *tmpname;
2697 
2698  if (GNUNET_OK !=
2700  "PATHS",
2701  option,
2702  &tmpname))
2703  {
2705  "PATHS",
2706  option);
2707  return GNUNET_NO;
2708  }
2709  if (GNUNET_SYSERR ==
2710  GNUNET_DISK_directory_remove (tmpname))
2711  {
2713  "remove",
2714  tmpname);
2715  GNUNET_free (tmpname);
2716  return GNUNET_OK;
2717  }
2718  GNUNET_free (tmpname);
2719  return GNUNET_OK;
2720 }
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
int GNUNET_DISK_directory_remove(const char *filename)
Remove all files in a directory (rm -r).
Definition: disk.c:1359
static char * option
Name of the option.
Definition: gnunet-config.c:38
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function: