GNUnet  0.11.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  GlobClosure
struct  GNUNET_DISK_MapHandle
 Handle for a memory-mapping operation. More...


#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)


static int translate_unix_perms (enum GNUNET_DISK_AccessPermissions perm)
 Translate GNUnet-internal permission bitmap to UNIX file access permission bitmap. More...
static enum GNUNET_GenericReturnValue get_size_rec (void *cls, const char *fn)
 Iterate over all files in the given directory and accumulate their size. More...
enum GNUNET_GenericReturnValue GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
 Checks whether a handle is invalid. More...
enum GNUNET_GenericReturnValue 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...
enum GNUNET_GenericReturnValue 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...
enum GNUNET_GenericReturnValue 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...
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test (const char *fil, int is_readable)
 Test if fil is a directory and listable. More...
static enum GNUNET_GenericReturnValue file_test_internal (const char *fil, int amode)
 Check if fil can be accessed using amode. More...
enum GNUNET_GenericReturnValue 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...
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test_read (const char *fil)
 Check that fil corresponds to a filename and the file has read permissions. More...
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create (const char *dir)
 Implementation of "mkdir -p". More...
enum GNUNET_GenericReturnValue 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...
enum GNUNET_GenericReturnValue GNUNET_DISK_fn_write (const char *fn, const void *buf, size_t buf_size, enum GNUNET_DISK_AccessPermissions mode)
 Write a buffer to a file atomically. More...
int GNUNET_DISK_directory_scan (const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
 Scan a directory for files. More...
static bool glob_match (const char *pattern, const char *str)
 Check for a simple wildcard match. More...
static enum GNUNET_GenericReturnValue glob_cb (void *cls, const char *filename)
 Function called with a filename. More...
int GNUNET_DISK_glob (const char *glob_pattern, GNUNET_FileNameCallback callback, void *callback_cls)
 Find all files matching a glob pattern. More...
static enum GNUNET_GenericReturnValue remove_helper (void *unused, const char *fn)
 Function that removes the given directory by calling GNUNET_DISK_directory_remove(). More...
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove (const char *filename)
 Remove all files in a directory (rm -rf). More...
enum GNUNET_GenericReturnValue 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...
enum GNUNET_GenericReturnValue GNUNET_DISK_file_change_owner (const char *filename, const char *user)
 Change owner 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...
enum GNUNET_GenericReturnValue 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 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...
enum GNUNET_GenericReturnValue GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
 Unmap a file. More...
enum GNUNET_GenericReturnValue GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
 Write file changes to disk. More...
struct GNUNET_DISK_PipeHandleGNUNET_DISK_pipe (enum GNUNET_DISK_PipeFlags pf)
 Creates an interprocess channel. More...
struct GNUNET_DISK_PipeHandleGNUNET_DISK_pipe_from_fd (enum GNUNET_DISK_PipeFlags pf, int fd[2])
 Creates a pipe object from a couple of file descriptors. More...
enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd end)
 Closes one half of 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...
enum GNUNET_GenericReturnValue 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...
enum GNUNET_GenericReturnValue GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh, void *dst, size_t dst_len)
 Retrieve OS file handle. More...
static enum GNUNET_GenericReturnValue 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

Christian Grothoff
Nils Durner

Definition in file disk.c.

Macro Definition Documentation


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

Definition at line 31 of file disk.c.


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

Definition at line 33 of file disk.c.


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

Definition at line 36 of file disk.c.


#define COPY_BLK_SIZE   65536

Block size for IO for copying files.

Definition at line 42 of file disk.c.


#define _IFMT   0170000 /* type of file */

Definition at line 59 of file disk.c.


#define _IFLNK   0120000 /* symbolic link */

Definition at line 60 of file disk.c.


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

Definition at line 61 of file disk.c.


#define MAP_FAILED   ((void *) -1)

Definition at line 1373 of file disk.c.

Function Documentation

◆ translate_unix_perms()

static int translate_unix_perms ( enum GNUNET_DISK_AccessPermissions  perm)

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

permfile permissions, GNUnet style
file permissions, UNIX style

Definition at line 109 of file disk.c.

110 {
111  int mode;
113  mode = 0;
115  mode |= S_IRUSR;
117  mode |= S_IWUSR;
119  mode |= S_IXUSR;
121  mode |= S_IRGRP;
123  mode |= S_IWGRP;
125  mode |= S_IXGRP;
127  mode |= S_IROTH;
129  mode |= S_IWOTH;
131  mode |= S_IXOTH;
133  return mode;
134 }
static enum @8 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
Everybody can execute.
Owner can execute.
Owner can read.
Group can read.
Group can execute.
Group can write.
Owner can write.
Everybody can read.
Everybody can write.

Referenced by GNUNET_DISK_file_close().

Here is the caller graph for this function:

◆ get_size_rec()

static enum GNUNET_GenericReturnValue get_size_rec ( void *  cls,
const char *  fn 

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

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

Definition at line 109 of file disk.c.

147 {
148  struct GetFileSizeData *gfsd = cls;
150 #if defined(HAVE_STAT64) && \
151  ! (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64)
152  struct stat64 buf;
154  if (0 != stat64 (fn, &buf))
155  {
157  return GNUNET_SYSERR;
158  }
159 #else
160  struct stat buf;
162  if (0 != stat (fn, &buf))
163  {
165  return GNUNET_SYSERR;
166  }
167 #endif
168  if ((S_ISDIR (buf.st_mode)) && (gfsd->single_file_mode == GNUNET_YES))
169  {
170  errno = EISDIR;
171  return GNUNET_SYSERR;
172  }
173  if ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
174  gfsd->total += buf.st_size;
175  if ((S_ISDIR (buf.st_mode)) && (0 == access (fn, X_OK)) &&
176  ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
177  {
179  return GNUNET_SYSERR;
180  }
181  return GNUNET_OK;
182 }
static enum GNUNET_GenericReturnValue get_size_rec(void *cls, const char *fn)
Iterate over all files in the given directory and accumulate their size.
Definition: disk.c:146
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:36
#define S_ISLNK(m)
Definition: disk.c:61
static char buf[2048]
Definition: gnunet_common.h:95
Definition: gnunet_common.h:97
Definition: gnunet_common.h:93
static char * fn
Filename of the unique file.
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition: disk.c:814
Closure for the recursion to determine the file size of a directory.
Definition: disk.c:83
uint64_t total
Set to the total file size.
Definition: disk.c:87
int include_sym_links
GNUNET_YES if symbolic links should be included.
Definition: disk.c:92
int single_file_mode
GNUNET_YES if mode is file-only (return total == -1 for directories).
Definition: disk.c:97

◆ mktemp_name()

static char* mktemp_name ( const char *  t)

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

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

Definition at line 293 of file disk.c.

294 {
295  const char *tmpdir;
296  char *tmpl;
297  char *fn;
299  if ((t[0] != '/') && (t[0] != '\\'))
300  {
301  /* FIXME: This uses system codepage on W32, not UTF-8 */
302  tmpdir = getenv ("TMPDIR");
303  if (NULL == tmpdir)
304  tmpdir = getenv ("TMP");
305  if (NULL == tmpdir)
306  tmpdir = getenv ("TEMP");
307  if (NULL == tmpdir)
308  tmpdir = "/tmp";
309  GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX");
310  }
311  else
312  {
313  GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX");
314  }
315  fn = tmpl;
316  return fn;
317 }
char * getenv()
static struct GNUNET_SCHEDULER_Task * t
Main task.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.

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

Referenced by GNUNET_DISK_directory_create(), and GNUNET_DISK_mkdtemp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ file_test_internal()

static enum GNUNET_GenericReturnValue file_test_internal ( const char *  fil,
int  amode 

Check if fil can be accessed using amode.

filfile to check for
amodeaccess mode
GNUnet error code

Definition at line 381 of file disk.c.

444 {
445  struct stat filestat;
446  int ret;
447  char *rdir;
449  rdir = GNUNET_STRINGS_filename_expand (fil);
450  if (rdir == NULL)
451  return GNUNET_SYSERR;
453  ret = stat (rdir, &filestat);
454  if (0 != ret)
455  {
456  if (errno != ENOENT)
457  {
459  GNUNET_free (rdir);
460  return GNUNET_SYSERR;
461  }
462  GNUNET_free (rdir);
463  return GNUNET_NO;
464  }
465  if (! S_ISREG (filestat.st_mode))
466  {
467  GNUNET_free (rdir);
468  return GNUNET_NO;
469  }
470  if (access (rdir, amode) < 0)
471  {
473  GNUNET_free (rdir);
474  return GNUNET_SYSERR;
475  }
476  GNUNET_free (rdir);
477  return GNUNET_YES;
478 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Definition: gnunet_common.h:94
#define GNUNET_free(ptr)
Wrapper around free.
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition: strings.c:482

◆ glob_match()

static bool glob_match ( const char *  pattern,
const char *  str 

Check for a simple wildcard match.

Only asterisks are allowed. Asterisks match everything, including slashes.

patternpattern with wildcards
strstring to match against
true on match, false otherwise

Definition at line 915 of file disk.c.

916 {
917  /* Position in the input string */
918  const char *str_pos = str;
919  /* Position in the pattern */
920  const char *pat_pos = pattern;
921  /* Backtrack position in string */
922  const char *str_bt = NULL;
923  /* Backtrack position in pattern */
924  const char *pat_bt = NULL;
926  for (;;)
927  {
928  if (*pat_pos == '*')
929  {
930  str_bt = str_pos;
931  pat_bt = pat_pos++;
932  }
933  else if (*pat_pos == *str_pos)
934  {
935  if ('\0' == *pat_pos)
936  return true;
937  str_pos++;
938  pat_pos++;
939  }
940  else
941  {
942  if (NULL == str_bt)
943  return false;
944  /* Backtrack to match one more
945  character as part of the asterisk. */
946  str_pos = str_bt + 1;
947  if ('\0' == *str_pos)
948  return false;
949  pat_pos = pat_bt;
950  }
951  }
952 }

◆ glob_cb()

static enum GNUNET_GenericReturnValue glob_cb ( void *  cls,
const char *  filename 

Function called with a filename.

filenamecomplete filename (absolute path)
GNUNET_OK to continue to iterate, GNUNET_NO to stop iteration with no error, GNUNET_SYSERR to abort iteration with error!

Definition at line 915 of file disk.c.

978 {
979  struct GlobClosure *gc = cls;
980  const char *fn;
982  fn = strrchr (filename, DIR_SEPARATOR);
983  fn = (NULL == fn) ? filename : (fn + 1);
986  "checking glob '%s' against '%s'\n",
987  gc->glob,
988  fn);
990  if (glob_match (gc->glob, fn))
991  {
992  enum GNUNET_GenericReturnValue cbret;
995  "found glob match '%s'\n",
996  filename);
997  gc->nres++;
998  cbret = gc->cb (gc->cls, filename);
999  if (GNUNET_OK != cbret)
1000  return cbret;
1001  }
1002  return GNUNET_OK;
1003 }
static bool glob_match(const char *pattern, const char *str)
Check for a simple wildcard match.
Definition: disk.c:915
#define LOG(kind,...)
Definition: disk.c:31
static char * filename
Named constants for return values.
Definition: gnunet_common.h:92
Definition: platform.h:164
GNUNET_FileNameCallback cb
Definition: disk.c:957
void * cls
Definition: disk.c:958
int nres
Number of files that actually matched the glob pattern.
Definition: disk.c:963
const char * glob
Definition: disk.c:956

Referenced by GNUNET_DISK_directory_remove().

Here is the caller graph for this function:

◆ remove_helper()

static enum GNUNET_GenericReturnValue remove_helper ( void *  unused,
const char *  fn 

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

unusednot used
fndirectory to remove

Definition at line 1007 of file disk.c.

1077 {
1078  (void) unused;
1080  return GNUNET_OK;
1081 }
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove(const char *filename)
Remove all files in a directory (rm -rf).
Definition: disk.c:1085

◆ GNUNET_DISK_internal_file_handle_()

enum GNUNET_GenericReturnValue GNUNET_DISK_internal_file_handle_ ( const struct GNUNET_DISK_FileHandle fh,
void *  dst,
size_t  dst_len 

Retrieve OS file handle.

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

Definition at line 1616 of file disk.c.

1636 {
1637  if (NULL == fh)
1638  return GNUNET_SYSERR;
1639  if (dst_len < sizeof(int))
1640  return GNUNET_SYSERR;
1641  *((int *) dst) = fh->fd;
1642  return GNUNET_OK;
1643 }
static int fh
Handle to the unique file.


Referenced by GNUNET_NETWORK_fdset_handle_set(), and start_process().

Here is the caller graph for this function:

◆ purge_cfg_dir()

static enum GNUNET_GenericReturnValue purge_cfg_dir ( void *  cls,
const struct GNUNET_CONFIGURATION_Handle cfg 

Helper function for GNUNET_DISK_purge_cfg_dir.

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

Definition at line 1616 of file disk.c.

1656 {
1657  const char *option = cls;
1658  char *tmpname;
1660  if (GNUNET_OK !=
1661  GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", option, &tmpname))
1662  {
1664  return GNUNET_NO;
1665  }
1667  {
1669  GNUNET_free (tmpname);
1670  return GNUNET_OK;
1671  }
1672  GNUNET_free (tmpname);
1673  return GNUNET_OK;
1674 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
enum GNUNET_GenericReturnValue 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.
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 'level' that indicates a failure of the command 'cmd' with the mess...

Referenced by GNUNET_DISK_purge_cfg_dir().

Here is the caller graph for this function: