GNUnet  0.19.3
Disk library

Disk IO APIs. More...

Collaboration diagram for Disk library:

Data Structures

struct  GNUNET_DISK_FileHandle
 Handle used to access files (and pipes). More...
 

Enumerations

enum  GNUNET_FILE_Type { GNUNET_DISK_HANLDE_TYPE_EVENT , GNUNET_DISK_HANLDE_TYPE_FILE , GNUNET_DISK_HANLDE_TYPE_PIPE }
 Type of a handle. More...
 
enum  GNUNET_DISK_OpenFlags {
  GNUNET_DISK_OPEN_READ = 1 , GNUNET_DISK_OPEN_WRITE = 2 , GNUNET_DISK_OPEN_READWRITE = 3 , GNUNET_DISK_OPEN_FAILIFEXISTS = 4 ,
  GNUNET_DISK_OPEN_TRUNCATE = 8 , GNUNET_DISK_OPEN_CREATE = 16 , GNUNET_DISK_OPEN_APPEND = 32
}
 Specifies how a file should be opened. More...
 
enum  GNUNET_DISK_MapType { GNUNET_DISK_MAP_TYPE_READ = 1 , GNUNET_DISK_MAP_TYPE_WRITE = 2 , GNUNET_DISK_MAP_TYPE_READWRITE = 3 }
 Specifies what type of memory map is desired. More...
 
enum  GNUNET_DISK_AccessPermissions {
  GNUNET_DISK_PERM_NONE = 0 , GNUNET_DISK_PERM_USER_READ = 1 , GNUNET_DISK_PERM_USER_WRITE = 2 , GNUNET_DISK_PERM_USER_EXEC = 4 ,
  GNUNET_DISK_PERM_GROUP_READ = 8 , GNUNET_DISK_PERM_GROUP_WRITE = 16 , GNUNET_DISK_PERM_GROUP_EXEC = 32 , GNUNET_DISK_PERM_OTHER_READ = 64 ,
  GNUNET_DISK_PERM_OTHER_WRITE = 128 , GNUNET_DISK_PERM_OTHER_EXEC = 256
}
 File access permissions, UNIX-style. More...
 
enum  GNUNET_DISK_Seek { GNUNET_DISK_SEEK_SET = 0 , GNUNET_DISK_SEEK_CUR = 1 , GNUNET_DISK_SEEK_END = 2 }
 Constants for specifying how to seek. More...
 
enum  GNUNET_DISK_PipeEnd { GNUNET_DISK_PIPE_END_READ = 0 , GNUNET_DISK_PIPE_END_WRITE = 1 }
 Enumeration identifying the two ends of a pipe. More...
 
enum  GNUNET_DISK_PipeFlags { GNUNET_DISK_PF_NONE , GNUNET_DISK_PF_BLOCKING_READ = 1 , GNUNET_DISK_PF_BLOCKING_WRITE = 2 , GNUNET_DISK_PF_BLOCKING_RW }
 Flags for GNUNET_DISK_pipe(). More...
 

Functions

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_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...
 
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...
 
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...
 
char * GNUNET_DISK_mktemp (const char *t)
 Create an (empty) temporary file on disk. More...
 
char * GNUNET_DISK_mkdtemp (const char *t)
 Create an (empty) temporary directory on disk. 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_handle_size (struct GNUNET_DISK_FileHandle *fh, off_t *size)
 Get the size of an open file. 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 (struct GNUNET_DISK_PipeHandle *p)
 Closes an interprocess channel. 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_file_close (struct GNUNET_DISK_FileHandle *h)
 Close an open file. 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...
 
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...
 
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...
 
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...
 
enum GNUNET_GenericReturnValue GNUNET_DISK_file_copy (const char *src, const char *dst)
 Copy a file. More...
 
int GNUNET_DISK_directory_scan (const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
 Scan a directory for files. More...
 
int GNUNET_DISK_glob (const char *glob_pattern, GNUNET_FileNameCallback callback, void *callback_cls)
 Find all files matching a glob pattern. More...
 
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create_for_file (const char *filename)
 Create the directory structure for storing a file. More...
 
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test (const char *fil, int is_readable)
 Test if fil is a directory and listable. More...
 
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove (const char *filename)
 Remove all files in a directory (rm -rf). 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...
 
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create (const char *dir)
 Implementation of "mkdir -p". 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...
 
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...
 

Detailed Description

Disk IO APIs.

Enumeration Type Documentation

◆ GNUNET_FILE_Type

Type of a handle.

Enumerator
GNUNET_DISK_HANLDE_TYPE_EVENT 

Handle represents an event.

GNUNET_DISK_HANLDE_TYPE_FILE 

Handle represents a file.

GNUNET_DISK_HANLDE_TYPE_PIPE 

Handle represents a pipe.

Definition at line 50 of file gnunet_disk_lib.h.

51 {
56 
61 
66 };
@ GNUNET_DISK_HANLDE_TYPE_FILE
Handle represents a file.
@ GNUNET_DISK_HANLDE_TYPE_PIPE
Handle represents a pipe.
@ GNUNET_DISK_HANLDE_TYPE_EVENT
Handle represents an event.

◆ GNUNET_DISK_OpenFlags

Specifies how a file should be opened.

Enumerator
GNUNET_DISK_OPEN_READ 

Open the file for reading.

GNUNET_DISK_OPEN_WRITE 

Open the file for writing.

GNUNET_DISK_OPEN_READWRITE 

Open the file for both reading and writing.

GNUNET_DISK_OPEN_FAILIFEXISTS 

Fail if file already exists.

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.

Definition at line 101 of file gnunet_disk_lib.h.

102 {
107 
112 
117 
122 
127 
132 
137 };
@ 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_MapType

Specifies what type of memory map is desired.

Enumerator
GNUNET_DISK_MAP_TYPE_READ 

Read-only memory map.

GNUNET_DISK_MAP_TYPE_WRITE 

Write-able memory map.

GNUNET_DISK_MAP_TYPE_READWRITE 

Read-write memory map.

Definition at line 142 of file gnunet_disk_lib.h.

143 {
148 
153 
158 };
@ GNUNET_DISK_MAP_TYPE_WRITE
Write-able memory map.
@ GNUNET_DISK_MAP_TYPE_READ
Read-only memory map.
@ GNUNET_DISK_MAP_TYPE_READWRITE
Read-write memory map.

◆ GNUNET_DISK_AccessPermissions

File access permissions, UNIX-style.

Enumerator
GNUNET_DISK_PERM_NONE 

Nobody is allowed to do anything to the file.

GNUNET_DISK_PERM_USER_READ 

Owner can read.

GNUNET_DISK_PERM_USER_WRITE 

Owner can write.

GNUNET_DISK_PERM_USER_EXEC 

Owner can execute.

GNUNET_DISK_PERM_GROUP_READ 

Group can read.

GNUNET_DISK_PERM_GROUP_WRITE 

Group can write.

GNUNET_DISK_PERM_GROUP_EXEC 

Group can execute.

GNUNET_DISK_PERM_OTHER_READ 

Everybody can read.

GNUNET_DISK_PERM_OTHER_WRITE 

Everybody can write.

GNUNET_DISK_PERM_OTHER_EXEC 

Everybody can execute.

Definition at line 164 of file gnunet_disk_lib.h.

165 {
170 
175 
180 
185 
190 
195 
200 
205 
210 
215 };
@ 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_Seek

Constants for specifying how to seek.

Do not change values or order, some of the code depends on the specific numeric values!

Enumerator
GNUNET_DISK_SEEK_SET 

Seek an absolute position (from the start of the file).

GNUNET_DISK_SEEK_CUR 

Seek a relative position (from the current offset).

GNUNET_DISK_SEEK_END 

Seek an absolute position from the end of the file.

Definition at line 222 of file gnunet_disk_lib.h.

223 {
228 
233 
238 };
@ GNUNET_DISK_SEEK_SET
Seek an absolute position (from the start of the file).
@ GNUNET_DISK_SEEK_CUR
Seek a relative position (from the current offset).
@ GNUNET_DISK_SEEK_END
Seek an absolute position from the end of the file.

◆ GNUNET_DISK_PipeEnd

Enumeration identifying the two ends of a pipe.

Enumerator
GNUNET_DISK_PIPE_END_READ 

The reading-end of a pipe.

GNUNET_DISK_PIPE_END_WRITE 

The writing-end of a pipe.

Definition at line 244 of file gnunet_disk_lib.h.

245 {
250 
255 };
@ GNUNET_DISK_PIPE_END_WRITE
The writing-end of a pipe.
@ GNUNET_DISK_PIPE_END_READ
The reading-end of a pipe.

◆ GNUNET_DISK_PipeFlags

Flags for GNUNET_DISK_pipe().

Enumerator
GNUNET_DISK_PF_NONE 

No special options, use non-blocking read/write operations.

GNUNET_DISK_PF_BLOCKING_READ 

Configure read end to block when reading if set.

GNUNET_DISK_PF_BLOCKING_WRITE 

Configure write end to block when writing if set.

GNUNET_DISK_PF_BLOCKING_RW 

Configure both pipe ends for blocking operations if set.

Definition at line 416 of file gnunet_disk_lib.h.

417 {
418 
423 
428 
433 
439 
440 };
@ GNUNET_DISK_PF_NONE
No special options, use non-blocking read/write operations.
@ GNUNET_DISK_PF_BLOCKING_WRITE
Configure write end to block when writing if set.
@ GNUNET_DISK_PF_BLOCKING_RW
Configure both pipe ends for blocking operations if set.
@ GNUNET_DISK_PF_BLOCKING_READ
Configure read end to block when reading if set.

Function Documentation

◆ GNUNET_DISK_handle_invalid()

enum GNUNET_GenericReturnValue GNUNET_DISK_handle_invalid ( const struct GNUNET_DISK_FileHandle h)

Checks whether a handle is invalid.

Parameters
hhandle to check
Returns
GNUNET_YES if invalid, GNUNET_NO if valid

Definition at line 108 of file disk.c.

186 {
187  return ((! h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO;
188 }
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
@ GNUNET_YES
@ GNUNET_NO

Referenced by file_hash_finish(), incrementBit(), maint_child_death(), and shutdown_pipe_cb().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_test()

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

Parameters
filfilename to check
Returns
GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something else (will print an error message in that case, too).

Definition at line 380 of file disk.c.

482 {
483  return file_test_internal (fil, F_OK);
484 }
static enum GNUNET_GenericReturnValue file_test_internal(const char *fil, int amode)
Check if fil can be accessed using amode.
Definition: disk.c:442

Referenced by callback_scan_for_operations(), callback_scan_for_rooms(), create_download_context(), create_unique_cfgs(), database_setup(), ego_callback(), gen_topo_from_file(), get_path_from_PATH(), GNUNET_CONFIGURATION_default(), GNUNET_CONTAINER_bloomfilter_load(), GNUNET_DISK_file_close(), GNUNET_FRIENDS_parse(), GNUNET_FRIENDS_write_start(), GNUNET_FS_download_start_task_(), GNUNET_HOSTLIST_client_start(), GNUNET_SERVICE_run_(), GNUNET_TESTBED_hosts_load_from_file(), GNUNET_TESTING_get_topo_from_file(), GNUNET_TRANSPORT_TESTING_start_peer(), hostkeys_load(), hosts_directory_scan_callback(), LEGACY_SERVICE_run(), load_hostlist_file(), load_list_messages(), load_list_tunnels(), load_member(), load_member_session(), load_member_session_history(), load_member_session_next(), load_message_store(), load_search_strings(), policy_filename_cb(), print_key(), read_host_file(), read_index_list(), read_update_information_graph(), restore_valid_peers(), run(), and start_peer_run().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_test_read()

enum GNUNET_GenericReturnValue GNUNET_DISK_file_test_read ( const char *  fil)

Check that fil corresponds to a filename and the file has read permissions.

Parameters
filfilename to check
Returns
GNUNET_YES if yes, GNUNET_NO if file doesn't exist or has no read permissions, GNUNET_SYSERR if something else (will print an error message in that case, too).

Definition at line 380 of file disk.c.

489 {
490  return file_test_internal (fil, R_OK);
491 }

Referenced by GNUNET_CONFIGURATION_default_filename().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_backup()

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.

Parameters
filname of the file to back up

Definition at line 359 of file disk.c.

360 {
361  size_t slen;
362  char *target;
363  unsigned int num;
364 
365  slen = strlen (fil) + 20;
366  target = GNUNET_malloc (slen);
367  num = 0;
368  do
369  {
370  GNUNET_snprintf (target, slen, "%s.%u~", fil, num++);
371  }
372  while (0 == access (target, F_OK));
373  if (0 != rename (fil, target))
375  GNUNET_free (target);
376 }
#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_ERROR
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_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.

References GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log_strerror_file, GNUNET_malloc, and GNUNET_snprintf().

Referenced by GNUNET_FRIENDS_write_start().

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

◆ GNUNET_DISK_file_seek()

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.

Parameters
hhandle of an open file
offsetposition to move to
whencespecification to which position the offset parameter relates to
Returns
the new position on success, GNUNET_SYSERR otherwise

Definition at line 205 of file disk.c.

208 {
209  static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END };
210 
211  if (h == NULL)
212  {
213  errno = EINVAL;
214  return GNUNET_SYSERR;
215  }
216  return lseek (h->fd, offset, t[whence]);
217 }
static struct GNUNET_SCHEDULER_Task * t
Main task.
@ GNUNET_SYSERR

Referenced by fh_reader(), get_store_message(), GNUNET_FS_data_reader_file_(), GNUNET_FS_handle_on_demand_block(), incrementBit(), iterate_save_messages(), load_list_messages(), load_list_tunnels(), load_member_session_history(), process_result_with_request(), save_list_messages(), save_list_tunnels(), save_member_session_history(), save_message_store(), try_top_down_reconstruction(), and unindex_reader().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_size()

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

Parameters
filenamename of the file or directory
sizeset to the size of the file (or, in the case of directories, the sum of all sizes of files in the directory)
include_symbolic_linksshould symbolic links be included?
single_file_modeGNUNET_YES to only get size of one file and return GNUNET_SYSERR for directories.
Returns
GNUNET_SYSERR on error, GNUNET_OK on success

Definition at line 205 of file disk.c.

225 {
226  struct GetFileSizeData gfsd;
228 
229  GNUNET_assert (size != NULL);
230  gfsd.total = 0;
231  gfsd.include_sym_links = include_symbolic_links;
232  gfsd.single_file_mode = single_file_mode;
233  ret = get_size_rec (&gfsd, filename);
234  *size = gfsd.total;
235  return ret;
236 }
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
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static char * filename
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static unsigned int size
Size of the "table".
Definition: peer.c:68
Closure for the recursion to determine the file size of a directory.
Definition: disk.c:82
int single_file_mode
GNUNET_YES if mode is file-only (return total == -1 for directories).
Definition: disk.c:96

Referenced by create_download_context(), database_setup(), discard_hosts_helper(), gen_topo_from_file(), GNUNET_CRYPTO_hash_file(), GNUNET_FRIENDS_parse(), GNUNET_FS_file_information_create_from_file(), GNUNET_FS_unindex_start(), GNUNET_TESTBED_hosts_load_from_file(), GNUNET_TESTING_get_topo_from_file(), hostkeys_load(), load(), load_file(), load_keys(), load_search_strings(), main(), policy_filename_cb(), preprocess_file(), print_key(), run(), and setup_ac().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_get_identifiers()

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.

This function is used between GNUnet processes to quickly check if two files with the same absolute path are actually identical. The two processes represent the same peer but may communicate over the network (and the file may be on an NFS volume). This function may not be supported on all operating systems.

Parameters
filenamename of the file
devset to the device ID
inoset to the inode ID
Returns
GNUNET_OK on success

Definition at line 205 of file disk.c.

243 {
244 #if HAVE_STAT
245  {
246  struct stat sbuf;
247 
248  if (0 != stat (filename, &sbuf))
249  {
250  return GNUNET_SYSERR;
251  }
252  *ino = (uint64_t) sbuf.st_ino;
253  }
254 #else
255  *ino = 0;
256 #endif
257 #if HAVE_STATVFS
258  {
259  struct statvfs fbuf;
260 
261  if (0 != statvfs (filename, &fbuf))
262  {
263  return GNUNET_SYSERR;
264  }
265  *dev = (uint64_t) fbuf.f_fsid;
266  }
267 #elif HAVE_STATFS
268  {
269  struct statfs fbuf;
270 
271  if (0 != statfs (filename, &fbuf))
272  {
273  return GNUNET_SYSERR;
274  }
275  *dev =
276  ((uint64_t) fbuf.f_fsid.val[0]) << 32 || ((uint64_t) fbuf.f_fsid.val[1]);
277  }
278 #else
279  *dev = 0;
280 #endif
281  return GNUNET_OK;
282 }
@ GNUNET_OK

References GNUNET_SYSERR, h, and t.

Referenced by handle_client_index_start(), and hash_for_index_cb().

Here is the caller graph for this function:

◆ GNUNET_DISK_mktemp()

char* GNUNET_DISK_mktemp ( const char *  t)

Create an (empty) temporary file on disk.

If the given name is not an absolute path, the current 'TMPDIR' will be prepended. In any case, 6 random characters will be appended to the name to create a unique filename.

Parameters
tcomponent to use for the name; does NOT contain "XXXXXX" or "/tmp/".
Returns
NULL on error, otherwise name of fresh file on disk in directory for temporary files

Definition at line 380 of file disk.c.

381 {
382  int fd;
383  char *fn;
384  mode_t omask;
385 
386  omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
387  fn = mktemp_name (t);
388  if (-1 == (fd = mkstemp (fn)))
389  {
391  GNUNET_free (fn);
392  umask (omask);
393  return NULL;
394  }
395  umask (omask);
396  if (0 != close (fd))
398  return fn;
399 }
static char * mktemp_name(const char *t)
Create the name for a temporary file or directory from a template.
Definition: disk.c:292
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:35
@ GNUNET_ERROR_TYPE_WARNING

Referenced by create_download_context(), GNUNET_DATACACHE_create(), GNUNET_FS_download_sync_(), libgnunet_plugin_datacache_sqlite_init(), make_serialization_file_name(), make_serialization_file_name_in_dir(), publish_fs_connect_complete_cb(), and testing_main().

Here is the caller graph for this function:

◆ GNUNET_DISK_mkdtemp()

char* GNUNET_DISK_mkdtemp ( const char *  t)

Create an (empty) temporary directory on disk.

If the given name is not an absolute path, the current 'TMPDIR' will be prepended. In any case, 6 random characters will be appended to the name to create a unique name.

Parameters
tcomponent to use for the name; does NOT contain "XXXXXX" or "/tmp/".
Returns
NULL on error, otherwise name of freshly created directory

Definition at line 339 of file disk.c.

340 {
341  char *fn;
342  mode_t omask;
343 
344  omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
345  fn = mktemp_name (t);
346  if (fn != mkdtemp (fn))
347  {
349  GNUNET_free (fn);
350  umask (omask);
351  return NULL;
352  }
353  umask (omask);
354  return fn;
355 }

References GNUNET_ERROR_TYPE_ERROR, GNUNET_free, LOG_STRERROR_FILE, mktemp_name(), and t.

Referenced by create_response(), and GNUNET_TESTING_system_create_with_portrange().

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

◆ GNUNET_DISK_file_open()

struct GNUNET_DISK_FileHandle* GNUNET_DISK_file_open ( const char *  fn,
enum GNUNET_DISK_OpenFlags  flags,
enum GNUNET_DISK_AccessPermissions  perm 
)

Open a file.

Note that the access permissions will only be used if a new file is created and if the underlying operating system supports the given permissions.

Parameters
fnfile name to be opened
flagsopening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
permpermissions for the newly created file, use GNUNET_DISK_PERM_NONE if a file could not be created by this call (because of flags)
Returns
IO handle on success, NULL on error

Definition at line 1234 of file disk.c.

1237 {
1238  char *expfn;
1239  struct GNUNET_DISK_FileHandle *ret;
1240 
1241  int oflags;
1242  int mode;
1243  int fd;
1244 
1245  expfn = GNUNET_STRINGS_filename_expand (fn);
1246  if (NULL == expfn)
1247  return NULL;
1248 
1249  mode = 0;
1251  oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */
1252  else if (flags & GNUNET_DISK_OPEN_READ)
1253  oflags = O_RDONLY;
1254  else if (flags & GNUNET_DISK_OPEN_WRITE)
1255  oflags = O_WRONLY;
1256  else
1257  {
1258  GNUNET_break (0);
1259  GNUNET_free (expfn);
1260  return NULL;
1261  }
1262  if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1263  oflags |= (O_CREAT | O_EXCL);
1264  if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1265  oflags |= O_TRUNC;
1266  if (flags & GNUNET_DISK_OPEN_APPEND)
1267  oflags |= O_APPEND;
1268  if (GNUNET_NO == GNUNET_DISK_file_test (fn))
1269  {
1270  if (flags & GNUNET_DISK_OPEN_CREATE)
1271  {
1273  oflags |= O_CREAT;
1274  mode = translate_unix_perms (perm);
1275  }
1276  }
1277 
1278  fd = open (expfn,
1279  oflags
1280 #if O_CLOEXEC
1281  | O_CLOEXEC
1282 #endif
1283  | O_LARGEFILE,
1284  mode);
1285  if (fd == -1)
1286  {
1287  if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))
1289  else
1290  LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "open", expfn);
1291  GNUNET_free (expfn);
1292  return NULL;
1293  }
1294 
1296 
1297  ret->fd = fd;
1298 
1299  GNUNET_free (expfn);
1300  return ret;
1301 }
static int translate_unix_perms(enum GNUNET_DISK_AccessPermissions perm)
Translate GNUnet-internal permission bitmap to UNIX file access permission bitmap.
Definition: disk.c:108
static enum @8 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
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).
Definition: disk.c:481
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:581
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition: strings.c:494
#define O_LARGEFILE
Definition: platform.h:211
Handle used to access files (and pipes).
int fd
File handle on Unix-like systems.

Referenced by create_hostkeys(), database_setup(), database_shutdown(), full_recursive_download(), get_file_handle(), GNUNET_ATS_solver_logging_write_to_disk(), GNUNET_BIO_read_open_file(), GNUNET_BIO_write_open_file(), GNUNET_CONTAINER_bloomfilter_load(), GNUNET_CRYPTO_hash_file(), GNUNET_DISK_fn_read(), GNUNET_FRIENDS_write_start(), GNUNET_FS_data_reader_file_(), GNUNET_FS_download_start_task_(), GNUNET_FS_handle_on_demand_block(), GNUNET_FS_unindex_do_remove_(), GNUNET_TESTING_peer_configure(), hostkeys_load(), iface_proc(), load_etc_hosts(), load_keys(), load_list_messages(), load_list_tunnels(), load_member_session_history(), load_message_store(), load_message_store_entries(), load_message_store_links(), lookup_dns_servers(), main(), open_static_page(), open_static_resource(), print_key(), process_result_with_request(), publish_fs_connect_complete_cb(), read_from_log(), REGEX_TEST_read_from_file(), restore_valid_peers(), run(), save_list_messages(), save_list_tunnels(), save_member_session_history(), save_message_store(), start_dump(), start_insert(), store_valid_peers(), to_file_raw(), tofile_(), try_match_block(), and write_benchmark_data().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_handle_size()

enum GNUNET_GenericReturnValue GNUNET_DISK_file_handle_size ( struct GNUNET_DISK_FileHandle fh,
off_t *  size 
)

Get the size of an open file.

Parameters
fhopen file handle
sizewhere to write size of the file
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 108 of file disk.c.

194 {
195  struct stat sbuf;
196 
197  if (0 != fstat (fh->fd, &sbuf))
198  return GNUNET_SYSERR;
199  *size = sbuf.st_size;
200  return GNUNET_OK;
201 }
static struct GNUNET_DISK_FileHandle * fh
File handle to STDIN, for reading restart/quit commands.

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_CONTAINER_bloomfilter_load(), load_etc_hosts(), lookup_dns_servers(), open_static_page(), open_static_resource(), REGEX_TEST_read_from_file(), and restore_valid_peers().

Here is the caller graph for this function:

◆ GNUNET_DISK_pipe()

struct GNUNET_DISK_PipeHandle* GNUNET_DISK_pipe ( enum GNUNET_DISK_PipeFlags  pf)

Creates an interprocess channel.

Parameters
pfhow to configure the pipe
Returns
handle to the new pipe, NULL on error

Definition at line 1441 of file disk.c.

1442 {
1443  int fd[2];
1444 
1445  if (-1 == pipe (fd))
1446  {
1447  int eno = errno;
1448 
1450  errno = eno;
1451  return NULL;
1452  }
1453  return GNUNET_DISK_pipe_from_fd (pf, fd);
1454 }
#define LOG_STRERROR(kind, syscall)
Definition: disk.c:32
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.
Definition: disk.c:1458

References GNUNET_DISK_PipeHandle::fd, GNUNET_DISK_pipe_from_fd(), GNUNET_ERROR_TYPE_ERROR, and LOG_STRERROR.

Referenced by child_management_start(), GNUNET_ARM_request_service_start(), GNUNET_NAT_mini_get_external_ipv4_(), GNUNET_OS_command_run(), GNUNET_SCHEDULER_driver_init(), main(), restart_nat_server(), run(), start_helper(), and start_process().

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

◆ GNUNET_DISK_pipe_from_fd()

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.

Useful for wrapping existing pipe FDs.

Parameters
pfhow to configure the pipe
fdan array of two fd values. One of them may be -1 for read-only or write-only pipes
Returns
handle to the new pipe, NULL on error

Definition at line 1458 of file disk.c.

1460 {
1461  struct GNUNET_DISK_PipeHandle *p;
1462  int ret = 0;
1463  int flags;
1464  int eno = 0; /* make gcc happy */
1465 
1466  p = GNUNET_new (struct GNUNET_DISK_PipeHandle);
1467  if (fd[0] >= 0)
1468  {
1469  p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle);
1470  p->fd[0]->fd = fd[0];
1471  if (0 == (GNUNET_DISK_PF_BLOCKING_READ & pf))
1472  {
1473  flags = fcntl (fd[0], F_GETFL);
1474  flags |= O_NONBLOCK;
1475  if (0 > fcntl (fd[0], F_SETFL, flags))
1476  {
1477  ret = -1;
1478  eno = errno;
1479  }
1480  }
1481  flags = fcntl (fd[0], F_GETFD);
1482  flags |= FD_CLOEXEC;
1483  if (0 > fcntl (fd[0], F_SETFD, flags))
1484  {
1485  ret = -1;
1486  eno = errno;
1487  }
1488  }
1489 
1490  if (fd[1] >= 0)
1491  {
1492  p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle);
1493  p->fd[1]->fd = fd[1];
1494  if (0 == (GNUNET_DISK_PF_BLOCKING_WRITE & pf))
1495  {
1496  flags = fcntl (fd[1], F_GETFL);
1497  flags |= O_NONBLOCK;
1498  if (0 > fcntl (fd[1], F_SETFL, flags))
1499  {
1500  ret = -1;
1501  eno = errno;
1502  }
1503  }
1504  flags = fcntl (fd[1], F_GETFD);
1505  flags |= FD_CLOEXEC;
1506  if (0 > fcntl (fd[1], F_SETFD, flags))
1507  {
1508  ret = -1;
1509  eno = errno;
1510  }
1511  }
1512  if (ret == -1)
1513  {
1514  errno = eno;
1516  if (p->fd[0]->fd >= 0)
1517  GNUNET_break (0 == close (p->fd[0]->fd));
1518  if (p->fd[1]->fd >= 0)
1519  GNUNET_break (0 == close (p->fd[1]->fd));
1520  GNUNET_free (p->fd[0]);
1521  GNUNET_free (p->fd[1]);
1522  GNUNET_free (p);
1523  errno = eno;
1524  return NULL;
1525  }
1526  return p;
1527 }
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-uri.c:38
Handle used to manage a pipe.
Definition: disk.c:68
struct GNUNET_DISK_FileHandle * fd[2]
File descriptors for the pipe.
Definition: disk.c:73

Referenced by GNUNET_DISK_pipe().

Here is the caller graph for this function:

◆ GNUNET_DISK_pipe_close()

enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close ( struct GNUNET_DISK_PipeHandle p)

Closes an interprocess channel.

Parameters
ppipe
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1557 of file disk.c.

1585 {
1586  int ret = GNUNET_OK;
1587 
1588  int read_end_close;
1589  int write_end_close;
1590  int read_end_close_errno;
1591  int write_end_close_errno;
1592 
1594  read_end_close_errno = errno;
1596  write_end_close_errno = errno;
1597  GNUNET_free (p);
1598 
1599  if (GNUNET_OK != read_end_close)
1600  {
1601  errno = read_end_close_errno;
1602  ret = read_end_close;
1603  }
1604  else if (GNUNET_OK != write_end_close)
1605  {
1606  errno = write_end_close_errno;
1607  ret = write_end_close;
1608  }
1609 
1610  return ret;
1611 }
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.
Definition: disk.c:1531

References end, GNUNET_DISK_PIPE_END_READ, GNUNET_DISK_PIPE_END_WRITE, p, and ret.

Referenced by child_management_done(), GN_stop_gnunet_nat_server_(), GNUNET_ARM_request_service_start(), GNUNET_HELPER_kill(), GNUNET_HELPER_wait(), GNUNET_NAT_mini_get_external_ipv4_(), GNUNET_NAT_mini_get_external_ipv4_cancel_(), GNUNET_OS_command_run(), GNUNET_OS_command_stop(), GNUNET_SCHEDULER_driver_done(), main(), nat_server_read(), restart_nat_server(), shutdown_task(), and start_process().

Here is the caller graph for this function:

◆ GNUNET_DISK_pipe_close_end()

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.

Parameters
ppipe to close end of
endwhich end of the pipe to close
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1458 of file disk.c.

1533 {
1535 
1537  {
1538  if (p->fd[0])
1539  {
1540  ret = GNUNET_DISK_file_close (p->fd[0]);
1541  p->fd[0] = NULL;
1542  }
1543  }
1544  else if (end == GNUNET_DISK_PIPE_END_WRITE)
1545  {
1546  if (p->fd[1])
1547  {
1548  ret = GNUNET_DISK_file_close (p->fd[1]);
1549  p->fd[1] = NULL;
1550  }
1551  }
1552  return ret;
1553 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1305

References GNUNET_DISK_PipeHandle::fd, GNUNET_break, GNUNET_DISK_PF_BLOCKING_READ, GNUNET_DISK_PF_BLOCKING_WRITE, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_new, LOG_STRERROR, p, and ret.

Referenced by GNUNET_NAT_mini_get_external_ipv4_(), GNUNET_OS_command_run(), restart_nat_server(), and start_helper().

Here is the caller graph for this function:

◆ GNUNET_DISK_pipe_detach_end()

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.

Detached end is a fully-functional FileHandle, it will not be affected by anything you do with the pipe afterwards. Each end of a pipe can only be detched from it once (i.e. it is not duplicated).

Parameters
ppipe to detach an end from
endwhich end of the pipe to detach
Returns
Detached end on success, NULL on failure (or if that end is not present or is closed).

Definition at line 1557 of file disk.c.

1559 {
1560  struct GNUNET_DISK_FileHandle *ret = NULL;
1561 
1563  {
1564  if (p->fd[0])
1565  {
1566  ret = p->fd[0];
1567  p->fd[0] = NULL;
1568  }
1569  }
1570  else if (end == GNUNET_DISK_PIPE_END_WRITE)
1571  {
1572  if (p->fd[1])
1573  {
1574  ret = p->fd[1];
1575  p->fd[1] = NULL;
1576  }
1577  }
1578 
1579  return ret;
1580 }

Referenced by GNUNET_ARM_request_service_start(), and start_process().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_close()

enum GNUNET_GenericReturnValue GNUNET_DISK_file_close ( struct GNUNET_DISK_FileHandle h)

Close an open file.

Parameters
hfile handle
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1234 of file disk.c.

1306 {
1308 
1309  if (NULL == h)
1310  {
1311  errno = EINVAL;
1312  return GNUNET_SYSERR;
1313  }
1314 
1315  ret = GNUNET_OK;
1316  if (0 != close (h->fd))
1317  {
1319  ret = GNUNET_SYSERR;
1320  }
1321  GNUNET_free (h);
1322  return ret;
1323 }

References GNUNET_DISK_FileHandle::fd, GNUNET_break, GNUNET_DISK_directory_create_for_file(), GNUNET_DISK_file_test(), GNUNET_DISK_OPEN_APPEND, GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_OPEN_FAILIFEXISTS, GNUNET_DISK_OPEN_READ, GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_OPEN_TRUNCATE, GNUNET_DISK_OPEN_WRITE, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_new, GNUNET_NO, GNUNET_STRINGS_filename_expand(), LOG_STRERROR_FILE, mode, O_LARGEFILE, ret, and translate_unix_perms().

Referenced by check_completed(), cleanup(), clear_message_store(), close_files_iter(), create_hostkeys(), database_setup(), database_shutdown(), do_shutdown(), file_hash_finish(), full_recursive_download(), get_cb(), GNUNET_ARM_operation_cancel(), GNUNET_ARM_request_service_start(), GNUNET_ATS_solver_logging_write_to_disk(), GNUNET_BIO_read_close(), GNUNET_CONTAINER_bloomfilter_free(), GNUNET_CONTAINER_bloomfilter_load(), GNUNET_CRYPTO_hash_file_cancel(), GNUNET_DISK_fn_read(), GNUNET_FRIENDS_write_stop(), GNUNET_FS_data_reader_file_(), GNUNET_FS_download_signal_suspend_(), GNUNET_FS_download_start_task_(), GNUNET_FS_handle_on_demand_block(), GNUNET_FS_unindex_signal_suspend_(), GNUNET_FS_unindex_stop(), GNUNET_OS_process_destroy(), GNUNET_TESTING_peer_configure(), hostkeys_load(), load_etc_hosts(), load_list_messages(), load_list_tunnels(), load_member_session_history(), load_message_store(), load_message_store_entries(), load_message_store_links(), lookup_dns_servers(), main(), notify_starting(), open_static_page(), open_static_resource(), parent_control_handler(), print_key(), process_result_with_request(), publish_fs_connect_complete_cb(), reconstruct_cont(), REGEX_TEST_read_from_file(), restore_valid_peers(), run(), save_list_messages(), save_list_tunnels(), save_member_session_history(), save_message_store(), shutdown_pch(), shutdown_task(), start_process(), store_valid_peers(), to_file_raw(), tofile_(), try_match_block(), unindex_finish(), unload_keys(), and write_benchmark_data().

Here is the call graph for this function:

◆ GNUNET_DISK_pipe_handle()

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.

Parameters
ppipe
nend to access
Returns
handle for the respective end

Definition at line 1615 of file disk.c.

1617 {
1618  switch (n)
1619  {
1622  return p->fd[n];
1623 
1624  default:
1625  GNUNET_break (0);
1626  return NULL;
1627  }
1628 }

Referenced by child_death_task(), GNUNET_NAT_mini_get_external_ipv4_(), GNUNET_OS_command_run(), GNUNET_SCHEDULER_driver_init(), GNUNET_wait_child(), initiate_put_from_pipe_trigger(), maint_child_death(), restart_nat_server(), run(), shutdown_pipe_cb(), sighandler_child_death(), sighandler_shutdown(), sign_worker(), start_helper(), start_process(), tokenizer_cb(), and work().

Here is the caller graph for this function:

◆ GNUNET_DISK_fix_permissions()

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.

If both arguments are GNUNET_NO, the file is made world-read-write-executable (777). Does nothing on W32.

Parameters
fnname of the file to update
require_uid_matchGNUNET_YES means 700
require_gid_matchGNUNET_YES means 770 unless require_uid_match is set

Definition at line 320 of file disk.c.

323 {
324  mode_t mode;
325 
326  if (GNUNET_YES == require_uid_match)
327  mode = S_IRUSR | S_IWUSR | S_IXUSR;
328  else if (GNUNET_YES == require_gid_match)
329  mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP;
330  else
331  mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH
332  | S_IWOTH | S_IXOTH;
333  if (0 != chmod (fn, mode))
335 }

References GNUNET_ERROR_TYPE_WARNING, GNUNET_log_strerror_file, GNUNET_YES, and mode.

Referenced by create_listen_socket(), LEGACY_SERVICE_start(), and service_task().

Here is the caller graph for this function:

◆ GNUNET_DISK_get_handle_from_int_fd()

struct GNUNET_DISK_FileHandle* GNUNET_DISK_get_handle_from_int_fd ( int  fno)

Get a handle from a native integer FD.

Parameters
fnonative integer file descriptor
Returns
file handle corresponding to the descriptor

Definition at line 1327 of file disk.c.

1328 {
1329  struct GNUNET_DISK_FileHandle *fh;
1330 
1331  if ((((off_t) -1) == lseek (fno, 0, SEEK_CUR)) && (EBADF == errno))
1332  return NULL; /* invalid FD */
1333 
1335 
1336  fh->fd = fno;
1337 
1338  return fh;
1339 }

References GNUNET_DISK_FileHandle::fd, fh, and GNUNET_new.

Referenced by extract_handles(), GNUNET_DISK_get_handle_from_native(), GNUNET_OS_install_parent_control_handler(), main(), start_dump(), and start_insert().

Here is the caller graph for this function:

◆ GNUNET_DISK_get_handle_from_native()

struct GNUNET_DISK_FileHandle* GNUNET_DISK_get_handle_from_native ( FILE *  fd)

Get a handle from a native FD.

Parameters
fdnative file descriptor
Returns
file handle corresponding to the descriptor

Definition at line 1343 of file disk.c.

1344 {
1345  int fno;
1346 
1347  fno = fileno (fd);
1348  if (-1 == fno)
1349  return NULL;
1351 }
struct GNUNET_DISK_FileHandle * GNUNET_DISK_get_handle_from_int_fd(int fno)
Get a handle from a native integer FD.
Definition: disk.c:1327

References GNUNET_DISK_FileHandle::fd, and GNUNET_DISK_get_handle_from_int_fd().

Referenced by run(), and testing_main().

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

◆ GNUNET_DISK_file_read()

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.

Parameters
hhandle to an open file
resultthe buffer to write the result to
lenthe maximum number of bytes to read
Returns
the number of bytes read on success, GNUNET_SYSERR on failure

Definition at line 621 of file disk.c.

624 {
625  if (NULL == h)
626  {
627  errno = EINVAL;
628  return GNUNET_SYSERR;
629  }
630  return read (h->fd, result, len);
631 }
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static int result
Global testing status.

References GNUNET_SYSERR, h, len, and result.

Referenced by child_death_task(), cmd_read(), database_setup(), fh_reader(), file_hash_task(), get_store_message(), GNUNET_CONTAINER_bloomfilter_load(), GNUNET_DISK_fn_read(), GNUNET_FS_data_reader_file_(), GNUNET_FS_handle_on_demand_block(), helper_read(), incrementBit(), initiate_put_from_pipe_trigger(), load_list_messages(), load_list_tunnels(), load_member_session_history(), main(), maint_child_death(), nat_server_read(), parent_control_handler(), print_key(), put_cb(), read_external_ipv4(), read_from_file(), read_task(), REGEX_TEST_read_from_file(), restore_valid_peers(), run(), shutdown_pipe_cb(), start_insert(), try_top_down_reconstruction(), and unindex_reader().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_read_non_blocking()

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.

Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN when no data can be read).

Parameters
hhandle to an open file
resultthe buffer to write the result to
lenthe maximum number of bytes to read
Returns
the number of bytes read on success, GNUNET_SYSERR on failure

Definition at line 635 of file disk.c.

638 {
639  int flags;
640  ssize_t ret;
641 
642  if (NULL == h)
643  {
644  errno = EINVAL;
645  return GNUNET_SYSERR;
646  }
647  /* set to non-blocking, read, then set back */
648  flags = fcntl (h->fd, F_GETFL);
649  if (0 == (flags & O_NONBLOCK))
650  (void) fcntl (h->fd, F_SETFL, flags | O_NONBLOCK);
651  ret = read (h->fd, result, len);
652  if (0 == (flags & O_NONBLOCK))
653  {
654  int eno = errno;
655  (void) fcntl (h->fd, F_SETFL, flags);
656  errno = eno;
657  }
658  return ret;
659 }

References GNUNET_SYSERR, h, len, result, and ret.

◆ GNUNET_DISK_fn_read()

ssize_t GNUNET_DISK_fn_read ( const char *  fn,
void *  result,
size_t  len 
)

Read the contents of a binary file into a buffer.

Parameters
fnfile name
resultthe buffer to write the result to
lenthe maximum number of bytes to read
Returns
number of bytes read, GNUNET_SYSERR on failure

Definition at line 663 of file disk.c.

666 {
667  struct GNUNET_DISK_FileHandle *fh;
668  ssize_t ret;
669  int eno;
670 
674  if (NULL == fh)
675  return GNUNET_SYSERR;
677  eno = errno;
679  errno = eno;
680  return ret;
681 }
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1234
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.
Definition: disk.c:621

References fh, GNUNET_assert, GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_file_read(), GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE, GNUNET_OK, GNUNET_SYSERR, len, result, and ret.

Referenced by discard_hosts_helper(), ego_callback(), gen_topo_from_file(), GNUNET_FRIENDS_parse(), GNUNET_TESTBED_hosts_load_from_file(), GNUNET_TESTING_get_topo_from_file(), load_file(), load_search_strings(), policy_filename_cb(), read_host_file(), run(), and setup_ac().

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

◆ GNUNET_DISK_file_write()

ssize_t GNUNET_DISK_file_write ( const struct GNUNET_DISK_FileHandle h,
const void *  buffer,
size_t  n 
)

Write a buffer to a file.

Parameters
hhandle to open file
bufferthe data to write
nnumber of bytes to write
Returns
number of bytes written on success, GNUNET_SYSERR on error

Definition at line 685 of file disk.c.

688 {
689  if (NULL == h)
690  {
691  errno = EINVAL;
692  return GNUNET_SYSERR;
693  }
694 
695  return write (h->fd, buffer, n);
696 }

References GNUNET_SYSERR, and h.

Referenced by create_hostkeys(), database_shutdown(), do_shutdown(), get_cb(), GNUNET_ATS_solver_logging_write_to_disk(), GNUNET_FRIENDS_write(), GNUNET_OS_process_kill(), GNUNET_TESTING_peer_configure(), handle_estimate(), helper_write(), incrementBit(), iterate_save_entries(), iterate_save_links(), iterate_save_member_session_history_hentries(), iterate_save_messages(), main(), process_result_with_request(), publish_fs_connect_complete_cb(), regex_found_handler(), save_list_messages(), save_list_tunnels(), sighandler_child_death(), sighandler_shutdown(), sign_worker(), start_dump(), stat_iterator(), stats_iterator(), store_and_free_entries(), store_peer_presistently_iterator(), to_file_raw(), tofile_(), try_match_block(), and write_task().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_write_blocking()

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.

Parameters
hhandle to open file
bufferthe data to write
nnumber of bytes to write
Returns
number of bytes written on success, GNUNET_SYSERR on error

Definition at line 700 of file disk.c.

703 {
704  int flags;
705  ssize_t ret;
706 
707  if (NULL == h)
708  {
709  errno = EINVAL;
710  return GNUNET_SYSERR;
711  }
712  /* set to blocking, write, then set back */
713  flags = fcntl (h->fd, F_GETFL);
714  if (0 != (flags & O_NONBLOCK))
715  (void) fcntl (h->fd, F_SETFL, flags - O_NONBLOCK);
716  ret = write (h->fd, buffer, n);
717  if (0 == (flags & O_NONBLOCK))
718  (void) fcntl (h->fd, F_SETFL, flags);
719  return ret;
720 }

References GNUNET_SYSERR, h, and ret.

Referenced by write_benchmark_data().

Here is the caller graph for this function:

◆ GNUNET_DISK_fn_write()

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.

The directory is created if necessary. Fail if filename already exists or if not exactly buf with buf_size bytes could be written to filename.

Parameters
fnfile name
bufthe data to write
buf_sizenumber of bytes to write from buf
modefile permissions
Returns
GNUNET_OK on success, GNUNET_NO if a file existed under filename GNUNET_SYSERR on failure

Definition at line 700 of file disk.c.

728 {
729  char *tmpl;
730  int fd;
731 
732  if (GNUNET_OK !=
734  {
736  "mkstemp",
737  fn);
738  return GNUNET_SYSERR;
739  }
740  {
741  char *dname;
742 
743  dname = GNUNET_strdup (fn);
744  GNUNET_asprintf (&tmpl,
745  "%s/XXXXXX",
746  dirname (dname));
747  GNUNET_free (dname);
748  }
749  fd = mkstemp (tmpl);
750  if (-1 == fd)
751  {
753  "mkstemp",
754  tmpl);
755  GNUNET_free (tmpl);
756  return GNUNET_SYSERR;
757  }
758 
759  if (0 != fchmod (fd,
761  {
763  "chmod",
764  tmpl);
765  GNUNET_assert (0 == close (fd));
766  if (0 != unlink (tmpl))
768  "unlink",
769  tmpl);
770  GNUNET_free (tmpl);
771  return GNUNET_SYSERR;
772  }
773  if (buf_size !=
774  write (fd,
775  buf,
776  buf_size))
777  {
779  "write",
780  tmpl);
781  GNUNET_assert (0 == close (fd));
782  if (0 != unlink (tmpl))
784  "unlink",
785  tmpl);
786  GNUNET_free (tmpl);
787  return GNUNET_SYSERR;
788  }
789  GNUNET_assert (0 == close (fd));
790 
791  if (0 != link (tmpl,
792  fn))
793  {
794  if (0 != unlink (tmpl))
796  "unlink",
797  tmpl);
798  GNUNET_free (tmpl);
799  return GNUNET_NO;
800  }
801  if (0 != unlink (tmpl))
803  "unlink",
804  tmpl);
805  GNUNET_free (tmpl);
806  return GNUNET_OK;
807 
808 
809 }
static char buf[2048]
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.

Referenced by calculate_pow(), clean_task(), discard_hosts_helper(), dump_my_hello(), GNUNET_FRIENDS_parse(), handle_create_message(), main(), run(), shutdown_task(), sync_pow(), update_hello(), and write_proof().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_copy()

enum GNUNET_GenericReturnValue GNUNET_DISK_file_copy ( const char *  src,
const char *  dst 
)

Copy a file.

Parameters
srcfile to copy
dstdestination file name
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 1006 of file disk.c.

1125 {
1126  char *buf;
1127  uint64_t pos;
1128  uint64_t size;
1129  size_t len;
1130  ssize_t sret;
1131  struct GNUNET_DISK_FileHandle *in;
1132  struct GNUNET_DISK_FileHandle *out;
1133 
1135  {
1137  return GNUNET_SYSERR;
1138  }
1139  pos = 0;
1140  in =
1142  if (! in)
1143  {
1145  return GNUNET_SYSERR;
1146  }
1147  out =
1148  GNUNET_DISK_file_open (dst,
1155  if (! out)
1156  {
1159  return GNUNET_SYSERR;
1160  }
1162  while (pos < size)
1163  {
1164  len = COPY_BLK_SIZE;
1165  if (len > size - pos)
1166  len = size - pos;
1167  sret = GNUNET_DISK_file_read (in, buf, len);
1168  if ((sret < 0) || (len != (size_t) sret))
1169  goto FAIL;
1170  sret = GNUNET_DISK_file_write (out, buf, len);
1171  if ((sret < 0) || (len != (size_t) sret))
1172  goto FAIL;
1173  pos += len;
1174  }
1175  GNUNET_free (buf);
1177  GNUNET_DISK_file_close (out);
1178  return GNUNET_OK;
1179  FAIL:
1180  GNUNET_free (buf);
1182  GNUNET_DISK_file_close (out);
1183  return GNUNET_SYSERR;
1184 }
#define COPY_BLK_SIZE
Block size for IO for copying files.
Definition: disk.c:41
@ FAIL
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:685
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).
Definition: disk.c:221

◆ GNUNET_DISK_directory_scan()

int GNUNET_DISK_directory_scan ( const char *  dir_name,
GNUNET_FileNameCallback  callback,
void *  callback_cls 
)

Scan a directory for files.

Parameters
dir_namethe name of the directory
callbackthe method to call for each file
callback_clsclosure for callback
Returns
the number of files found, -1 on error

Definition at line 813 of file disk.c.

816 {
817  DIR *dinfo;
818  struct dirent *finfo;
819  struct stat istat;
820  int count = 0;
822  char *name;
823  char *dname;
824  unsigned int name_len;
825  unsigned int n_size;
826 
827  GNUNET_assert (NULL != dir_name);
829  if (NULL == dname)
830  return GNUNET_SYSERR;
831  while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
832  dname[strlen (dname) - 1] = '\0';
833  if (0 != stat (dname, &istat))
834  {
836  GNUNET_free (dname);
837  return GNUNET_SYSERR;
838  }
839  if (! S_ISDIR (istat.st_mode))
840  {
842  _ ("Expected `%s' to be a directory!\n"),
843  dir_name);
844  GNUNET_free (dname);
845  return GNUNET_SYSERR;
846  }
847  errno = 0;
848  dinfo = opendir (dname);
849  if ((EACCES == errno) || (NULL == dinfo))
850  {
851  LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
852  if (NULL != dinfo)
853  closedir (dinfo);
854  GNUNET_free (dname);
855  return GNUNET_SYSERR;
856  }
857  name_len = 256;
858  n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
859  name = GNUNET_malloc (n_size);
860  while (NULL != (finfo = readdir (dinfo)))
861  {
862  if ((0 == strcmp (finfo->d_name, ".")) ||
863  (0 == strcmp (finfo->d_name, "..")))
864  continue;
865  if (NULL != callback)
866  {
867  if (name_len < strlen (finfo->d_name))
868  {
869  GNUNET_free (name);
870  name_len = strlen (finfo->d_name);
871  n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
872  name = GNUNET_malloc (n_size);
873  }
874  /* dname can end in "/" only if dname == "/";
875  * if dname does not end in "/", we need to add
876  * a "/" (otherwise, we must not!) */
878  n_size,
879  "%s%s%s",
880  dname,
881  (0 == strcmp (dname, DIR_SEPARATOR_STR))
882  ? ""
884  finfo->d_name);
885  ret = callback (callback_cls, name);
886  if (GNUNET_OK != ret)
887  {
888  closedir (dinfo);
889  GNUNET_free (name);
890  GNUNET_free (dname);
891  if (GNUNET_NO == ret)
892  return count;
893  return GNUNET_SYSERR;
894  }
895  }
896  count++;
897  }
898  closedir (dinfo);
899  GNUNET_free (name);
900  GNUNET_free (dname);
901  return count;
902 }
#define LOG(kind,...)
Definition: disk.c:30
static const char * dir_name
Top-level directory we monitor to auto-publish.
#define DIR_SEPARATOR
Definition: platform.h:164
#define DIR_SEPARATOR_STR
Definition: platform.h:165
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
const char * name

References _, dir_name, DIR_SEPARATOR, DIR_SEPARATOR_STR, GNUNET_assert, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_malloc, GNUNET_NO, GNUNET_OK, GNUNET_snprintf(), GNUNET_STRINGS_filename_expand(), GNUNET_SYSERR, LOG, LOG_STRERROR_FILE, name, and ret.

Referenced by cron_clean_data_hosts(), cron_scan_directory_data_hosts(), deserialization_master(), deserialize_download(), deserialize_search(), determine_id(), discover_testbed_nodes(), do_directory_scan(), GNUNET_DISK_directory_remove(), GNUNET_PLUGIN_load_all(), iter_testbed_path(), load_member(), load_member_store(), load_operation_store(), load_srv_handle_configuration(), preprocess_file(), profiler_eval(), run(), and scan().

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

◆ GNUNET_DISK_glob()

int GNUNET_DISK_glob ( const char *  glob_pattern,
GNUNET_FileNameCallback  callback,
void *  callback_cls 
)

Find all files matching a glob pattern.

Currently, the glob_pattern only supports asterisks in the last path component.

Parameters
glob_patternthe glob pattern to search for
callbackthe method to call for each file
callback_clsclosure for callback
Returns
the number of files found, -1 on error

Definition at line 1006 of file disk.c.

1009 {
1010  char *mypat = GNUNET_strdup (glob_pattern);
1011  char *sep;
1012  int ret;
1013 
1014  if ( (NULL != strrchr (glob_pattern, '+')) ||
1015  (NULL != strrchr (glob_pattern, '[')) ||
1016  (NULL != strrchr (glob_pattern, '+')) ||
1017  (NULL != strrchr (glob_pattern, '~')) )
1018  {
1020  "unsupported glob pattern: '%s'\n",
1021  glob_pattern);
1022  GNUNET_free (mypat);
1023  return -1;
1024  }
1025 
1026  sep = strrchr (mypat, DIR_SEPARATOR);
1027  if (NULL == sep)
1028  {
1029  GNUNET_free (mypat);
1030  return -1;
1031  }
1032 
1033  *sep = '\0';
1034 
1035  if (NULL != strchr (mypat, '*'))
1036  {
1037  GNUNET_free (mypat);
1038  GNUNET_break (0);
1040  "glob pattern may only contain '*' in the final path component\n");
1041  return -1;
1042  }
1043 
1044  {
1045  struct GlobClosure gc = {
1046  .glob = sep + 1,
1047  .cb = callback,
1048  .cls = callback_cls,
1049  .nres = 0,
1050  };
1052  "scanning directory '%s' for glob matches on '%s'\n",
1053  mypat,
1054  gc.glob);
1056  glob_cb,
1057  &gc
1058  );
1059  GNUNET_free (mypat);
1060  return (ret < 0) ? ret : gc.nres;
1061  }
1062 }
static enum GNUNET_GenericReturnValue glob_cb(void *cls, const char *filename)
Function called with a filename.
Definition: disk.c:975
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition: disk.c:813
int nres
Number of files that actually matched the glob pattern.
Definition: disk.c:962
const char * glob
Definition: disk.c:955

◆ GNUNET_DISK_directory_create_for_file()

enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create_for_file ( const char *  filename)

Create the directory structure for storing a file.

Parameters
filenamename of a file in the directory
Returns
GNUNET_OK on success, GNUNET_SYSERR on failure, GNUNET_NO if directory exists but is not writeable

Definition at line 380 of file disk.c.

582 {
583  char *rdir;
584  size_t len;
585  int eno;
587 
589  if (NULL == rdir)
590  {
591  errno = EINVAL;
592  return GNUNET_SYSERR;
593  }
594  if (0 == access (rdir, W_OK))
595  {
596  GNUNET_free (rdir);
597  return GNUNET_OK;
598  }
599  len = strlen (rdir);
600  while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
601  len--;
602  rdir[len] = '\0';
603  /* The empty path is invalid and in this case refers to / */
604  if (0 == len)
605  {
606  GNUNET_free (rdir);
607  rdir = GNUNET_strdup ("/");
608  }
610  if ( (GNUNET_OK == res) &&
611  (0 != access (rdir, W_OK)) )
612  res = GNUNET_NO;
613  eno = errno;
614  GNUNET_free (rdir);
615  errno = eno;
616  return res;
617 }
static int res
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create(const char *dir)
Implementation of "mkdir -p".
Definition: disk.c:495

Referenced by callback_set_handle_name(), create_hostkeys(), database_setup(), get_server_addresses(), GNUNET_ATS_solver_logging_write_to_disk(), GNUNET_DISK_file_close(), GNUNET_FRIENDS_write_start(), GNUNET_FS_download_sync_(), LEGACY_SERVICE_get_server_addresses(), main(), make_serialization_file_name(), make_serialization_file_name_in_dir(), run(), save(), save_hostlist_file(), setup_log_file(), store_valid_peers(), trigger_recursive_download(), unix_transport_server_start(), and update_hello().

Here is the caller graph for this function:

◆ GNUNET_DISK_directory_test()

enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test ( const char *  fil,
int  is_readable 
)

Test if fil is a directory and listable.

Optionally, also check if the directory is readable. Will not print an error message if the directory does not exist. Will log errors if GNUNET_SYSERR is returned (i.e., a file exists with the same name).

Parameters
filfilename to test
is_readableGNUNET_YES to additionally check if fil is readable; GNUNET_NO to disable this check
Returns
GNUNET_YES if yes, GNUNET_NO if not; GNUNET_SYSERR if it does not exist or stated

Definition at line 380 of file disk.c.

404 {
405  struct stat filestat;
406  int ret;
407 
408  ret = stat (fil, &filestat);
409  if (ret != 0)
410  {
411  if (errno != ENOENT)
413  return GNUNET_SYSERR;
414  }
415  if (! S_ISDIR (filestat.st_mode))
416  {
418  "A file already exits with the same name %s\n",
419  fil);
420  return GNUNET_NO;
421  }
422  if (GNUNET_YES == is_readable)
423  ret = access (fil, R_OK | X_OK);
424  else
425  ret = access (fil, X_OK);
426  if (ret < 0)
427  {
429  return GNUNET_NO;
430  }
431  return GNUNET_YES;
432 }
@ GNUNET_ERROR_TYPE_INFO

Referenced by callback_scan_for_members(), callback_scan_for_sessions(), callback_set_handle_name(), create_service(), deserialization_master(), deserialize_download(), deserialize_search(), get_store_operation_type(), GNUNET_CONFIGURATION_load(), GNUNET_FS_remove_sync_dir_(), GNUNET_OS_installation_get_path(), iterate_load_next_member_sessions(), iterate_load_next_session(), iterate_save_members(), iterate_save_session(), load_member(), load_member_store(), load_operation_store(), load_srv_handle_configuration(), load_srv_room(), remove_srv_room(), run(), save_member(), save_member_store(), save_srv_handle_configuration(), and save_srv_room().

Here is the caller graph for this function:

◆ GNUNET_DISK_directory_remove()

enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove ( const char *  filename)

Remove all files in a directory (rm -rf).

Call with caution.

Parameters
filenamethe file to remove
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 1006 of file disk.c.

1085 {
1086  struct stat istat;
1087 
1088  if (NULL == filename)
1089  {
1090  GNUNET_break (0);
1091  return GNUNET_SYSERR;
1092  }
1093  if (0 != lstat (filename, &istat))
1094  return GNUNET_NO; /* file may not exist... */
1095  (void) chmod (filename,
1096  S_IWUSR | S_IRUSR | S_IXUSR);
1097  if (0 == unlink (filename))
1098  return GNUNET_OK;
1099  if ( (errno != EISDIR) &&
1100  /* EISDIR is not sufficient in all cases, e.g.
1101  * sticky /tmp directory may result in EPERM on BSD.
1102  * So we also explicitly check "isDirectory" */
1103  (GNUNET_YES !=
1105  GNUNET_YES)) )
1106  {
1108  return GNUNET_SYSERR;
1109  }
1110  if (GNUNET_SYSERR ==
1112  return GNUNET_SYSERR;
1113  if (0 != rmdir (filename))
1114  {
1116  return GNUNET_SYSERR;
1117  }
1118  return GNUNET_OK;
1119 }
static enum GNUNET_GenericReturnValue remove_helper(void *unused, const char *fn)
Function that removes the given directory by calling GNUNET_DISK_directory_remove().
Definition: disk.c:1074
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition: disk.c:403

References DIR_SEPARATOR, GlobClosure::glob, glob_cb(), GNUNET_break, GNUNET_DISK_directory_scan(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_strdup, LOG, GlobClosure::nres, and ret.

Referenced by calculate_pow(), callback_set_handle_name(), clean_task(), create_response(), delete_files(), discard_hosts_helper(), GNUNET_FS_remove_sync_dir_(), GNUNET_TESTING_system_destroy(), main(), publish_timeout(), remove_room_member_session(), remove_srv_room(), report_uri(), run(), and write_proof().

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

◆ GNUNET_DISK_purge_cfg_dir()

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.

Parameters
cfg_filenameconfiguration file to parse
optionoption with the dir name to purge

Definition at line 1677 of file disk.c.

1679 {
1682  &purge_cfg_dir,
1683  (void *) option));
1684 }
static enum GNUNET_GenericReturnValue purge_cfg_dir(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
Helper function for GNUNET_DISK_purge_cfg_dir.
Definition: disk.c:1653
static char * cfg_filename
Name of the configuration file.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_parse_and_run(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.

References cfg_filename, GNUNET_break, GNUNET_CONFIGURATION_parse_and_run(), GNUNET_OK, and purge_cfg_dir().

Here is the call graph for this function:

◆ GNUNET_DISK_directory_create()

enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create ( const char *  dir)

Implementation of "mkdir -p".

Parameters
dirthe directory to create
Returns
GNUNET_SYSERR on failure, GNUNET_OK otherwise

Definition at line 380 of file disk.c.

496 {
497  char *rdir;
498  unsigned int len;
499  unsigned int pos;
500  unsigned int pos2;
501  int ret = GNUNET_OK;
502 
504  if (rdir == NULL)
505  {
506  GNUNET_break (0);
507  return GNUNET_SYSERR;
508  }
509 
510  len = strlen (rdir);
511 
512  pos = 1; /* skip heading '/' */
513 
514  /* Check which low level directories already exist */
515  pos2 = len;
516  rdir[len] = DIR_SEPARATOR;
517  while (pos <= pos2)
518  {
519  if (DIR_SEPARATOR == rdir[pos2])
520  {
521  rdir[pos2] = '\0';
523  if (GNUNET_NO == ret)
524  {
526  "Creating directory `%s' failed",
527  rdir);
528  GNUNET_free (rdir);
529  return GNUNET_SYSERR;
530  }
531  rdir[pos2] = DIR_SEPARATOR;
532  if (GNUNET_YES == ret)
533  {
534  pos2++;
535  break;
536  }
537  }
538  pos2--;
539  }
540  rdir[len] = '\0';
541  if (pos < pos2)
542  pos = pos2;
543  /* Start creating directories */
544  while (pos <= len)
545  {
546  if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
547  {
548  rdir[pos] = '\0';
550  if (GNUNET_NO == ret)
551  {
553  "Creating directory `%s' failed",
554  rdir);
555  GNUNET_free (rdir);
556  return GNUNET_SYSERR;
557  }
558  if (GNUNET_SYSERR == ret)
559  {
560  ret = mkdir (rdir,
561  S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH
562  | S_IXOTH); /* 755 */
563 
564  if ((ret != 0) && (errno != EEXIST))
565  {
567  GNUNET_free (rdir);
568  return GNUNET_SYSERR;
569  }
570  }
571  rdir[pos] = DIR_SEPARATOR;
572  }
573  pos++;
574  }
575  GNUNET_free (rdir);
576  return GNUNET_OK;
577 }
static char * dir
Set to the directory where runtime files are stored.
Definition: gnunet-arm.c:89
#define GNUNET_log(kind,...)

References GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, LOG_STRERROR_FILE, mktemp_name(), and t.

Referenced by create_service(), cron_scan_directory_data_hosts(), ensure_folder_exist(), get_store_operation_type(), iterate_save_members(), iterate_save_session(), jwks_endpoint(), run(), save_member(), save_member_store(), save_srv_handle_configuration(), save_srv_room(), token_endpoint(), write_benchmark_data(), and write_pid_file().

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

◆ GNUNET_DISK_filename_canonicalize()

void GNUNET_DISK_filename_canonicalize ( char *  fn)

Removes special characters as ':' from a filename.

Parameters
fnthe filename to canonicalize

Definition at line 1188 of file disk.c.

1189 {
1190  char *idx;
1191  char c;
1192 
1193  for (idx = fn; *idx; idx++)
1194  {
1195  c = *idx;
1196 
1197  if ((c == '/') || (c == '\\') || (c == ':') || (c == '*') || (c == '?') ||
1198  (c ==
1199  '"')
1200  ||
1201  (c == '<') || (c == '>') || (c == '|') )
1202  {
1203  *idx = '_';
1204  }
1205  }
1206 }

Referenced by progress_cb().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_change_owner()

enum GNUNET_GenericReturnValue GNUNET_DISK_file_change_owner ( const char *  filename,
const char *  user 
)

Change owner of a file.

Parameters
filenamefile to change
usernew owner of the file
Returns
GNUNET_OK on success, GNUNET_SYSERR on failure

Definition at line 1188 of file disk.c.

1212 {
1213  struct passwd *pws;
1214 
1215  pws = getpwnam (user);
1216  if (NULL == pws)
1217  {
1219  _ ("Cannot obtain information about user `%s': %s\n"),
1220  user,
1221  strerror (errno));
1222  return GNUNET_SYSERR;
1223  }
1224  if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
1225  {
1227  return GNUNET_SYSERR;
1228  }
1229  return GNUNET_OK;
1230 }

Referenced by write_pid_file().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_map()

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.

Parameters
hopen file handle
mhandle to the new mapping (will be set)
accessaccess specification, GNUNET_DISK_MAP_TYPE_xxx
lensize of the mapping
Returns
pointer to the mapped memory region, NULL on failure

Definition at line 1377 of file disk.c.

1381 {
1382  int prot;
1383 
1384  if (NULL == h)
1385  {
1386  errno = EINVAL;
1387  return NULL;
1388  }
1389  prot = 0;
1390  if (access & GNUNET_DISK_MAP_TYPE_READ)
1391  prot = PROT_READ;
1392  if (access & GNUNET_DISK_MAP_TYPE_WRITE)
1393  prot |= PROT_WRITE;
1394  *m = GNUNET_new (struct GNUNET_DISK_MapHandle);
1395  (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1396  GNUNET_assert (NULL != (*m)->addr);
1397  if (MAP_FAILED == (*m)->addr)
1398  {
1399  GNUNET_free (*m);
1400  return NULL;
1401  }
1402  (*m)->len = len;
1403  return (*m)->addr;
1404 }
#define MAP_FAILED
Definition: disk.c:1372
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:104
Handle for a memory-mapping operation.
Definition: disk.c:1358

References GNUNET_assert, GNUNET_DISK_MAP_TYPE_READ, GNUNET_DISK_MAP_TYPE_WRITE, GNUNET_free, GNUNET_new, h, len, m, and MAP_FAILED.

Referenced by database_setup(), full_recursive_download(), hostkeys_load(), load_etc_hosts(), load_keys(), lookup_dns_servers(), and run().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_unmap()

enum GNUNET_GenericReturnValue GNUNET_DISK_file_unmap ( struct GNUNET_DISK_MapHandle h)

Unmap a file.

Parameters
hmapping handle
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1377 of file disk.c.

1409 {
1411 
1412  if (NULL == h)
1413  {
1414  errno = EINVAL;
1415  return GNUNET_SYSERR;
1416  }
1417  ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1418  GNUNET_free (h);
1419  return ret;
1420 }

Referenced by database_setup(), full_recursive_download(), hostkeys_unload(), load_etc_hosts(), lookup_dns_servers(), run(), and unload_keys().

Here is the caller graph for this function:

◆ GNUNET_DISK_file_sync()

enum GNUNET_GenericReturnValue GNUNET_DISK_file_sync ( const struct GNUNET_DISK_FileHandle h)

Write file changes to disk.

Parameters
hhandle to an open file
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1377 of file disk.c.

1425 {
1426  if (h == NULL)
1427  {
1428  errno = EINVAL;
1429  return GNUNET_SYSERR;
1430  }
1431 
1432 #if ! defined(__linux__) || ! defined(GNU)
1433  return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1434 #else
1435  return fdatasync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1436 #endif
1437 }

Referenced by save_list_messages(), save_list_tunnels(), save_member_session_history(), and save_message_store().

Here is the caller graph for this function: