GNUnet  0.19.2
disk.c File Reference

disk IO convenience methods More...

#include "platform.h"
#include "gnunet_util_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 30 of file disk.c.


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

Definition at line 32 of file disk.c.


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

Definition at line 35 of file disk.c.


#define COPY_BLK_SIZE   65536

Block size for IO for copying files.

Definition at line 41 of file disk.c.


#define _IFMT   0170000 /* type of file */

Definition at line 58 of file disk.c.


#define _IFLNK   0120000 /* symbolic link */

Definition at line 59 of file disk.c.


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

Definition at line 60 of file disk.c.


#define MAP_FAILED   ((void *) -1)

Definition at line 1372 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 108 of file disk.c.

109 {
110  int mode;
112  mode = 0;
114  mode |= S_IRUSR;
116  mode |= S_IWUSR;
118  mode |= S_IXUSR;
120  mode |= S_IRGRP;
122  mode |= S_IWGRP;
124  mode |= S_IXGRP;
126  mode |= S_IROTH;
128  mode |= S_IWOTH;
130  mode |= S_IXOTH;
132  return mode;
133 }
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 108 of file disk.c.

146 {
147  struct GetFileSizeData *gfsd = cls;
149 #if defined(HAVE_STAT64) && \
150  ! (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64)
151  struct stat64 buf;
153  if (0 != stat64 (fn, &buf))
154  {
156  return GNUNET_SYSERR;
157  }
158 #else
159  struct stat buf;
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 }
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:145
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:35
#define S_ISLNK(m)
Definition: disk.c:60
static char buf[2048]
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition: disk.c:813
Closure for the recursion to determine the file size of a directory.
Definition: disk.c:82
uint64_t total
Set to the total file size.
Definition: disk.c:86
int include_sym_links
GNUNET_YES if symbolic links should be included.
Definition: disk.c:91
int single_file_mode
GNUNET_YES if mode is file-only (return total == -1 for directories).
Definition: disk.c:96

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

293 {
294  const char *tmpdir;
295  char *tmpl;
296  char *fn;
298  if ((t[0] != '/') && (t[0] != '\\'))
299  {
300  /* FIXME: This uses system codepage on W32, not UTF-8 */
301  tmpdir = getenv ("TMPDIR");
302  if (NULL == tmpdir)
303  tmpdir = getenv ("TMP");
304  if (NULL == tmpdir)
305  tmpdir = getenv ("TEMP");
306  if (NULL == tmpdir)
307  tmpdir = "/tmp";
308  GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX");
309  }
310  else
311  {
312  GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX");
313  }
314  fn = tmpl;
315  return fn;
316 }
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 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 380 of file disk.c.

443 {
444  struct stat filestat;
445  int ret;
446  char *rdir;
448  rdir = GNUNET_STRINGS_filename_expand (fil);
449  if (rdir == NULL)
450  return GNUNET_SYSERR;
452  ret = stat (rdir, &filestat);
453  if (0 != ret)
454  {
455  if (errno != ENOENT)
456  {
458  GNUNET_free (rdir);
459  return GNUNET_SYSERR;
460  }
461  GNUNET_free (rdir);
462  return GNUNET_NO;
463  }
464  if (! S_ISREG (filestat.st_mode))
465  {
466  GNUNET_free (rdir);
467  return GNUNET_NO;
468  }
469  if (access (rdir, amode) < 0)
470  {
472  GNUNET_free (rdir);
473  return GNUNET_SYSERR;
474  }
475  GNUNET_free (rdir);
476  return GNUNET_YES;
477 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#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:494

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

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

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

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

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

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

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

1635 {
1636  if (NULL == fh)
1637  return GNUNET_SYSERR;
1638  if (dst_len < sizeof(int))
1639  return GNUNET_SYSERR;
1640  *((int *) dst) = fh->fd;
1641  return GNUNET_OK;
1642 }
static struct GNUNET_DISK_FileHandle * fh
File handle to STDIN, for reading restart/quit commands.
int fd
File handle on Unix-like systems.


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

1655 {
1656  const char *option = cls;
1657  char *tmpname;
1659  if (GNUNET_OK !=
1660  GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", option, &tmpname))
1661  {
1663  return GNUNET_NO;
1664  }
1666  {
1668  GNUNET_free (tmpname);
1669  return GNUNET_OK;
1670  }
1671  GNUNET_free (tmpname);
1672  return GNUNET_OK;
1673 }
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: