GNUnet 0.21.1
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 1375 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.

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;
static enum @44 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_open(), and GNUNET_DISK_fn_write().

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

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 }
159 struct stat buf;
161 if (0 != stat (fn, &buf))
162 {
164 return GNUNET_SYSERR;
165 }
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;
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
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: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

References get_size_rec(), 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 get_size_rec(), and GNUNET_DISK_file_size().

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)

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.

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;
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_mkdtemp(), and GNUNET_DISK_mktemp().

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

445 struct stat filestat;
446 int ret;
447 char *rdir;
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;
static int ret
Final status code.
Definition: gnunet-arm.c: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:495


Referenced by GNUNET_DISK_file_test(), and GNUNET_DISK_file_test_read().

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

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

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

Referenced by glob_cb().

Here is the caller graph for this function:

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

981 struct GlobClosure *gc = cls;
982 const char *fn;
984 fn = strrchr (filename, DIR_SEPARATOR);
985 fn = (NULL == fn) ? filename : (fn + 1);
988 "checking glob '%s' against '%s'\n",
989 gc->glob,
990 fn);
992 if (glob_match (gc->glob, fn))
993 {
994 enum GNUNET_GenericReturnValue cbret;
997 "found glob match '%s'\n",
998 filename);
999 gc->nres++;
1000 cbret = gc->cb (gc->cls, filename);
1001 if (GNUNET_OK != cbret)
1002 return cbret;
1003 }
1004 return GNUNET_OK;
static bool glob_match(const char *pattern, const char *str)
Check for a simple wildcard match.
Definition: disk.c:916
#define LOG(kind,...)
Definition: disk.c:30
static char * filename
Named constants for return values.
Definition: platform.h:165
GNUNET_FileNameCallback cb
Definition: disk.c:959
void * cls
Definition: disk.c:960
int nres
Number of files that actually matched the glob pattern.
Definition: disk.c:965
const char * glob
Definition: disk.c:958

References GlobClosure::cb, GlobClosure::cls, DIR_SEPARATOR, filename, GlobClosure::glob, glob_match(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_OK, LOG, and GlobClosure::nres.

Referenced by GNUNET_DISK_glob().

Here is the call graph for this function:
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 1077 of file disk.c.

1080 (void) unused;
1081 (void) GNUNET_DISK_directory_remove (fn);
1082 return GNUNET_OK;
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove(const char *filename)
Remove all files in a directory (rm -rf).
Definition: disk.c:1087

References GNUNET_DISK_directory_remove(), and GNUNET_OK.

Referenced by GNUNET_DISK_directory_remove().

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

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

1638 if (NULL == fh)
1639 return GNUNET_SYSERR;
1640 if (dst_len < sizeof(int))
1641 return GNUNET_SYSERR;
1642 *((int *) dst) = fh->fd;
1643 return GNUNET_OK;
int fd
File handle on Unix-like systems.

References GNUNET_DISK_FileHandle::fd, GNUNET_OK, and GNUNET_SYSERR.

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

1658 const char *option = cls;
1659 char *tmpname;
1661 if (GNUNET_OK !=
1662 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", option, &tmpname))
1663 {
1665 return GNUNET_NO;
1666 }
1668 {
1670 GNUNET_free (tmpname);
1671 return GNUNET_OK;
1672 }
1673 GNUNET_free (tmpname);
1674 return GNUNET_OK;
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:109
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...

References cfg, 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, and GNUNET_SYSERR.

Referenced by GNUNET_DISK_purge_cfg_dir().

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