RTS API Documentation  1.10.11
Modules | Data Structures | Typedefs | Functions
File I/O Handling Functions
+ Collaboration diagram for File I/O Handling Functions:

Modules

 File Seek Flags
 
 File Permissions flags
 
 File Lock Types
 
 File Open Flags/Routines
 

Data Structures

struct  switch_array_header_t
 

Typedefs

typedef struct fspr_file_t switch_file_t
 
typedef int32_t switch_fileperms_t
 
typedef int switch_seek_where_t
 
typedef struct switch_dir switch_dir_t
 
typedef struct switch_array_header_t switch_array_header_t
 

Functions

switch_status_t switch_file_open (switch_file_t **newf, const char *fname, int32_t flag, switch_fileperms_t perm, switch_memory_pool_t *pool)
 
switch_status_t switch_file_seek (switch_file_t *thefile, switch_seek_where_t where, int64_t *offset)
 
switch_status_t switch_file_copy (const char *from_path, const char *to_path, switch_fileperms_t perms, switch_memory_pool_t *pool)
 
switch_status_t switch_file_close (switch_file_t *thefile)
 
switch_status_t switch_file_trunc (switch_file_t *thefile, int64_t offset)
 
switch_status_t switch_file_lock (switch_file_t *thefile, int type)
 
switch_status_t switch_file_remove (const char *path, switch_memory_pool_t *pool)
 
switch_status_t switch_file_rename (const char *from_path, const char *to_path, switch_memory_pool_t *pool)
 
switch_status_t switch_file_read (switch_file_t *thefile, void *buf, switch_size_t *nbytes)
 
switch_status_t switch_file_write (switch_file_t *thefile, const void *buf, switch_size_t *nbytes)
 
int switch_file_printf (switch_file_t *thefile, const char *format,...)
 
switch_status_t switch_file_mktemp (switch_file_t **thefile, char *templ, int32_t flags, switch_memory_pool_t *pool)
 
switch_size_t switch_file_get_size (switch_file_t *thefile)
 
switch_status_t switch_file_exists (const char *filename, switch_memory_pool_t *pool)
 
switch_status_t switch_directory_exists (const char *dirname, switch_memory_pool_t *pool)
 
switch_status_t switch_dir_make (const char *path, switch_fileperms_t perm, switch_memory_pool_t *pool)
 
switch_status_t switch_dir_make_recursive (const char *path, switch_fileperms_t perm, switch_memory_pool_t *pool)
 
switch_status_t switch_dir_open (switch_dir_t **new_dir, const char *dirname, switch_memory_pool_t *pool)
 
switch_status_t switch_dir_close (switch_dir_t *thedir)
 
const char * switch_dir_next_file (switch_dir_t *thedir, char *buf, switch_size_t len)
 
uint32_t switch_dir_count (switch_dir_t *thedir)
 

Detailed Description

Typedef Documentation

◆ switch_array_header_t

Definition at line 925 of file switch_apr.h.

◆ switch_dir_t

typedef struct switch_dir switch_dir_t

Definition at line 911 of file switch_apr.h.

◆ switch_file_t

typedef struct fspr_file_t switch_file_t

Structure for referencing files.

Definition at line 685 of file switch_apr.h.

◆ switch_fileperms_t

typedef int32_t switch_fileperms_t

Definition at line 687 of file switch_apr.h.

◆ switch_seek_where_t

typedef int switch_seek_where_t

Definition at line 688 of file switch_apr.h.

Function Documentation

◆ switch_dir_close()

switch_status_t switch_dir_close ( switch_dir_t thedir)

Definition at line 582 of file switch_apr.c.

References switch_dir::dir_handle.

Referenced by switch_loadable_module_enumerate_available().

583 {
584  switch_status_t status = fspr_dir_close(thedir->dir_handle);
585 
586  free(thedir);
587  return status;
588 }
switch_status_t
Common return values.
fspr_dir_t * dir_handle
Definition: switch_apr.c:557

◆ switch_dir_count()

uint32_t switch_dir_count ( switch_dir_t thedir)

Definition at line 590 of file switch_apr.c.

References count, switch_dir::dir_handle, switch_dir::finfo, name, and SWITCH_STATUS_SUCCESS.

591 {
592  const char *name;
593  fspr_int32_t finfo_flags = APR_FINFO_DIRENT | APR_FINFO_TYPE | APR_FINFO_NAME;
594  uint32_t count = 0;
595 
596  fspr_dir_rewind(thedir->dir_handle);
597 
598  while (fspr_dir_read(&(thedir->finfo), finfo_flags, thedir->dir_handle) == SWITCH_STATUS_SUCCESS) {
599 
600  if (thedir->finfo.filetype != APR_REG && thedir->finfo.filetype != APR_LNK) {
601  continue;
602  }
603 
604  if (!(name = thedir->finfo.fname)) {
605  name = thedir->finfo.name;
606  }
607 
608  if (name) {
609  count++;
610  }
611  }
612 
613  fspr_dir_rewind(thedir->dir_handle);
614 
615  return count;
616 }
int count
Definition: switch_cJSON.h:204
const char *const name
Definition: switch_cJSON.h:250
fspr_dir_t * dir_handle
Definition: switch_apr.c:557
fspr_finfo_t finfo
Definition: switch_apr.c:558

◆ switch_dir_make()

switch_status_t switch_dir_make ( const char *  path,
switch_fileperms_t  perm,
switch_memory_pool_t pool 
)

Create a new directory on the file system.

Parameters
paththe path for the directory to be created. (use / on all systems)
permPermissions for the new direcoty.
poolthe pool to use.

Definition at line 546 of file switch_apr.c.

547 {
548  return fspr_dir_make(path, perm, pool);
549 }
switch_memory_pool_t * pool
const char *const const char *const path

◆ switch_dir_make_recursive()

switch_status_t switch_dir_make_recursive ( const char *  path,
switch_fileperms_t  perm,
switch_memory_pool_t pool 
)

Creates a new directory on the file system, but behaves like 'mkdir -p'. Creates intermediate directories as required. No error will be reported if PATH already exists.

Parameters
paththe path for the directory to be created. (use / on all systems)
permPermissions for the new direcoty.
poolthe pool to use.

Definition at line 551 of file switch_apr.c.

Referenced by main(), switch_core_init(), and switch_ivr_record_session_event().

552 {
553  return fspr_dir_make_recursive(path, perm, pool);
554 }
switch_memory_pool_t * pool
const char *const const char *const path

◆ switch_dir_next_file()

const char* switch_dir_next_file ( switch_dir_t thedir,
char *  buf,
switch_size_t  len 
)

Definition at line 618 of file switch_apr.c.

References buf, switch_dir::dir_handle, switch_dir::finfo, name, switch_copy_string(), and SWITCH_STATUS_SUCCESS.

Referenced by switch_loadable_module_enumerate_available().

619 {
620  const char *fname = NULL;
621  fspr_int32_t finfo_flags = APR_FINFO_DIRENT | APR_FINFO_TYPE | APR_FINFO_NAME;
622  const char *name;
623 
624  while (fspr_dir_read(&(thedir->finfo), finfo_flags, thedir->dir_handle) == SWITCH_STATUS_SUCCESS) {
625 
626  if (thedir->finfo.filetype != APR_REG && thedir->finfo.filetype != APR_LNK) {
627  continue;
628  }
629 
630  if (!(name = thedir->finfo.fname)) {
631  name = thedir->finfo.name;
632  }
633 
634  if (name) {
635  switch_copy_string(buf, name, len);
636  fname = buf;
637  break;
638  } else {
639  continue;
640  }
641  }
642  return fname;
643 }
char * switch_copy_string(char *dst, const char *src, switch_size_t dst_size)
Definition: switch_apr.c:222
switch_byte_t switch_byte_t * buf
const char *const name
Definition: switch_cJSON.h:250
fspr_dir_t * dir_handle
Definition: switch_apr.c:557
fspr_finfo_t finfo
Definition: switch_apr.c:558

◆ switch_dir_open()

switch_status_t switch_dir_open ( switch_dir_t **  new_dir,
const char *  dirname,
switch_memory_pool_t pool 
)

Definition at line 561 of file switch_apr.c.

References switch_dir::dir_handle, memset(), and SWITCH_STATUS_FALSE.

Referenced by switch_loadable_module_enumerate_available().

562 {
563  switch_status_t status;
564  switch_dir_t *dir = malloc(sizeof(*dir));
565 
566  if (!dir) {
567  *new_dir = NULL;
568  return SWITCH_STATUS_FALSE;
569  }
570 
571  memset(dir, 0, sizeof(*dir));
572  if ((status = fspr_dir_open(&(dir->dir_handle), dirname, pool)) == APR_SUCCESS) {
573  *new_dir = dir;
574  } else {
575  free(dir);
576  *new_dir = NULL;
577  }
578 
579  return status;
580 }
switch_memory_pool_t * pool
switch_status_t
Common return values.
fspr_dir_t * dir_handle
Definition: switch_apr.c:557
memset(buf, 0, buflen)

◆ switch_directory_exists()

switch_status_t switch_directory_exists ( const char *  dirname,
switch_memory_pool_t pool 
)

Definition at line 497 of file switch_apr.c.

References switch_core_destroy_memory_pool, and switch_core_new_memory_pool.

Referenced by switch_core_perform_file_open().

498 {
499  fspr_dir_t *dir_handle;
500  switch_memory_pool_t *our_pool = NULL;
501  switch_status_t status;
502 
503  if (!pool) {
504  switch_core_new_memory_pool(&our_pool);
505  pool = our_pool;
506  }
507 
508  if ((status = fspr_dir_open(&dir_handle, dirname, pool)) == APR_SUCCESS) {
509  fspr_dir_close(dir_handle);
510  }
511 
512  if (our_pool) {
514  }
515 
516  return status;
517 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:633
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:642
switch_memory_pool_t * pool
switch_status_t
Common return values.
struct fspr_pool_t switch_memory_pool_t

◆ switch_file_close()

switch_status_t switch_file_close ( switch_file_t thefile)

Close the specified file.

Parameters
thefileThe file descriptor to close.

Definition at line 432 of file switch_apr.c.

Referenced by get_file_size(), main(), and SWITCH_STANDARD_APP().

433 {
434  return fspr_file_close(thefile);
435 }

◆ switch_file_copy()

switch_status_t switch_file_copy ( const char *  from_path,
const char *  to_path,
switch_fileperms_t  perms,
switch_memory_pool_t pool 
)

Definition at line 426 of file switch_apr.c.

427 {
428  return fspr_file_copy(from_path, to_path, perms, pool);
429 }
switch_memory_pool_t * pool

◆ switch_file_exists()

switch_status_t switch_file_exists ( const char *  filename,
switch_memory_pool_t pool 
)

Definition at line 519 of file switch_apr.c.

References switch_core_destroy_memory_pool, switch_core_new_memory_pool, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and zstr.

Referenced by load_config(), msrp_init_ssl(), switch_core_cert_gen_fingerprint(), switch_core_check_dtls_pem(), switch_core_gen_certs(), switch_core_perform_file_open(), switch_img_txt_handle_create(), and switch_rtp_add_dtls().

520 {
521  int32_t wanted = APR_FINFO_TYPE;
522  switch_memory_pool_t *our_pool = NULL;
524  fspr_finfo_t info = { 0 };
525 
526  if (zstr(filename)) {
527  return status;
528  }
529 
530  if (!pool) {
531  switch_core_new_memory_pool(&our_pool);
532  }
533 
534  fspr_stat(&info, filename, wanted, pool ? pool : our_pool);
535  if (info.filetype != APR_NOFILE) {
536  status = SWITCH_STATUS_SUCCESS;
537  }
538 
539  if (our_pool) {
541  }
542 
543  return status;
544 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:633
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:642
switch_memory_pool_t * pool
#define zstr(x)
Definition: switch_utils.h:314
switch_status_t
Common return values.
const char * filename
struct fspr_pool_t switch_memory_pool_t

◆ switch_file_get_size()

switch_size_t switch_file_get_size ( switch_file_t thefile)

Definition at line 491 of file switch_apr.c.

References SWITCH_STATUS_SUCCESS.

Referenced by get_file_size(), and SWITCH_STANDARD_APP().

492 {
493  struct fspr_finfo_t finfo;
494  return fspr_file_info_get(&finfo, APR_FINFO_SIZE, thefile) == SWITCH_STATUS_SUCCESS ? (switch_size_t) finfo.size : 0;
495 }
uintptr_t switch_size_t

◆ switch_file_lock()

switch_status_t switch_file_lock ( switch_file_t thefile,
int  type 
)

Definition at line 442 of file switch_apr.c.

Referenced by main().

443 {
444  return fspr_file_lock(thefile, type);
445 }

◆ switch_file_mktemp()

switch_status_t switch_file_mktemp ( switch_file_t **  thefile,
char *  templ,
int32_t  flags,
switch_memory_pool_t pool 
)

Definition at line 486 of file switch_apr.c.

487 {
488  return fspr_file_mktemp(thefile, templ, flags, pool);
489 }
switch_memory_pool_t * pool

◆ switch_file_open()

switch_status_t switch_file_open ( switch_file_t **  newf,
const char *  fname,
int32_t  flag,
switch_fileperms_t  perm,
switch_memory_pool_t pool 
)

Open the specified file.

Parameters
newfThe opened file descriptor.
fnameThe full path to the file (using / on all systems)
flagOr'ed value of:
        SWITCH_FOPEN_READ                               open for reading
        SWITCH_FOPEN_WRITE                              open for writing
        SWITCH_FOPEN_CREATE                             create the file if not there
        SWITCH_FOPEN_APPEND                             file ptr is set to end prior to all writes
        SWITCH_FOPEN_TRUNCATE                   set length to zero if file exists
        SWITCH_FOPEN_BINARY                             not a text file (This flag is ignored on
                                                                                UNIX because it has no meaning)
        SWITCH_FOPEN_BUFFERED                   buffer the data.  Default is non-buffered
        SWITCH_FOPEN_EXCL                               return error if APR_CREATE and file exists
        SWITCH_FOPEN_DELONCLOSE                 delete the file after closing.
        SWITCH_FOPEN_XTHREAD                            Platform dependent tag to open the file
                                                                                for use across multiple threads
        SWITCH_FOPEN_SHARELOCK                  Platform dependent support for higher
                                                                                level locked read/write access to support
                                                                                writes across process/machines
        SWITCH_FOPEN_NOCLEANUP                  Do not register a cleanup with the pool
                                                                                passed in on the pool argument (see below).
                                                                                The fspr_os_file_t handle in fspr_file_t will not
                                                                                be closed when the pool is destroyed.
        SWITCH_FOPEN_SENDFILE_ENABLED   Open with appropriate platform semantics
                                                                                for sendfile operations.  Advisory only,
                                                                                fspr_socket_sendfile does not check this flag.
permAccess permissions for file.
poolThe pool to use.
Remarks
If perm is SWITCH_FPROT_OS_DEFAULT and the file is being created, appropriate default permissions will be used.

Definition at line 411 of file switch_apr.c.

Referenced by get_file_size(), main(), and SWITCH_STANDARD_APP().

413 {
414  return fspr_file_open(newf, fname, flag, perm, pool);
415 }
switch_memory_pool_t * pool

◆ switch_file_printf()

int switch_file_printf ( switch_file_t thefile,
const char *  format,
  ... 
)

Definition at line 467 of file switch_apr.c.

References switch_file_write(), and switch_vasprintf().

468 {
469  va_list ap;
470  int ret;
471  char *data;
472 
473  va_start(ap, format);
474 
475  if ((ret = switch_vasprintf(&data, format, ap)) != -1) {
476  switch_size_t bytes = strlen(data);
477  switch_file_write(thefile, data, &bytes);
478  free(data);
479  }
480 
481  va_end(ap);
482 
483  return ret;
484 }
switch_status_t switch_file_write(switch_file_t *thefile, const void *buf, switch_size_t *nbytes)
Definition: switch_apr.c:462
char const int const cJSON_bool format
Definition: switch_cJSON.h:153
uintptr_t switch_size_t
int switch_vasprintf(char **ret, const char *fmt, va_list ap)
Definition: switch_apr.c:1290

◆ switch_file_read()

switch_status_t switch_file_read ( switch_file_t thefile,
void *  buf,
switch_size_t nbytes 
)

Read data from the specified file.

Parameters
thefileThe file descriptor to read from.
bufThe buffer to store the data to.
nbytesOn entry, the number of bytes to read; on exit, the number of bytes read.
Remarks
fspr_file_read will read up to the specified number of bytes, but never more. If there isn't enough data to fill that number of bytes, all of the available data is read. The third argument is modified to reflect the number of bytes read. If a char was put back into the stream via ungetc, it will be the first character returned.
It is not possible for both bytes to be read and an APR_EOF or other error to be returned. APR_EINTR is never returned.

Definition at line 457 of file switch_apr.c.

Referenced by main(), and SWITCH_STANDARD_APP().

458 {
459  return fspr_file_read(thefile, buf, nbytes);
460 }
switch_byte_t switch_byte_t * buf

◆ switch_file_remove()

switch_status_t switch_file_remove ( const char *  path,
switch_memory_pool_t pool 
)

Delete the specified file.

Parameters
pathThe full path to the file (using / on all systems)
poolThe pool to use.
Remarks
If the file is open, it won't be removed until all instances are closed.

Definition at line 452 of file switch_apr.c.

Referenced by record_callback().

453 {
454  return fspr_file_remove(path, pool);
455 }
switch_memory_pool_t * pool
const char *const const char *const path

◆ switch_file_rename()

switch_status_t switch_file_rename ( const char *  from_path,
const char *  to_path,
switch_memory_pool_t pool 
)

Definition at line 447 of file switch_apr.c.

Referenced by switch_ivr_insert_file().

448 {
449  return fspr_file_rename(from_path, to_path, pool);
450 }
switch_memory_pool_t * pool

◆ switch_file_seek()

switch_status_t switch_file_seek ( switch_file_t thefile,
switch_seek_where_t  where,
int64_t *  offset 
)

Definition at line 417 of file switch_apr.c.

418 {
419  fspr_status_t rv;
420  fspr_off_t off = (fspr_off_t) (*offset);
421  rv = fspr_file_seek(thefile, where, &off);
422  *offset = (int64_t) off;
423  return rv;
424 }

◆ switch_file_trunc()

switch_status_t switch_file_trunc ( switch_file_t thefile,
int64_t  offset 
)

Definition at line 437 of file switch_apr.c.

438 {
439  return fspr_file_trunc(thefile, offset);
440 }

◆ switch_file_write()

switch_status_t switch_file_write ( switch_file_t thefile,
const void *  buf,
switch_size_t nbytes 
)

Write data to the specified file.

Parameters
thefileThe file descriptor to write to.
bufThe buffer which contains the data.
nbytesOn entry, the number of bytes to write; on exit, the number of bytes written.
Remarks
fspr_file_write will write up to the specified number of bytes, but never more. If the OS cannot write that many bytes, it will write as many as it can. The third argument is modified to reflect the * number of bytes written.
It is possible for both bytes to be written and an error to be returned. APR_EINTR is never returned.

Definition at line 462 of file switch_apr.c.

Referenced by main(), switch_file_printf(), and SWITCH_STANDARD_APP().

463 {
464  return fspr_file_write(thefile, buf, nbytes);
465 }
switch_byte_t switch_byte_t * buf