GNUnet  0.11.x
Data Structures | Enumerations | Functions
Disk library

Disk IO APIs. More...

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

Functions

int GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
 Checks whether a handle is invalid. More...
 
int 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...
 
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...
 
int 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...
 
int 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...
 
int 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 (int blocking_read, int blocking_write, int inherit_read, int inherit_write)
 Creates an interprocess channel. More...
 
struct GNUNET_DISK_PipeHandleGNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2])
 Creates a pipe object from a couple of file descriptors. More...
 
int GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
 Closes an interprocess channel. More...
 
int 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...
 
int 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...
 
ssize_t GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n, enum GNUNET_DISK_AccessPermissions mode)
 Write a buffer to a file. More...
 
int 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_directory_create_for_file (const char *filename)
 Create the directory structure for storing a file. More...
 
int GNUNET_DISK_directory_test (const char *fil, int is_readable)
 Test if fil is a directory and listable. More...
 
int 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...
 
int GNUNET_DISK_directory_create (const char *dir)
 Implementation of "mkdir -p". More...
 
int GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lock_start, off_t lock_end, int excl)
 Lock a part of a file. More...
 
int GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlock_start, off_t unlock_end)
 Unlock a part of a file. More...
 
void GNUNET_DISK_filename_canonicalize (char *fn)
 Removes special characters as ':' from a filename. More...
 
int 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...
 
int GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
 Unmap a file. More...
 
int 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 41 of file gnunet_disk_lib.h.

42 {
47 
52 
57 };
Handle represents a file.
Handle represents a pipe.
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 91 of file gnunet_disk_lib.h.

92 {
97 
102 
107 
112 
117 
122 
127 };
Open the file for reading.
Create file if it doesn't exist.
Append to the file.
Truncate file if it exists.
Open the file for writing.
Open the file for both reading and writing.
Fail if file already exists.

◆ 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 132 of file gnunet_disk_lib.h.

133 {
138 
143 
148 };
Read-write memory map.
Read-only memory map.
Write-able 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 154 of file gnunet_disk_lib.h.

155 {
160 
165 
170 
175 
180 
185 
190 
195 
200 
205 };
Everybody can execute.
Nobody is allowed to do anything to the file.
Everybody can write.
Everybody can read.

◆ 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 212 of file gnunet_disk_lib.h.

213 {
218 
223 
228 };
Seek a relative position (from the current offset).
Seek an absolute position from the end of the file.
Seek an absolute position (from the start 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 234 of file gnunet_disk_lib.h.

235 {
240 
245 };
The writing-end of a pipe.
The reading-end of a pipe.

Function Documentation

◆ GNUNET_DISK_handle_invalid()

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

References GNUNET_DISK_FileHandle::fd, GNUNET_NO, and GNUNET_YES.

Referenced by decrementBit(), file_hash_finish(), incrementBit(), make_empty_file(), and shutdown_pipe_cb().

193 {
194  return ((! h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO;
195 }
#define GNUNET_NO
Definition: gnunet_common.h:86
#define GNUNET_YES
Definition: gnunet_common.h:85
int fd
File handle on Unix-like systems.
Here is the caller graph for this function:

◆ GNUNET_DISK_file_test()

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

References GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_NO, GNUNET_STRINGS_filename_expand(), GNUNET_SYSERR, GNUNET_YES, LOG_STRERROR_FILE, and ret.

Referenced by create_download_context(), create_unique_cfgs(), database_setup(), ego_callback(), gen_topo_from_file(), get_path_from_PATH(), GNUNET_CONTAINER_bloomfilter_load(), GNUNET_CRYPTO_ecdsa_key_create_from_file(), GNUNET_CRYPTO_eddsa_key_create_from_file(), GNUNET_DISK_file_open(), GNUNET_FRIENDS_parse(), GNUNET_FRIENDS_write_start(), GNUNET_FS_download_start_task_(), GNUNET_HOSTLIST_client_start(), GNUNET_PQ_run_sql(), GNUNET_PROGRAM_run2(), GNUNET_SERVICE_run_(), GNUNET_TESTBED_hosts_load_from_file(), GNUNET_TRANSPORT_TESTING_start_peer(), hostkeys_load(), hosts_directory_scan_callback(), LEGACY_SERVICE_run(), load_hostlist_file(), load_search_strings(), policy_filename_cb(), print_key(), read_host_file(), read_index_list(), read_update_information_graph(), restore_valid_peers(), and run().

545 {
546  struct stat filestat;
547  int ret;
548  char *rdir;
549 
550  rdir = GNUNET_STRINGS_filename_expand (fil);
551  if (rdir == NULL)
552  return GNUNET_SYSERR;
553 
554  ret = stat (rdir, &filestat);
555  if (ret != 0)
556  {
557  if (errno != ENOENT)
558  {
560  GNUNET_free (rdir);
561  return GNUNET_SYSERR;
562  }
563  GNUNET_free (rdir);
564  return GNUNET_NO;
565  }
566  if (! S_ISREG (filestat.st_mode))
567  {
568  GNUNET_free (rdir);
569  return GNUNET_NO;
570  }
571  if (access (rdir, F_OK) < 0)
572  {
574  GNUNET_free (rdir);
575  return GNUNET_SYSERR;
576  }
577  GNUNET_free (rdir);
578  return GNUNET_YES;
579 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_NO
Definition: gnunet_common.h:86
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition: strings.c:617
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:36
#define GNUNET_YES
Definition: gnunet_common.h:85
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
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 436 of file disk.c.

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

Referenced by GNUNET_FRIENDS_write_start().

437 {
438  size_t slen;
439  char *target;
440  unsigned int num;
441 
442  slen = strlen (fil) + 20;
443  target = GNUNET_malloc (slen);
444  num = 0;
445  do
446  {
447  GNUNET_snprintf (target, slen, "%s.%u~", fil, num++);
448  }
449  while (0 == access (target, F_OK));
450  if (0 != rename (fil, target))
452  GNUNET_free (target);
453 }
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
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
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 226 of file disk.c.

References GNUNET_DISK_FileHandle::fd, GNUNET_SYSERR, and t.

Referenced by decrementBit(), fh_reader(), GNUNET_FS_data_reader_file_(), GNUNET_FS_handle_on_demand_block(), incrementBit(), make_empty_file(), process_result_with_request(), try_top_down_reconstruction(), and unindex_reader().

229 {
230  if (h == NULL)
231  {
232  errno = EINVAL;
233  return GNUNET_SYSERR;
234  }
235 
236  static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END };
237 
238  return lseek (h->fd, offset, t[whence]);
239 }
static struct GNUNET_SCHEDULER_Task * t
Main task.
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
int fd
File handle on Unix-like systems.
Here is the caller graph for this function:

◆ GNUNET_DISK_file_size()

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

References getSizeRec(), GNUNET_assert, GetFileSizeData::include_sym_links, ret, GetFileSizeData::single_file_mode, and GetFileSizeData::total.

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

261 {
262  struct GetFileSizeData gfsd;
263  int ret;
264 
265  GNUNET_assert (size != NULL);
266  gfsd.total = 0;
267  gfsd.include_sym_links = include_symbolic_links;
268  gfsd.single_file_mode = single_file_mode;
269  ret = getSizeRec (&gfsd, filename);
270  *size = gfsd.total;
271  return ret;
272 }
int single_file_mode
GNUNET_YES if mode is file-only (return total == -1 for directories).
Definition: disk.c:97
Closure for the recursion to determine the file size of a directory.
Definition: disk.c:82
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static char * filename
static int getSizeRec(void *cls, const char *fn)
Iterate over all files in the given directory and accumulate their size.
Definition: disk.c:146
static unsigned int size
Size of the "table".
Definition: peer.c:67
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_DISK_file_get_identifiers()

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

References GNUNET_OK, and GNUNET_SYSERR.

Referenced by handle_client_index_start(), and hash_for_index_cb().

294 {
295 #if HAVE_STAT
296  {
297  struct stat sbuf;
298 
299  if (0 != stat (filename, &sbuf))
300  {
301  return GNUNET_SYSERR;
302  }
303  *ino = (uint64_t) sbuf.st_ino;
304  }
305 #else
306  *ino = 0;
307 #endif
308 #if HAVE_STATVFS
309  {
310  struct statvfs fbuf;
311 
312  if (0 != statvfs (filename, &fbuf))
313  {
314  return GNUNET_SYSERR;
315  }
316  *dev = (uint64_t) fbuf.f_fsid;
317  }
318 #elif HAVE_STATFS
319  {
320  struct statfs fbuf;
321 
322  if (0 != statfs (filename, &fbuf))
323  {
324  return GNUNET_SYSERR;
325  }
326  *dev =
327  ((uint64_t) fbuf.f_fsid.val[0]) << 32 || ((uint64_t) fbuf.f_fsid.val[1]);
328  }
329 #else
330  *dev = 0;
331 #endif
332  return GNUNET_OK;
333 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
static char * filename
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
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 468 of file disk.c.

References GNUNET_DISK_PipeHandle::fd, fn, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, LOG_STRERROR_FILE, and mktemp_name().

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

469 {
470  int fd;
471  char *fn;
472  mode_t omask;
473 
474  omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
475  fn = mktemp_name (t);
476  if (-1 == (fd = mkstemp (fn)))
477  {
479  GNUNET_free (fn);
480  umask (omask);
481  return NULL;
482  }
483  umask (omask);
484  if (0 != close (fd))
486  return fn;
487 }
static struct GNUNET_SCHEDULER_Task * t
Main task.
static char * fn
Filename of the unique file.
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:36
static char * mktemp_name(const char *t)
Create the name for a temporary file or directory from a template.
Definition: disk.c:343
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
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

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

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

Referenced by access_handler_callback(), and GNUNET_TESTING_system_create_with_portrange().

410 {
411  char *fn;
412  mode_t omask;
413 
414  omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
415  fn = mktemp_name (t);
416  if (fn != mkdtemp (fn))
417  {
419  GNUNET_free (fn);
420  umask (omask);
421  return NULL;
422  }
423  umask (omask);
424  return fn;
425 }
static struct GNUNET_SCHEDULER_Task * t
Main task.
static char * fn
Filename of the unique file.
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:36
static char * mktemp_name(const char *t)
Create the name for a temporary file or directory from a template.
Definition: disk.c:343
#define GNUNET_free(ptr)
Wrapper around free.
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 1267 of file disk.c.

References GNUNET_DISK_FileHandle::fd, GNUNET_DISK_PipeHandle::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 create_hostkeys(), database_setup(), database_shutdown(), full_recursive_download(), get_file_handle(), GNUNET_ATS_solver_logging_write_to_disk(), GNUNET_ATS_TEST_logging_write_to_file(), GNUNET_BIO_read_open(), GNUNET_BIO_write_open(), GNUNET_CONTAINER_bloomfilter_load(), GNUNET_CRYPTO_ecdsa_key_create_from_file(), GNUNET_CRYPTO_eddsa_key_create_from_file(), GNUNET_CRYPTO_hash_file(), GNUNET_DISK_file_copy(), GNUNET_DISK_fn_read(), GNUNET_DISK_fn_write(), 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(), GNUNET_TIME_absolute_get_monotonic(), hostkeys_load(), iface_proc(), load_etc_hosts(), load_keys(), lookup_dns_servers(), main(), print_key(), process_result_with_request(), publish_fs_connect_complete_cb(), REGEX_TEST_read_from_file(), restore_valid_peers(), run(), server_log(), start_dump(), start_insert(), store_valid_peers(), to_file_raw(), tofile_(), try_match_block(), write_benchmark_data(), write_bw_gnuplot_script(), write_rtt_gnuplot_script(), and write_throughput_gnuplot_script().

1270 {
1271  char *expfn;
1272  struct GNUNET_DISK_FileHandle *ret;
1273 
1274  int oflags;
1275  int mode;
1276  int fd;
1277 
1279  if (NULL == expfn)
1280  return NULL;
1281 
1282  mode = 0;
1284  oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */
1285  else if (flags & GNUNET_DISK_OPEN_READ)
1286  oflags = O_RDONLY;
1287  else if (flags & GNUNET_DISK_OPEN_WRITE)
1288  oflags = O_WRONLY;
1289  else
1290  {
1291  GNUNET_break (0);
1292  GNUNET_free (expfn);
1293  return NULL;
1294  }
1295  if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1296  oflags |= (O_CREAT | O_EXCL);
1297  if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1298  oflags |= O_TRUNC;
1299  if (flags & GNUNET_DISK_OPEN_APPEND)
1300  oflags |= O_APPEND;
1302  {
1303  if (flags & GNUNET_DISK_OPEN_CREATE)
1304  {
1306  oflags |= O_CREAT;
1307  mode = translate_unix_perms (perm);
1308  }
1309  }
1310 
1311  fd = open (expfn,
1312  oflags
1313 #if O_CLOEXEC
1314  | O_CLOEXEC
1315 #endif
1316  | O_LARGEFILE,
1317  mode);
1318  if (fd == -1)
1319  {
1320  if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))
1322  else
1323  LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "open", expfn);
1324  GNUNET_free (expfn);
1325  return NULL;
1326  }
1327 
1328  ret = GNUNET_new (struct GNUNET_DISK_FileHandle);
1329 
1330  ret->fd = fd;
1331 
1332  GNUNET_free (expfn);
1333  return ret;
1334 }
int 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:544
Open the file for reading.
Create file if it doesn&#39;t exist.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
int GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:684
Append to the file.
#define GNUNET_NO
Definition: gnunet_common.h:86
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static enum @8 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
static int translate_unix_perms(enum GNUNET_DISK_AccessPermissions perm)
Translate GNUnet-internal permission bitmap to UNIX file access permission bitmap.
Definition: disk.c:109
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define O_LARGEFILE
Definition: platform.h:220
static char * fn
Filename of the unique file.
Truncate file if it exists.
Open the file for writing.
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition: strings.c:617
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:36
Open the file for both reading and writing.
int fd
File handle on Unix-like systems.
Fail if file already exists.
Handle used to access files (and pipes).
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_DISK_file_handle_size()

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

References GNUNET_DISK_FileHandle::fd, GNUNET_OK, and GNUNET_SYSERR.

Referenced by GNUNET_CONTAINER_bloomfilter_load(), GNUNET_TIME_absolute_get_monotonic(), load_etc_hosts(), lookup_dns_servers(), REGEX_TEST_read_from_file(), and restore_valid_peers().

207 {
208  struct stat sbuf;
209 
210  if (0 != fstat (fh->fd, &sbuf))
211  return GNUNET_SYSERR;
212  *size = sbuf.st_size;
213  return GNUNET_OK;
214 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
static unsigned int size
Size of the "table".
Definition: peer.c:67
int fd
File handle on Unix-like systems.
Here is the caller graph for this function:

◆ GNUNET_DISK_pipe()

struct GNUNET_DISK_PipeHandle* GNUNET_DISK_pipe ( int  blocking_read,
int  blocking_write,
int  inherit_read,
int  inherit_write 
)

Creates an interprocess channel.

Parameters
blocking_readcreates an asynchronous pipe for reading if set to GNUNET_NO
blocking_writecreates an asynchronous pipe for writing if set to GNUNET_NO
inherit_read1 to make read handle inheritable, 0 otherwise (NT only)
inherit_write1 to make write handle inheritable, 0 otherwise (NT only)
Returns
handle to the new pipe, NULL on error
Parameters
blocking_readcreates an asynchronous pipe for reading if set to GNUNET_NO
blocking_writecreates an asynchronous pipe for writing if set to GNUNET_NO
inherit_readinherit the parent processes stdin (only for windows)
inherit_writeinherit the parent processes stdout (only for windows)
Returns
handle to the new pipe, NULL on error

Definition at line 1525 of file disk.c.

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

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

1529 {
1530  int fd[2];
1531  int ret;
1532  int eno;
1533 
1534  (void) inherit_read;
1535  (void) inherit_write;
1536  ret = pipe (fd);
1537  if (ret == -1)
1538  {
1539  eno = errno;
1541  errno = eno;
1542  return NULL;
1543  }
1544  return GNUNET_DISK_pipe_from_fd (blocking_read, blocking_write, fd);
1545 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define LOG_STRERROR(kind, syscall)
Definition: disk.c:33
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe_from_fd(int blocking_read, int blocking_write, int fd[2])
Creates a pipe object from a couple of file descriptors.
Definition: disk.c:1559
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 ( int  blocking_read,
int  blocking_write,
int  fd[2] 
)

Creates a pipe object from a couple of file descriptors.

Useful for wrapping existing pipe FDs.

Parameters
blocking_readcreates an asynchronous pipe for reading if set to GNUNET_NO
blocking_writecreates an asynchronous pipe for writing if set to GNUNET_NO
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

Useful for wrapping existing pipe FDs.

Parameters
blocking_readcreates an asynchronous pipe for reading if set to GNUNET_NO
blocking_writecreates an asynchronous pipe for writing if set to GNUNET_NO
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 1559 of file disk.c.

References GNUNET_DISK_FileHandle::fd, GNUNET_DISK_PipeHandle::fd, GNUNET_break, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_free_non_null, GNUNET_new, LOG_STRERROR, p, and ret.

Referenced by GNUNET_DISK_pipe().

1560 {
1561  struct GNUNET_DISK_PipeHandle *p;
1562 
1563  p = GNUNET_new (struct GNUNET_DISK_PipeHandle);
1564 
1565  int ret;
1566  int flags;
1567  int eno = 0; /* make gcc happy */
1568 
1569  ret = 0;
1570  if (fd[0] >= 0)
1571  {
1572  p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle);
1573  p->fd[0]->fd = fd[0];
1574  if (! blocking_read)
1575  {
1576  flags = fcntl (fd[0], F_GETFL);
1577  flags |= O_NONBLOCK;
1578  if (0 > fcntl (fd[0], F_SETFL, flags))
1579  {
1580  ret = -1;
1581  eno = errno;
1582  }
1583  }
1584  flags = fcntl (fd[0], F_GETFD);
1585  flags |= FD_CLOEXEC;
1586  if (0 > fcntl (fd[0], F_SETFD, flags))
1587  {
1588  ret = -1;
1589  eno = errno;
1590  }
1591  }
1592 
1593  if (fd[1] >= 0)
1594  {
1595  p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle);
1596  p->fd[1]->fd = fd[1];
1597  if (! blocking_write)
1598  {
1599  flags = fcntl (fd[1], F_GETFL);
1600  flags |= O_NONBLOCK;
1601  if (0 > fcntl (fd[1], F_SETFL, flags))
1602  {
1603  ret = -1;
1604  eno = errno;
1605  }
1606  }
1607  flags = fcntl (fd[1], F_GETFD);
1608  flags |= FD_CLOEXEC;
1609  if (0 > fcntl (fd[1], F_SETFD, flags))
1610  {
1611  ret = -1;
1612  eno = errno;
1613  }
1614  }
1615  if (ret == -1)
1616  {
1617  errno = eno;
1619  if (p->fd[0]->fd >= 0)
1620  GNUNET_break (0 == close (p->fd[0]->fd));
1621  if (p->fd[1]->fd >= 0)
1622  GNUNET_break (0 == close (p->fd[1]->fd));
1623  GNUNET_free_non_null (p->fd[0]);
1624  GNUNET_free_non_null (p->fd[1]);
1625  GNUNET_free (p);
1626  errno = eno;
1627  return NULL;
1628  }
1629 
1630  return p;
1631 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define LOG_STRERROR(kind, syscall)
Definition: disk.c:33
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_DISK_FileHandle * fd[2]
File descriptors for the pipe.
Definition: disk.c:74
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
int fd
File handle on Unix-like systems.
Handle used to access files (and pipes).
Handle used to manage a pipe.
Definition: disk.c:68
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ GNUNET_DISK_pipe_close()

int GNUNET_DISK_pipe_close ( struct GNUNET_DISK_PipeHandle p)

Closes an interprocess channel.

Parameters
ppipe
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise
Parameters
ppipe to close
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1714 of file disk.c.

References GNUNET_DISK_pipe_close_end(), GNUNET_DISK_PIPE_END_READ, GNUNET_DISK_PIPE_END_WRITE, GNUNET_free, GNUNET_OK, and ret.

Referenced by 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(), maint_child_death(), nat_server_read(), restart_nat_server(), and start_process().

1715 {
1716  int ret = GNUNET_OK;
1717 
1718  int read_end_close;
1719  int write_end_close;
1720  int read_end_close_errno;
1721  int write_end_close_errno;
1722 
1724  read_end_close_errno = errno;
1726  write_end_close_errno = errno;
1727  GNUNET_free (p);
1728 
1729  if (GNUNET_OK != read_end_close)
1730  {
1731  errno = read_end_close_errno;
1732  ret = read_end_close;
1733  }
1734  else if (GNUNET_OK != write_end_close)
1735  {
1736  errno = write_end_close_errno;
1737  ret = write_end_close;
1738  }
1739 
1740  return ret;
1741 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
The writing-end of a pipe.
The reading-end of a pipe.
int GNUNET_DISK_pipe_close_end(struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd end)
Closes an interprocess channel.
Definition: disk.c:1642
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_DISK_pipe_close_end()

int 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

Closes one half of an interprocess channel.

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

Definition at line 1642 of file disk.c.

References GNUNET_DISK_PipeHandle::fd, GNUNET_DISK_file_close(), GNUNET_DISK_PIPE_END_READ, GNUNET_DISK_PIPE_END_WRITE, GNUNET_OK, and ret.

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

1644 {
1645  int ret = GNUNET_OK;
1646 
1648  {
1649  if (p->fd[0])
1650  {
1651  ret = GNUNET_DISK_file_close (p->fd[0]);
1652  p->fd[0] = NULL;
1653  }
1654  }
1655  else if (end == GNUNET_DISK_PIPE_END_WRITE)
1656  {
1657  if (p->fd[1])
1658  {
1659  ret = GNUNET_DISK_file_close (p->fd[1]);
1660  p->fd[1] = NULL;
1661  }
1662  }
1663 
1664  return ret;
1665 }
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1344
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
struct GNUNET_DISK_FileHandle * fd[2]
File descriptors for the pipe.
Definition: disk.c:74
The writing-end of a pipe.
The reading-end of a pipe.
Here is the call graph for this function:
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 1681 of file disk.c.

References GNUNET_DISK_PipeHandle::fd, GNUNET_DISK_PIPE_END_READ, GNUNET_DISK_PIPE_END_WRITE, and ret.

Referenced by GNUNET_ARM_request_service_start(), and start_process().

1683 {
1684  struct GNUNET_DISK_FileHandle *ret = NULL;
1685 
1687  {
1688  if (p->fd[0])
1689  {
1690  ret = p->fd[0];
1691  p->fd[0] = NULL;
1692  }
1693  }
1694  else if (end == GNUNET_DISK_PIPE_END_WRITE)
1695  {
1696  if (p->fd[1])
1697  {
1698  ret = p->fd[1];
1699  p->fd[1] = NULL;
1700  }
1701  }
1702 
1703  return ret;
1704 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
struct GNUNET_DISK_FileHandle * fd[2]
File descriptors for the pipe.
Definition: disk.c:74
The writing-end of a pipe.
The reading-end of a pipe.
Handle used to access files (and pipes).
Here is the caller graph for this function:

◆ GNUNET_DISK_file_close()

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

References GNUNET_DISK_FileHandle::fd, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_OK, GNUNET_SYSERR, LOG_STRERROR, and ret.

Referenced by check_completed(), cleanup(), 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_ATS_TEST_logging_write_to_file(), GNUNET_BIO_flush(), GNUNET_BIO_read_close(), GNUNET_BIO_write_close(), GNUNET_CONTAINER_bloomfilter_free(), GNUNET_CONTAINER_bloomfilter_load(), GNUNET_CRYPTO_ecdsa_key_create_from_file(), GNUNET_CRYPTO_eddsa_key_create_from_file(), GNUNET_CRYPTO_hash_file_cancel(), GNUNET_DISK_file_copy(), GNUNET_DISK_fn_read(), GNUNET_DISK_fn_write(), GNUNET_DISK_pipe_close_end(), 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(), GNUNET_TIME_absolute_get_monotonic(), hostkeys_load(), load_etc_hosts(), lookup_dns_servers(), main(), 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(), server_log(), shutdown_pch(), shutdown_task(), start_process(), stop_broadcast(), store_valid_peers(), to_file_raw(), tofile_(), try_match_block(), unindex_finish(), unload_keys(), write_benchmark_data(), write_bw_gnuplot_script(), write_rtt_gnuplot_script(), and write_throughput_gnuplot_script().

1345 {
1346  int ret;
1347 
1348  if (h == NULL)
1349  {
1350  errno = EINVAL;
1351  return GNUNET_SYSERR;
1352  }
1353 
1354  ret = GNUNET_OK;
1355 
1356  if (close (h->fd) != 0)
1357  {
1359  ret = GNUNET_SYSERR;
1360  }
1361 
1362  GNUNET_free (h);
1363  return ret;
1364 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define LOG_STRERROR(kind, syscall)
Definition: disk.c:33
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
int fd
File handle on Unix-like systems.
#define GNUNET_free(ptr)
Wrapper around free.

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

References GNUNET_DISK_PipeHandle::fd, GNUNET_break, GNUNET_DISK_PIPE_END_READ, and GNUNET_DISK_PIPE_END_WRITE.

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

1754 {
1755  switch (n)
1756  {
1759  return p->fd[n];
1760 
1761  default:
1762  GNUNET_break (0);
1763  return NULL;
1764  }
1765 }
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_DISK_FileHandle * fd[2]
File descriptors for the pipe.
Definition: disk.c:74
The writing-end of a pipe.
The reading-end of a pipe.
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 argumets 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

If both argumets are GNUNET_NO, the file is made world-read-write-executable (777).

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

References GNUNET_ERROR_TYPE_WARNING, GNUNET_log_strerror_file, GNUNET_YES, and mode.

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

382 {
383  mode_t mode;
384 
385  if (GNUNET_YES == require_uid_match)
386  mode = S_IRUSR | S_IWUSR | S_IXUSR;
387  else if (GNUNET_YES == require_gid_match)
388  mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP;
389  else
390  mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH
391  | S_IWOTH | S_IXOTH;
392  if (0 != chmod (fn, mode))
394 }
static enum @8 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
static char * fn
Filename of the unique file.
#define GNUNET_YES
Definition: gnunet_common.h:85
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
Parameters
fnonative integer file descriptor
Returns
file handle corresponding to the descriptor, NULL on error

Definition at line 1374 of file disk.c.

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

1375 {
1376  struct GNUNET_DISK_FileHandle *fh;
1377 
1378  if ((((off_t) -1) == lseek (fno, 0, SEEK_CUR)) && (EBADF == errno))
1379  return NULL; /* invalid FD */
1380 
1381  fh = GNUNET_new (struct GNUNET_DISK_FileHandle);
1382 
1383  fh->fd = fno;
1384 
1385  return fh;
1386 }
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static int fh
Handle to the unique file.
int fd
File handle on Unix-like systems.
Handle used to access files (and pipes).
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

Get a handle from a native FD.

Parameters
fdnative streaming file descriptor
Returns
file handle corresponding to the descriptor

Definition at line 1396 of file disk.c.

References GNUNET_DISK_get_handle_from_int_fd().

Referenced by run(), and testing_main().

1397 {
1398  int fno;
1399 
1400  fno = fileno (fd);
1401  if (-1 == fno)
1402  return NULL;
1403 
1405 }
struct GNUNET_DISK_FileHandle * GNUNET_DISK_get_handle_from_int_fd(int fno)
Get a handle from a native integer FD.
Definition: disk.c:1374
int fd
File handle on Unix-like systems.
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 732 of file disk.c.

References GNUNET_DISK_FileHandle::fd, and GNUNET_SYSERR.

Referenced by child_death_task(), cmd_read(), database_setup(), decrementBit(), fh_reader(), file_hash_task(), GNUNET_BIO_read(), GNUNET_CONTAINER_bloomfilter_load(), GNUNET_CRYPTO_ecdsa_key_create_from_file(), GNUNET_CRYPTO_eddsa_key_create_from_file(), GNUNET_DISK_file_copy(), GNUNET_DISK_fn_read(), GNUNET_FS_data_reader_file_(), GNUNET_FS_handle_on_demand_block(), helper_read(), incrementBit(), main(), maint_child_death(), nat_server_read(), parent_control_handler(), print_key(), put_cb(), read_external_ipv4(), read_task(), REGEX_TEST_read_from_file(), restore_valid_peers(), run(), server_log(), shutdown_pipe_cb(), start_insert(), try_top_down_reconstruction(), and unindex_reader().

735 {
736  if (NULL == h)
737  {
738  errno = EINVAL;
739  return GNUNET_SYSERR;
740  }
741 
742  return read (h->fd, result, len);
743 }
static int result
Global testing status.
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
int fd
File handle on Unix-like systems.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
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 757 of file disk.c.

References GNUNET_DISK_FileHandle::fd, GNUNET_SYSERR, and ret.

760 {
761  if (NULL == h)
762  {
763  errno = EINVAL;
764  return GNUNET_SYSERR;
765  }
766 
767  int flags;
768  ssize_t ret;
769 
770  /* set to non-blocking, read, then set back */
771  flags = fcntl (h->fd, F_GETFL);
772  if (0 == (flags & O_NONBLOCK))
773  (void) fcntl (h->fd, F_SETFL, flags | O_NONBLOCK);
774  ret = read (h->fd, result, len);
775  if (0 == (flags & O_NONBLOCK))
776  {
777  int eno = errno;
778  (void) fcntl (h->fd, F_SETFL, flags);
779  errno = eno;
780  }
781  return ret;
782 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static int result
Global testing status.
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
int fd
File handle on Unix-like systems.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...

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

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, and ret.

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

795 {
796  struct GNUNET_DISK_FileHandle *fh;
797  ssize_t ret;
798  int eno;
799 
801  if (NULL == fh)
802  return GNUNET_SYSERR;
803  ret = GNUNET_DISK_file_read (fh, result, len);
804  eno = errno;
806  errno = eno;
807  return ret;
808 }
Open the file for reading.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1344
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:732
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Nobody is allowed to do anything to the file.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
static char * fn
Filename of the unique file.
static int result
Global testing status.
static int fh
Handle to the unique file.
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
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:1267
Handle used to access files (and pipes).
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
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 820 of file disk.c.

References GNUNET_DISK_FileHandle::fd, and GNUNET_SYSERR.

Referenced by create_hostkeys(), database_shutdown(), decrementBit(), do_shutdown(), get_cb(), GNUNET_ATS_solver_logging_write_to_disk(), GNUNET_ATS_TEST_logging_write_to_file(), GNUNET_BIO_flush(), GNUNET_CRYPTO_ecdsa_key_create_from_file(), GNUNET_CRYPTO_eddsa_key_create_from_file(), GNUNET_DISK_file_copy(), GNUNET_DISK_fn_write(), GNUNET_FRIENDS_write(), GNUNET_OS_process_kill(), GNUNET_TESTING_peer_configure(), GNUNET_TIME_absolute_get_monotonic(), handle_estimate(), helper_write(), incrementBit(), main(), make_empty_file(), process_result_with_request(), publicize_rm(), publish_fs_connect_complete_cb(), regex_found_handler(), sighandler_child_death(), sighandler_chld(), sighandler_shutdown(), start_dump(), stat_iterator(), stats_iterator(), store_and_free_entries(), store_peer_presistently_iterator(), to_file_raw(), tofile_(), try_match_block(), write_bw_gnuplot_script(), write_rtt_gnuplot_script(), write_task(), and write_throughput_gnuplot_script().

823 {
824  if (NULL == h)
825  {
826  errno = EINVAL;
827  return GNUNET_SYSERR;
828  }
829 
830  return write (h->fd, buffer, n);
831 }
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
int fd
File handle on Unix-like systems.
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 843 of file disk.c.

References GNUNET_DISK_FileHandle::fd, GNUNET_SYSERR, and ret.

Referenced by write_benchmark_data().

846 {
847  if (NULL == h)
848  {
849  errno = EINVAL;
850  return GNUNET_SYSERR;
851  }
852 
853 
854  int flags;
855  ssize_t ret;
856 
857  /* set to blocking, write, then set back */
858  flags = fcntl (h->fd, F_GETFL);
859  if (0 != (flags & O_NONBLOCK))
860  (void) fcntl (h->fd, F_SETFL, flags - O_NONBLOCK);
861  ret = write (h->fd, buffer, n);
862  if (0 == (flags & O_NONBLOCK))
863  (void) fcntl (h->fd, F_SETFL, flags);
864  return ret;
865 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
int fd
File handle on Unix-like systems.
Here is the caller graph for this function:

◆ GNUNET_DISK_fn_write()

ssize_t GNUNET_DISK_fn_write ( const char *  fn,
const void *  buffer,
size_t  n,
enum GNUNET_DISK_AccessPermissions  mode 
)

Write a buffer to a file.

If the file is longer than the given buffer size, it will be truncated.

Parameters
fnfile name
bufferthe data to write
nnumber of bytes to write
modefile permissions
Returns
number of bytes written on success, GNUNET_SYSERR on error

If the file is longer than the number of bytes that will be written, it will be truncated.

Parameters
fnfile name
bufferthe data to write
nnumber of bytes to write
modefile permissions
Returns
number of bytes written on success, GNUNET_SYSERR on error

Definition at line 879 of file disk.c.

References fh, GNUNET_assert, GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_file_write(), GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_OPEN_TRUNCATE, GNUNET_DISK_OPEN_WRITE, GNUNET_OK, GNUNET_SYSERR, and ret.

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

883 {
884  struct GNUNET_DISK_FileHandle *fh;
885  ssize_t ret;
886 
887  fh =
891  mode);
892  if (! fh)
893  return GNUNET_SYSERR;
894  ret = GNUNET_DISK_file_write (fh, buffer, n);
896  return ret;
897 }
Create file if it doesn&#39;t exist.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1344
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
static enum @8 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
static char * fn
Filename of the unique file.
Truncate file if it exists.
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:820
static int fh
Handle to the unique file.
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
Open the file for writing.
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:1267
Handle used to access files (and pipes).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_DISK_file_copy()

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

References buf, COPY_BLK_SIZE, FAIL, GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_file_read(), GNUNET_DISK_file_size(), GNUNET_DISK_file_write(), GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_OPEN_FAILIFEXISTS, GNUNET_DISK_OPEN_READ, GNUNET_DISK_OPEN_WRITE, GNUNET_DISK_PERM_GROUP_READ, GNUNET_DISK_PERM_GROUP_WRITE, GNUNET_DISK_PERM_NONE, GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log_strerror_file, GNUNET_malloc, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, len, and size.

1072 {
1073  char *buf;
1074  uint64_t pos;
1075  uint64_t size;
1076  size_t len;
1077  ssize_t sret;
1078  struct GNUNET_DISK_FileHandle *in;
1079  struct GNUNET_DISK_FileHandle *out;
1080 
1081  if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES, GNUNET_YES))
1082  {
1084  return GNUNET_SYSERR;
1085  }
1086  pos = 0;
1087  in =
1089  if (! in)
1090  {
1092  return GNUNET_SYSERR;
1093  }
1094  out =
1095  GNUNET_DISK_file_open (dst,
1102  if (! out)
1103  {
1106  return GNUNET_SYSERR;
1107  }
1108  buf = GNUNET_malloc (COPY_BLK_SIZE);
1109  while (pos < size)
1110  {
1111  len = COPY_BLK_SIZE;
1112  if (len > size - pos)
1113  len = size - pos;
1114  sret = GNUNET_DISK_file_read (in, buf, len);
1115  if ((sret < 0) || (len != (size_t) sret))
1116  goto FAIL;
1117  sret = GNUNET_DISK_file_write (out, buf, len);
1118  if ((sret < 0) || (len != (size_t) sret))
1119  goto FAIL;
1120  pos += len;
1121  }
1122  GNUNET_free (buf);
1124  GNUNET_DISK_file_close (out);
1125  return GNUNET_OK;
1126 FAIL:
1127  GNUNET_free (buf);
1129  GNUNET_DISK_file_close (out);
1130  return GNUNET_SYSERR;
1131 }
Open the file for reading.
Create file if it doesn&#39;t exist.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1344
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:732
Nobody is allowed to do anything to the file.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
#define COPY_BLK_SIZE
Block size for IO for copying files.
Definition: disk.c:42
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:820
static char buf[2048]
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
static unsigned int size
Size of the "table".
Definition: peer.c:67
Open the file for writing.
int 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:257
#define GNUNET_YES
Definition: gnunet_common.h:85
Fail if file already exists.
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:1267
Handle used to access files (and pipes).
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:

◆ 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
Parameters
dir_namethe name of the directory
callbackthe method to call for each file, can be NULL, in that case, we only count
callback_clsclosure for callback
Returns
the number of files found, GNUNET_SYSERR on error or ieration aborted by callback returning GNUNET_SYSERR

Definition at line 911 of file disk.c.

References _, 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(), getSizeRec(), GNUNET_CONFIGURATION_load_from(), GNUNET_DISK_directory_remove(), GNUNET_PLUGIN_load_all(), iter_testbed_path(), preprocess_file(), profiler_eval(), run(), and scan().

914 {
915  DIR *dinfo;
916  struct dirent *finfo;
917  struct stat istat;
918  int count = 0;
919  int ret;
920  char *name;
921  char *dname;
922  unsigned int name_len;
923  unsigned int n_size;
924 
925  GNUNET_assert (NULL != dir_name);
927  if (NULL == dname)
928  return GNUNET_SYSERR;
929  while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
930  dname[strlen (dname) - 1] = '\0';
931  if (0 != stat (dname, &istat))
932  {
934  GNUNET_free (dname);
935  return GNUNET_SYSERR;
936  }
937  if (! S_ISDIR (istat.st_mode))
938  {
940  _ ("Expected `%s' to be a directory!\n"),
941  dir_name);
942  GNUNET_free (dname);
943  return GNUNET_SYSERR;
944  }
945  errno = 0;
946  dinfo = opendir (dname);
947  if ((EACCES == errno) || (NULL == dinfo))
948  {
949  LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
950  if (NULL != dinfo)
951  closedir (dinfo);
952  GNUNET_free (dname);
953  return GNUNET_SYSERR;
954  }
955  name_len = 256;
956  n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
957  name = GNUNET_malloc (n_size);
958  while (NULL != (finfo = readdir (dinfo)))
959  {
960  if ((0 == strcmp (finfo->d_name, ".")) ||
961  (0 == strcmp (finfo->d_name, "..")))
962  continue;
963  if (NULL != callback)
964  {
965  if (name_len < strlen (finfo->d_name))
966  {
967  GNUNET_free (name);
968  name_len = strlen (finfo->d_name);
969  n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
970  name = GNUNET_malloc (n_size);
971  }
972  /* dname can end in "/" only if dname == "/";
973  * if dname does not end in "/", we need to add
974  * a "/" (otherwise, we must not!) */
975  GNUNET_snprintf (name,
976  n_size,
977  "%s%s%s",
978  dname,
979  (0 == strcmp (dname, DIR_SEPARATOR_STR))
980  ? ""
982  finfo->d_name);
983  ret = callback (callback_cls, name);
984  if (GNUNET_OK != ret)
985  {
986  closedir (dinfo);
987  GNUNET_free (name);
988  GNUNET_free (dname);
989  if (GNUNET_NO == ret)
990  return count;
991  return GNUNET_SYSERR;
992  }
993  }
994  count++;
995  }
996  closedir (dinfo);
997  GNUNET_free (name);
998  GNUNET_free (dname);
999  return count;
1000 }
static const char * dir_name
Top-level directory we monitor to auto-publish.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_NO
Definition: gnunet_common.h:86
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define _(String)
GNU gettext support macro.
Definition: platform.h:180
#define DIR_SEPARATOR_STR
Definition: platform.h:167
#define LOG(kind,...)
Definition: disk.c:31
#define DIR_SEPARATOR
Definition: platform.h:166
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition: strings.c:617
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:36
const char * name
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_DISK_directory_create_for_file()

int 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
Parameters
filenamename of a file in the directory
Returns
GNUNET_OK on success, GNUNET_SYSERR on failure, GNUNET_NO if the directory exists but is not writeable for us

Definition at line 684 of file disk.c.

References DIR_SEPARATOR, GNUNET_DISK_directory_create(), GNUNET_free, GNUNET_NO, GNUNET_OK, GNUNET_strdup, GNUNET_STRINGS_filename_expand(), GNUNET_SYSERR, len, and ret.

Referenced by create_hostkeys(), database_setup(), get_server_addresses(), GNUNET_ATS_solver_logging_write_to_disk(), GNUNET_CONFIGURATION_write(), GNUNET_CRYPTO_ecdsa_key_create_from_file(), GNUNET_CRYPTO_eddsa_key_create_from_file(), GNUNET_DISK_file_open(), GNUNET_FRIENDS_parse(), GNUNET_FRIENDS_write_start(), GNUNET_FS_download_sync_(), handle_create_message(), 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().

685 {
686  char *rdir;
687  size_t len;
688  int ret;
689  int eno;
690 
692  if (NULL == rdir)
693  {
694  errno = EINVAL;
695  return GNUNET_SYSERR;
696  }
697  if (0 == access (rdir, W_OK))
698  {
699  GNUNET_free (rdir);
700  return GNUNET_OK;
701  }
702 
703  len = strlen (rdir);
704  while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
705  len--;
706  rdir[len] = '\0';
707  /* The empty path is invalid and in this case refers to / */
708  if (0 == len)
709  {
710  GNUNET_free (rdir);
711  rdir = GNUNET_strdup ("/");
712  }
713  ret = GNUNET_DISK_directory_create (rdir);
714  if ((GNUNET_OK == ret) && (0 != access (rdir, W_OK)))
715  ret = GNUNET_NO;
716  eno = errno;
717  GNUNET_free (rdir);
718  errno = eno;
719  return ret;
720 }
int GNUNET_DISK_directory_create(const char *dir)
Implementation of "mkdir -p".
Definition: disk.c:589
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_NO
Definition: gnunet_common.h:86
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static char * filename
#define DIR_SEPARATOR
Definition: platform.h:166
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition: strings.c:617
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_DISK_directory_test()

int 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

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 stat'ed

Definition at line 503 of file disk.c.

References GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_NO, GNUNET_SYSERR, GNUNET_YES, LOG, LOG_STRERROR_FILE, and ret.

Referenced by deserialization_master(), deserialize_download(), deserialize_search(), GNUNET_DISK_directory_create(), GNUNET_DISK_directory_remove(), GNUNET_FS_remove_sync_dir_(), GNUNET_OS_installation_get_path(), and run().

504 {
505  struct stat filestat;
506  int ret;
507 
508  ret = stat (fil, &filestat);
509  if (ret != 0)
510  {
511  if (errno != ENOENT)
513  return GNUNET_SYSERR;
514  }
515  if (! S_ISDIR (filestat.st_mode))
516  {
518  "A file already exits with the same name %s\n",
519  fil);
520  return GNUNET_NO;
521  }
522  if (GNUNET_YES == is_readable)
523  ret = access (fil, R_OK | X_OK);
524  else
525  ret = access (fil, X_OK);
526  if (ret < 0)
527  {
529  return GNUNET_NO;
530  }
531  return GNUNET_YES;
532 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_NO
Definition: gnunet_common.h:86
#define LOG(kind,...)
Definition: disk.c:31
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:36
#define GNUNET_YES
Definition: gnunet_common.h:85
Here is the caller graph for this function:

◆ GNUNET_DISK_directory_remove()

int 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

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

References GNUNET_break, GNUNET_DISK_directory_scan(), GNUNET_DISK_directory_test(), GNUNET_ERROR_TYPE_WARNING, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, LOG_STRERROR_FILE, and remove_helper().

Referenced by access_handler_callback(), delete_files(), GNUNET_FS_remove_sync_dir_(), GNUNET_TESTING_system_destroy(), publish_timeout(), purge_cfg_dir(), remove_helper(), report_uri(), and run().

1029 {
1030  struct stat istat;
1031 
1032  if (NULL == filename)
1033  {
1034  GNUNET_break (0);
1035  return GNUNET_SYSERR;
1036  }
1037  if (0 != lstat (filename, &istat))
1038  return GNUNET_NO; /* file may not exist... */
1039  (void) chmod (filename, S_IWUSR | S_IRUSR | S_IXUSR);
1040  if (0 == unlink (filename))
1041  return GNUNET_OK;
1042  if ((errno != EISDIR) &&
1043  /* EISDIR is not sufficient in all cases, e.g.
1044  * sticky /tmp directory may result in EPERM on BSD.
1045  * So we also explicitly check "isDirectory" */
1047  {
1049  return GNUNET_SYSERR;
1050  }
1051  if (GNUNET_SYSERR ==
1053  return GNUNET_SYSERR;
1054  if (0 != rmdir (filename))
1055  {
1057  return GNUNET_SYSERR;
1058  }
1059  return GNUNET_OK;
1060 }
int GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition: disk.c:503
#define GNUNET_NO
Definition: gnunet_common.h:86
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition: disk.c:911
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static int remove_helper(void *unused, const char *fn)
Function that removes the given directory by calling GNUNET_DISK_directory_remove().
Definition: disk.c:1012
static char * filename
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:36
#define GNUNET_YES
Definition: gnunet_common.h:85
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 1830 of file disk.c.

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

1831 {
1834  &purge_cfg_dir,
1835  (void *) option));
1836 }
static char * cfg_filename
Name of the configuration file.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static char * option
Name of the option.
Definition: gnunet-config.c:38
static int purge_cfg_dir(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
Helper function for GNUNET_DISK_purge_cfg_dir.
Definition: disk.c:1800
int 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...
Here is the call graph for this function:

◆ GNUNET_DISK_directory_create()

int GNUNET_DISK_directory_create ( const char *  dir)

Implementation of "mkdir -p".

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

Definition at line 589 of file disk.c.

References DIR_SEPARATOR, GNUNET_break, GNUNET_DISK_directory_test(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_STRINGS_filename_expand(), GNUNET_SYSERR, GNUNET_YES, len, LOG_STRERROR_FILE, and ret.

Referenced by cron_scan_directory_data_hosts(), ensure_folder_exist(), GNUNET_DISK_directory_create_for_file(), run(), write_benchmark_data(), and write_pid_file().

590 {
591  char *rdir;
592  unsigned int len;
593  unsigned int pos;
594  unsigned int pos2;
595  int ret = GNUNET_OK;
596 
598  if (rdir == NULL)
599  {
600  GNUNET_break (0);
601  return GNUNET_SYSERR;
602  }
603 
604  len = strlen (rdir);
605 
606  pos = 1; /* skip heading '/' */
607 
608  /* Check which low level directories already exist */
609  pos2 = len;
610  rdir[len] = DIR_SEPARATOR;
611  while (pos <= pos2)
612  {
613  if (DIR_SEPARATOR == rdir[pos2])
614  {
615  rdir[pos2] = '\0';
617  if (GNUNET_NO == ret)
618  {
620  "Creating directory `%s' failed",
621  rdir);
622  GNUNET_free (rdir);
623  return GNUNET_SYSERR;
624  }
625  rdir[pos2] = DIR_SEPARATOR;
626  if (GNUNET_YES == ret)
627  {
628  pos2++;
629  break;
630  }
631  }
632  pos2--;
633  }
634  rdir[len] = '\0';
635  if (pos < pos2)
636  pos = pos2;
637  /* Start creating directories */
638  while (pos <= len)
639  {
640  if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
641  {
642  rdir[pos] = '\0';
644  if (GNUNET_NO == ret)
645  {
647  "Creating directory `%s' failed",
648  rdir);
649  GNUNET_free (rdir);
650  return GNUNET_SYSERR;
651  }
652  if (GNUNET_SYSERR == ret)
653  {
654  ret = mkdir (rdir,
655  S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH
656  | S_IXOTH); /* 755 */
657 
658  if ((ret != 0) && (errno != EEXIST))
659  {
661  GNUNET_free (rdir);
662  return GNUNET_SYSERR;
663  }
664  }
665  rdir[pos] = DIR_SEPARATOR;
666  }
667  pos++;
668  }
669  GNUNET_free (rdir);
670  return GNUNET_OK;
671 }
static char * dir
Set to the directory where runtime files are stored.
Definition: gnunet-arm.c:89
int GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition: disk.c:503
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_NO
Definition: gnunet_common.h:86
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define DIR_SEPARATOR
Definition: platform.h:166
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition: strings.c:617
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:36
#define GNUNET_log(kind,...)
#define GNUNET_YES
Definition: gnunet_common.h:85
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_DISK_file_lock()

int GNUNET_DISK_file_lock ( struct GNUNET_DISK_FileHandle fh,
off_t  lock_start,
off_t  lock_end,
int  excl 
)

Lock a part of a file.

Parameters
fhfile handle
lock_startabsolute position from where to lock
lock_endabsolute position until where to lock
exclGNUNET_YES for an exclusive lock
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 1200 of file disk.c.

References GNUNET_DISK_FileHandle::fd, GNUNET_OK, and GNUNET_SYSERR.

Referenced by GNUNET_CRYPTO_ecdsa_key_create_from_file(), and GNUNET_CRYPTO_eddsa_key_create_from_file().

1204 {
1205  if (fh == NULL)
1206  {
1207  errno = EINVAL;
1208  return GNUNET_SYSERR;
1209  }
1210 
1211  struct flock fl;
1212 
1213  memset (&fl, 0, sizeof(struct flock));
1214  fl.l_type = excl ? F_WRLCK : F_RDLCK;
1215  fl.l_whence = SEEK_SET;
1216  fl.l_start = lock_start;
1217  fl.l_len = lock_end;
1218 
1219  return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1220 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
int fd
File handle on Unix-like systems.
Here is the caller graph for this function:

◆ GNUNET_DISK_file_unlock()

int GNUNET_DISK_file_unlock ( struct GNUNET_DISK_FileHandle fh,
off_t  unlock_start,
off_t  unlock_end 
)

Unlock a part of a file.

Parameters
fhfile handle
unlock_startabsolute position from where to unlock
unlock_endabsolute position until where to unlock
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 1232 of file disk.c.

References GNUNET_DISK_FileHandle::fd, GNUNET_OK, and GNUNET_SYSERR.

Referenced by GNUNET_CRYPTO_ecdsa_key_create_from_file(), and GNUNET_CRYPTO_eddsa_key_create_from_file().

1235 {
1236  if (fh == NULL)
1237  {
1238  errno = EINVAL;
1239  return GNUNET_SYSERR;
1240  }
1241 
1242  struct flock fl;
1243 
1244  memset (&fl, 0, sizeof(struct flock));
1245  fl.l_type = F_UNLCK;
1246  fl.l_whence = SEEK_SET;
1247  fl.l_start = unlock_start;
1248  fl.l_len = unlock_end;
1249 
1250  return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1251 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
int fd
File handle on Unix-like systems.
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 1139 of file disk.c.

Referenced by progress_cb().

1140 {
1141  char *idx;
1142  char c;
1143 
1144  for (idx = fn; *idx; idx++)
1145  {
1146  c = *idx;
1147 
1148  if ((c == '/') || (c == '\\') || (c == ':') || (c == '*') || (c == '?') ||
1149  (c ==
1150  '"')
1151  ||
1152  (c == '<') || (c == '>') || (c == '|') )
1153  {
1154  *idx = '_';
1155  }
1156  }
1157 }
static char * fn
Filename of the unique file.
Here is the caller graph for this function:

◆ GNUNET_DISK_file_change_owner()

int 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
Parameters
filenamename of file to change the owner of
username of the new owner
Returns
GNUNET_OK on success, GNUNET_SYSERR on failure

Definition at line 1168 of file disk.c.

References _, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_OK, GNUNET_SYSERR, LOG, and LOG_STRERROR_FILE.

Referenced by write_pid_file().

1169 {
1170  struct passwd *pws;
1171 
1172  pws = getpwnam (user);
1173  if (NULL == pws)
1174  {
1176  _ ("Cannot obtain information about user `%s': %s\n"),
1177  user,
1178  strerror (errno));
1179  return GNUNET_SYSERR;
1180  }
1181  if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
1182  {
1184  return GNUNET_SYSERR;
1185  }
1186  return GNUNET_OK;
1187 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define _(String)
GNU gettext support macro.
Definition: platform.h:180
static char * filename
#define LOG(kind,...)
Definition: disk.c:31
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: disk.c:36
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
Parameters
hopen file handle
mhandle to the new mapping
accessaccess specification, GNUNET_DISK_MAP_TYPE_xxx
lensize of the mapping
Returns
pointer to the mapped memory region, NULL on failure

Definition at line 1439 of file disk.c.

References GNUNET_DISK_FileHandle::fd, GNUNET_assert, GNUNET_DISK_MAP_TYPE_READ, GNUNET_DISK_MAP_TYPE_WRITE, GNUNET_free, GNUNET_new, len, and MAP_FAILED.

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

1443 {
1444  if (NULL == h)
1445  {
1446  errno = EINVAL;
1447  return NULL;
1448  }
1449 
1450  int prot;
1451 
1452  prot = 0;
1453  if (access & GNUNET_DISK_MAP_TYPE_READ)
1454  prot = PROT_READ;
1455  if (access & GNUNET_DISK_MAP_TYPE_WRITE)
1456  prot |= PROT_WRITE;
1457  *m = GNUNET_new (struct GNUNET_DISK_MapHandle);
1458  (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1459  GNUNET_assert (NULL != (*m)->addr);
1460  if (MAP_FAILED == (*m)->addr)
1461  {
1462  GNUNET_free (*m);
1463  return NULL;
1464  }
1465  (*m)->len = len;
1466  return (*m)->addr;
1467 }
#define MAP_FAILED
Definition: disk.c:1426
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Read-only memory map.
Write-able memory map.
int fd
File handle on Unix-like systems.
Handle for a memory-mapping operation.
Definition: disk.c:1411
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the caller graph for this function:

◆ GNUNET_DISK_file_unmap()

int GNUNET_DISK_file_unmap ( struct GNUNET_DISK_MapHandle h)

Unmap a file.

Parameters
hmapping handle
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise
Parameters
hmapping handle
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1476 of file disk.c.

References GNUNET_DISK_MapHandle::addr, GNUNET_free, GNUNET_OK, GNUNET_SYSERR, GNUNET_DISK_MapHandle::len, and ret.

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

1477 {
1478  int ret;
1479 
1480  if (h == NULL)
1481  {
1482  errno = EINVAL;
1483  return GNUNET_SYSERR;
1484  }
1485 
1486  ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1487 
1488  GNUNET_free (h);
1489  return ret;
1490 }
void * addr
Address where the map is in memory.
Definition: disk.c:1416
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
size_t len
Number of bytes mapped.
Definition: disk.c:1421
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ GNUNET_DISK_file_sync()

int 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
Parameters
hhandle to an open file
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1499 of file disk.c.

References GNUNET_DISK_FileHandle::fd, GNUNET_OK, and GNUNET_SYSERR.

Referenced by GNUNET_CRYPTO_ecdsa_key_create_from_file(), GNUNET_CRYPTO_eddsa_key_create_from_file(), and publicize_rm().

1500 {
1501  if (h == NULL)
1502  {
1503  errno = EINVAL;
1504  return GNUNET_SYSERR;
1505  }
1506 
1507 #if ! defined(__linux__) || ! defined(GNU)
1508  return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1509 #else
1510  return fdatasync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1511 #endif
1512 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
int fd
File handle on Unix-like systems.
Here is the caller graph for this function: