31#define LOG(kind, ...) GNUNET_log_from (kind, "util-disk", __VA_ARGS__)
33#define LOG_STRERROR(kind, syscall) \
34 GNUNET_log_from_strerror (kind, "util-disk", syscall)
36#define LOG_STRERROR_FILE(kind, syscall, filename) \
37 GNUNET_log_from_strerror_file (kind, "util-disk", syscall, filename)
42#define COPY_BLK_SIZE 65536
55#include <sys/statvfs.h>
61#define S_ISLNK(m) (((m)&_IFMT) == _IFLNK)
150#if defined(HAVE_STAT64) && \
151 ! (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64)
154 if (0 != lstat64 (fn, &buf))
162 if (0 != lstat (fn, &buf))
174 gfsd->
total += buf.st_size;
175 if ((S_ISDIR (buf.st_mode)) && (0 == access (fn, X_OK)) &&
198 if (0 != fstat (fh->
fd, &sbuf))
200 *
size = sbuf.st_size;
210 static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END };
217 return lseek (
h->fd, offset,
t[whence]);
224 int include_symbolic_links,
225 int single_file_mode)
253 *ino = (uint64_t) sbuf.st_ino;
266 *dev = (uint64_t) fbuf.f_fsid;
277 ((uint64_t) fbuf.f_fsid.val[0]) << 32 || ((uint64_t) fbuf.f_fsid.val[1]);
299 if ((
t[0] !=
'/') && (
t[0] !=
'\\'))
302 tmpdir =
getenv (
"TMPDIR");
322 int require_uid_match,
323 int require_gid_match)
328 mode = S_IRUSR | S_IWUSR | S_IXUSR;
330 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP;
332 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH
334 if (0 != chmod (fn,
mode))
345 omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
347 if (fn != mkdtemp (fn))
366 slen = strlen (fil) + 20;
385 if (0 != renameat2 (AT_FDCWD,
406 while (0 == access (target, F_OK));
407 if (0 != rename (fil, target))
424 omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
426 if (-1 == (fd = mkstemp (fn)))
443 struct stat filestat;
446 ret = stat (fil, &filestat);
453 if (! S_ISDIR (filestat.st_mode))
456 "A file already exits with the same name %s\n",
461 ret = access (fil, R_OK | X_OK);
463 ret = access (fil, X_OK);
483 struct stat filestat;
491 ret = stat (rdir, &filestat);
503 if (! S_ISREG (filestat.st_mode))
508 if (access (rdir, amode) < 0)
565 "Creating directory `%s' failed\n",
592 "Creating directory `%s' failed\n",
600 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH
603 if ((
ret != 0) && (errno != EEXIST))
633 if (0 == access (rdir, W_OK))
650 (0 != access (rdir, W_OK)) )
669 return read (
h->fd,
result, len);
726 flags = fcntl (
h->fd, F_GETFL);
727 if (0 != (flags & O_NONBLOCK))
728 (void) fcntl (
h->fd, F_SETFL, flags - O_NONBLOCK);
729 ret = write (
h->fd, buffer, n);
730 if (0 == (flags & O_NONBLOCK))
731 (void) fcntl (
h->fd, F_SETFL, flags);
779 if (0 != unlink (tmpl))
795 if (0 != unlink (tmpl))
807 if (0 != unlink (tmpl))
814 if (0 != unlink (tmpl))
831 struct dirent *finfo;
836 unsigned int name_len;
843 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] ==
DIR_SEPARATOR))
844 dname[strlen (dname) - 1] =
'\0';
845 dinfo = opendir (dname);
855 while (NULL != (finfo = readdir (dinfo)))
857 if ((0 == strcmp (finfo->d_name,
".")) ||
858 (0 == strcmp (finfo->d_name,
"..")))
860 if (NULL != callback)
862 if (name_len < strlen (finfo->d_name))
865 name_len = strlen (finfo->d_name);
880 ret = callback (callback_cls,
name);
913 const char *str_pos = str;
915 const char *pat_pos = pattern;
917 const char *str_bt = NULL;
919 const char *pat_bt = NULL;
928 else if (*pat_pos == *str_pos)
930 if (
'\0' == *pat_pos)
941 str_pos = str_bt + 1;
942 if (
'\0' == *str_pos)
979 fn = (NULL == fn) ?
filename : (fn + 1);
982 "checking glob '%s' against '%s'\n",
991 "found glob match '%s'\n",
1011 if ( (NULL != strrchr (glob_pattern,
'+')) ||
1012 (NULL != strrchr (glob_pattern,
'[')) ||
1013 (NULL != strrchr (glob_pattern,
'~')) )
1016 "unsupported glob pattern: '%s'\n",
1031 if (NULL != strchr (mypat,
'*'))
1036 "glob pattern may only contain '*' in the final path component\n");
1044 .cls = callback_cls,
1048 "scanning directory '%s' for glob matches on '%s'\n",
1092 S_IWUSR | S_IRUSR | S_IXUSR);
1095 if ( (errno != EISDIR) &&
1161 if (len >
size - pos)
1164 if ((sret < 0) || (len != (
size_t) sret))
1167 if ((sret < 0) || (len != (
size_t) sret))
1193 for (idx = fn; *idx; idx++)
1197 if ((c ==
'/') || (c ==
'\\') || (c ==
':') || (c ==
'*') || (c ==
'?') ||
1201 (c ==
'<') || (c ==
'>') || (c ==
'|') )
1215 pws = getpwnam (user);
1219 _ (
"Cannot obtain information about user `%s': %s\n"),
1224 if (0 != chown (
filename, pws->pw_uid, pws->pw_gid))
1263 oflags |= (O_CREAT | O_EXCL);
1319 if (0 != close (
h->fd))
1334 if ((((off_t) -1) == lseek (fno, 0, SEEK_CUR)) && (EBADF == errno))
1375#define MAP_FAILED ((void *) -1)
1398 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED,
h->fd, 0);
1435#if ! defined(__linux__) || ! defined(GNU)
1448#if HAVE_PIPE2 && O_CLOEXEC
1449 if (-1 == pipe2 (
fd, O_CLOEXEC))
1451 if (-1 == pipe (
fd))
1477 p->fd[0]->fd =
fd[0];
1480 flags = fcntl (
fd[0], F_GETFL);
1481 flags |= O_NONBLOCK;
1482 if (0 > fcntl (
fd[0], F_SETFL, flags))
1488 flags = fcntl (
fd[0], F_GETFD);
1489 flags |= FD_CLOEXEC;
1490 if (0 > fcntl (
fd[0], F_SETFD, flags))
1500 p->fd[1]->fd =
fd[1];
1503 flags = fcntl (
fd[1], F_GETFL);
1504 flags |= O_NONBLOCK;
1505 if (0 > fcntl (
fd[1], F_SETFL, flags))
1511 flags = fcntl (
fd[1], F_GETFD);
1512 flags |= FD_CLOEXEC;
1513 if (0 > fcntl (
fd[1], F_SETFD, flags))
1523 if (
p->fd[0]->fd >= 0)
1525 if (
p->fd[1]->fd >= 0)
1596 int read_end_close_errno;
1597 int write_end_close_errno;
1600 read_end_close_errno = errno;
1602 write_end_close_errno = errno;
1607 errno = read_end_close_errno;
1608 ret = read_end_close;
1612 errno = write_end_close_errno;
1613 ret = write_end_close;
1659 const char *option = cls;
static enum GNUNET_GenericReturnValue get_size_rec(void *cls, const char *fn)
Iterate over all files in the given directory and accumulate their size.
#define COPY_BLK_SIZE
Block size for IO for copying files.
static enum GNUNET_GenericReturnValue glob_cb(void *cls, const char *filename)
Function called with a filename.
#define LOG_STRERROR_FILE(kind, syscall, filename)
static int translate_unix_perms(enum GNUNET_DISK_AccessPermissions perm)
Translate GNUnet-internal permission bitmap to UNIX file access permission bitmap.
static enum GNUNET_GenericReturnValue file_test_internal(const char *fil, int amode)
Check if fil can be accessed using amode.
static bool glob_match(const char *pattern, const char *str)
Check for a simple wildcard match.
static enum GNUNET_GenericReturnValue purge_cfg_dir(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
Helper function for GNUNET_DISK_purge_cfg_dir.
static char * mktemp_name(const char *t)
Create the name for a temporary file or directory from a template.
#define LOG_STRERROR(kind, syscall)
static enum GNUNET_GenericReturnValue remove_helper(void *unused, const char *fn)
Function that removes the given directory by calling GNUNET_DISK_directory_remove().
enum GNUNET_GenericReturnValue GNUNET_DISK_internal_file_handle_(const struct GNUNET_DISK_FileHandle *fh, int *dst)
Retrieve OS file handle.
Internal DISK related helper functions.
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
static int ret
Final status code.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static char * dir
Set to the directory where runtime files are stored.
static int end
Set if we are to shutdown all services (including ARM).
static const char * dir_name
Top-level directory we monitor to auto-publish.
static char * cfg_filename
Name of the configuration file.
static char * name
Name (label) of the records to list.
static char * res
Currently read line or NULL on EOF.
static int result
Global testing status.
static struct GNUNET_OS_Process * p
Helper process we started.
static struct GNUNET_SCHEDULER_Task * t
Main task.
static enum @44 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
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.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_parse_and_run(const struct GNUNET_OS_ProjectData *pd, const char *filename, GNUNET_CONFIGURATION_Callback cb, void *cb_cls)
Parse a configuration file filename and run the function cb with the resulting configuration object.
const struct GNUNET_DISK_FileHandle * GNUNET_DISK_pipe_handle(const struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd n)
Get the handle to a particular pipe end.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
void GNUNET_DISK_filename_canonicalize(char *fn)
Removes special characters as ':' from a filename.
int GNUNET_DISK_glob(const char *glob_pattern, GNUNET_FileNameCallback callback, void *callback_cls)
Find all files matching a glob pattern.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_change_owner(const char *filename, const char *user)
Change owner of a file.
char * GNUNET_DISK_mktemp(const char *t)
Create an (empty) temporary file on disk.
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).
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...
char * 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 ...
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.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_pipe_detach_end(struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd end)
Detaches one of the ends from the pipe.
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe_from_fd(enum GNUNET_DISK_PipeFlags pf, int fd[2])
Creates a pipe object from a couple of file descriptors.
GNUNET_DISK_OpenFlags
Specifies how a file should be opened.
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
GNUNET_DISK_PipeFlags
Flags for GNUNET_DISK_pipe().
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.
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(enum GNUNET_DISK_PipeFlags pf)
Creates an interprocess channel.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test_read(const char *fil)
Check that fil corresponds to a filename and the file has read permissions.
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).
GNUNET_DISK_AccessPermissions
File access permissions, UNIX-style.
enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
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.
char * GNUNET_DISK_mkdtemp(const char *t)
Create an (empty) temporary directory on disk.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_copy(const char *src, const char *dst)
Copy a file.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove(const char *filename)
Remove all files in a directory (rm -rf).
enum GNUNET_GenericReturnValue GNUNET_DISK_file_sync(const struct GNUNET_DISK_FileHandle *h)
Write file changes to disk.
GNUNET_DISK_Seek
Constants for specifying how to seek.
enum GNUNET_GenericReturnValue GNUNET_DISK_handle_invalid(const struct GNUNET_DISK_FileHandle *h)
Checks whether a handle is invalid.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
void GNUNET_DISK_purge_cfg_dir(const struct GNUNET_OS_ProjectData *pd, const char *cfg_filename, const char *option)
Remove the directory given under option in section [PATHS] in configuration under cfg_filename.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_get_handle_from_native(FILE *fd)
Get a handle from a native FD.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
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.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_get_handle_from_int_fd(int fno)
Get a handle from a native integer FD.
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.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
Get the size of an open file.
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.
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.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h)
Unmap a file.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create(const char *dir)
Implementation of "mkdir -p".
GNUNET_DISK_PipeEnd
Enumeration identifying the two ends of a pipe.
ssize_t GNUNET_DISK_fn_read(const char *fn, void *result, size_t len)
Read the contents of a binary file into a buffer.
GNUNET_DISK_MapType
Specifies what type of memory map is desired.
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_OPEN_FAILIFEXISTS
Fail if file already exists.
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_TRUNCATE
Truncate file if it exists.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_OPEN_APPEND
Append to the file.
@ GNUNET_DISK_OPEN_READWRITE
Open the file for both reading and writing.
@ GNUNET_DISK_PF_BLOCKING_WRITE
Configure write end to block when writing if set.
@ GNUNET_DISK_PF_BLOCKING_READ
Configure read end to block when reading if set.
@ GNUNET_DISK_PERM_OTHER_EXEC
Everybody can execute.
@ GNUNET_DISK_PERM_USER_EXEC
Owner can execute.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_NONE
Nobody is allowed to do anything to the file.
@ GNUNET_DISK_PERM_GROUP_READ
Group can read.
@ GNUNET_DISK_PERM_GROUP_EXEC
Group can execute.
@ GNUNET_DISK_PERM_GROUP_WRITE
Group can write.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
@ GNUNET_DISK_PERM_OTHER_READ
Everybody can read.
@ GNUNET_DISK_PERM_OTHER_WRITE
Everybody can write.
@ GNUNET_DISK_PIPE_END_WRITE
The writing-end of a pipe.
@ GNUNET_DISK_PIPE_END_READ
The reading-end of a pipe.
@ GNUNET_DISK_MAP_TYPE_WRITE
Write-able memory map.
@ GNUNET_DISK_MAP_TYPE_READ
Read-only memory map.
enum GNUNET_GenericReturnValue(* GNUNET_FileNameCallback)(void *cls, const char *filename)
Function called with a filename.
#define GNUNET_log(kind,...)
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
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...
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
static unsigned int size
Size of the "table".
Handle used to access files (and pipes).
int fd
File handle on Unix-like systems.
Handle for a memory-mapping operation.
void * addr
Address where the map is in memory.
size_t len
Number of bytes mapped.
Handle used to manage a pipe.
struct GNUNET_DISK_FileHandle * fd[2]
File descriptors for the pipe.
Project-specific data used to help the OS subsystem find installation paths.
Closure for the recursion to determine the file size of a directory.
uint64_t total
Set to the total file size.
int include_sym_links
GNUNET_YES if symbolic links should be included.
int single_file_mode
GNUNET_YES if mode is file-only (return total == -1 for directories).
GNUNET_FileNameCallback cb
int nres
Number of files that actually matched the glob pattern.