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 42 of file disk.c.

Referenced by GNUNET_DISK_file_copy().

◆ _IFMT

#define _IFMT   0170000 /* type of file */

Definition at line 59 of file disk.c.

◆ _IFLNK

#define _IFLNK   0120000 /* symbolic link */

Definition at line 60 of file disk.c.

◆ S_ISLNK

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

Definition at line 61 of file disk.c.

Referenced by getSizeRec(), and GNUNET_STRINGS_check_filename().

◆ MAP_FAILED

#define MAP_FAILED   ((void *)-1)

Definition at line 1420 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 107 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().

108 {
109  int mode;
110 
111  mode = 0;
112  if (perm & GNUNET_DISK_PERM_USER_READ)
113  mode |= S_IRUSR;
114  if (perm & GNUNET_DISK_PERM_USER_WRITE)
115  mode |= S_IWUSR;
116  if (perm & GNUNET_DISK_PERM_USER_EXEC)
117  mode |= S_IXUSR;
118  if (perm & GNUNET_DISK_PERM_GROUP_READ)
119  mode |= S_IRGRP;
120  if (perm & GNUNET_DISK_PERM_GROUP_WRITE)
121  mode |= S_IWGRP;
122  if (perm & GNUNET_DISK_PERM_GROUP_EXEC)
123  mode |= S_IXGRP;
124  if (perm & GNUNET_DISK_PERM_OTHER_READ)
125  mode |= S_IROTH;
126  if (perm & GNUNET_DISK_PERM_OTHER_WRITE)
127  mode |= S_IWOTH;
128  if (perm & GNUNET_DISK_PERM_OTHER_EXEC)
129  mode |= S_IXOTH;
130 
131  return mode;
132 }
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 144 of file disk.c.

References 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, and GetFileSizeData::total.

Referenced by GNUNET_DISK_file_size().

145 {
146  struct GetFileSizeData *gfsd = cls;
147 
148 #if defined(HAVE_STAT64) && \
149  !(defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64)
150  struct stat64 buf;
151 
152  if (0 != stat64(fn, &buf))
153  {
155  return GNUNET_SYSERR;
156  }
157 #else
158  struct stat buf;
159 
160  if (0 != stat(fn, &buf))
161  {
163  return GNUNET_SYSERR;
164  }
165 #endif
166  if ((S_ISDIR(buf.st_mode)) && (gfsd->single_file_mode == GNUNET_YES))
167  {
168  errno = EISDIR;
169  return GNUNET_SYSERR;
170  }
171  if ((!S_ISLNK(buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
172  gfsd->total += buf.st_size;
173  if ((S_ISDIR(buf.st_mode)) && (0 == access(fn, X_OK)) &&
174  ((!S_ISLNK(buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
175  {
177  return GNUNET_SYSERR;
178  }
179  return GNUNET_OK;
180 }
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:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition: disk.c:909
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:144
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
#define S_ISLNK(m)
Definition: disk.c:61
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:36
#define GNUNET_YES
Definition: gnunet_common.h:77
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 340 of file disk.c.

References fn, getenv(), and GNUNET_asprintf().

Referenced by GNUNET_DISK_mkdtemp(), and GNUNET_DISK_mktemp().

341 {
342  const char *tmpdir;
343  char *tmpl;
344  char *fn;
345 
346  if ((t[0] != '/') && (t[0] != '\\'))
347  {
348  /* FIXME: This uses system codepage on W32, not UTF-8 */
349  tmpdir = getenv("TMPDIR");
350  if (NULL == tmpdir)
351  tmpdir = getenv("TMP");
352  if (NULL == tmpdir)
353  tmpdir = getenv("TEMP");
354  if (NULL == tmpdir)
355  tmpdir = "/tmp";
356  GNUNET_asprintf(&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX");
357  }
358  else
359  {
360  GNUNET_asprintf(&tmpl, "%s%s", t, "XXXXXX");
361  }
362  fn = tmpl;
363  return fn;
364 }
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.
char * getenv()
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 1010 of file disk.c.

References GNUNET_DISK_directory_remove(), and GNUNET_OK.

Referenced by GNUNET_DISK_directory_remove().

1011 {
1012  (void)unused;
1014  return GNUNET_OK;
1015 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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:1026
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 1769 of file disk.c.

References GNUNET_DISK_FileHandle::fd, GNUNET_OK, and GNUNET_SYSERR.

Referenced by GNUNET_NETWORK_fdset_handle_set(), and start_process().

1772 {
1773  if (NULL == fh)
1774  return GNUNET_SYSERR;
1775 
1776  if (dst_len < sizeof(int))
1777  return GNUNET_SYSERR;
1778  *((int *)dst) = fh->fd;
1779 
1780  return GNUNET_OK;
1781 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
int fd
File handle on Unix-like systems.
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 1792 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().

1793 {
1794  const char *option = cls;
1795  char *tmpname;
1796 
1797  if (GNUNET_OK !=
1798  GNUNET_CONFIGURATION_get_value_filename(cfg, "PATHS", option, &tmpname))
1799  {
1801  return GNUNET_NO;
1802  }
1804  {
1806  GNUNET_free(tmpname);
1807  return GNUNET_OK;
1808  }
1809  GNUNET_free(tmpname);
1810  return GNUNET_OK;
1811 }
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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:1026
static char * option
Name of the option.
Definition: gnunet-config.c:38
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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: