FreeSWITCH API Documentation  1.7.0
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 apr_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

Definition at line 925 of file switch_apr.h.

typedef struct switch_dir switch_dir_t

Definition at line 911 of file switch_apr.h.

typedef struct apr_file_t switch_file_t

Structure for referencing files.

Definition at line 685 of file switch_apr.h.

typedef int32_t switch_fileperms_t

Definition at line 687 of file switch_apr.h.

typedef int switch_seek_where_t

Definition at line 688 of file switch_apr.h.

Function Documentation

switch_status_t switch_dir_close ( switch_dir_t thedir)

Definition at line 559 of file switch_apr.c.

Referenced by switch_loadable_module_enumerate_available().

560 {
561  switch_status_t status = apr_dir_close(thedir->dir_handle);
562 
563  free(thedir);
564  return status;
565 }
switch_status_t
Common return values.
apr_dir_t * dir_handle
Definition: switch_apr.c:534
uint32_t switch_dir_count ( switch_dir_t thedir)

Definition at line 567 of file switch_apr.c.

References SWITCH_STATUS_SUCCESS.

568 {
569  const char *name;
570  apr_int32_t finfo_flags = APR_FINFO_DIRENT | APR_FINFO_TYPE | APR_FINFO_NAME;
571  uint32_t count = 0;
572 
573  apr_dir_rewind(thedir->dir_handle);
574 
575  while (apr_dir_read(&(thedir->finfo), finfo_flags, thedir->dir_handle) == SWITCH_STATUS_SUCCESS) {
576 
577  if (thedir->finfo.filetype != APR_REG && thedir->finfo.filetype != APR_LNK) {
578  continue;
579  }
580 
581  if (!(name = thedir->finfo.fname)) {
582  name = thedir->finfo.name;
583  }
584 
585  if (name) {
586  count++;
587  }
588  }
589 
590  apr_dir_rewind(thedir->dir_handle);
591 
592  return count;
593 }
apr_finfo_t finfo
Definition: switch_apr.c:535
apr_dir_t * dir_handle
Definition: switch_apr.c:534
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 523 of file switch_apr.c.

524 {
525  return apr_dir_make(path, perm, pool);
526 }
switch_memory_pool_t * pool
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 528 of file switch_apr.c.

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

529 {
530  return apr_dir_make_recursive(path, perm, pool);
531 }
switch_memory_pool_t * pool
const char* switch_dir_next_file ( switch_dir_t thedir,
char *  buf,
switch_size_t  len 
)

Definition at line 595 of file switch_apr.c.

References buf, switch_copy_string(), and SWITCH_STATUS_SUCCESS.

Referenced by switch_loadable_module_enumerate_available().

596 {
597  const char *fname = NULL;
598  apr_int32_t finfo_flags = APR_FINFO_DIRENT | APR_FINFO_TYPE | APR_FINFO_NAME;
599  const char *name;
600 
601  while (apr_dir_read(&(thedir->finfo), finfo_flags, thedir->dir_handle) == SWITCH_STATUS_SUCCESS) {
602 
603  if (thedir->finfo.filetype != APR_REG && thedir->finfo.filetype != APR_LNK) {
604  continue;
605  }
606 
607  if (!(name = thedir->finfo.fname)) {
608  name = thedir->finfo.name;
609  }
610 
611  if (name) {
612  switch_copy_string(buf, name, len);
613  fname = buf;
614  break;
615  } else {
616  continue;
617  }
618  }
619  return fname;
620 }
char * switch_copy_string(char *dst, const char *src, switch_size_t dst_size)
Definition: switch_apr.c:199
switch_byte_t switch_byte_t * buf
apr_finfo_t finfo
Definition: switch_apr.c:535
apr_dir_t * dir_handle
Definition: switch_apr.c:534
switch_status_t switch_dir_open ( switch_dir_t **  new_dir,
const char *  dirname,
switch_memory_pool_t pool 
)

Definition at line 538 of file switch_apr.c.

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

Referenced by switch_loadable_module_enumerate_available().

539 {
540  switch_status_t status;
541  switch_dir_t *dir = malloc(sizeof(*dir));
542 
543  if (!dir) {
544  *new_dir = NULL;
545  return SWITCH_STATUS_FALSE;
546  }
547 
548  memset(dir, 0, sizeof(*dir));
549  if ((status = apr_dir_open(&(dir->dir_handle), dirname, pool)) == APR_SUCCESS) {
550  *new_dir = dir;
551  } else {
552  free(dir);
553  *new_dir = NULL;
554  }
555 
556  return status;
557 }
switch_memory_pool_t * pool
switch_status_t
Common return values.
apr_dir_t * dir_handle
Definition: switch_apr.c:534
memset(buf, 0, buflen)
switch_status_t switch_directory_exists ( const char *  dirname,
switch_memory_pool_t pool 
)

Definition at line 474 of file switch_apr.c.

References switch_core_destroy_memory_pool, and switch_core_new_memory_pool.

Referenced by switch_core_perform_file_open().

475 {
476  apr_dir_t *dir_handle;
477  switch_memory_pool_t *our_pool = NULL;
478  switch_status_t status;
479 
480  if (!pool) {
481  switch_core_new_memory_pool(&our_pool);
482  pool = our_pool;
483  }
484 
485  if ((status = apr_dir_open(&dir_handle, dirname, pool)) == APR_SUCCESS) {
486  apr_dir_close(dir_handle);
487  }
488 
489  if (our_pool) {
491  }
492 
493  return status;
494 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:631
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:640
switch_memory_pool_t * pool
switch_status_t
Common return values.
struct apr_pool_t switch_memory_pool_t
switch_status_t switch_file_close ( switch_file_t thefile)

Close the specified file.

Parameters
thefileThe file descriptor to close.

Definition at line 409 of file switch_apr.c.

Referenced by main().

410 {
411  return apr_file_close(thefile);
412 }
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 403 of file switch_apr.c.

404 {
405  return apr_file_copy(from_path, to_path, perms, pool);
406 }
switch_memory_pool_t * pool
switch_status_t switch_file_exists ( const char *  filename,
switch_memory_pool_t pool 
)

Definition at line 496 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 switch_core_cert_gen_fingerprint(), switch_core_gen_certs(), switch_core_perform_file_open(), switch_img_txt_handle_create(), and switch_rtp_add_dtls().

497 {
498  int32_t wanted = APR_FINFO_TYPE;
499  switch_memory_pool_t *our_pool = NULL;
501  apr_finfo_t info = { 0 };
502 
503  if (zstr(filename)) {
504  return status;
505  }
506 
507  if (!pool) {
508  switch_core_new_memory_pool(&our_pool);
509  }
510 
511  apr_stat(&info, filename, wanted, pool ? pool : our_pool);
512  if (info.filetype != APR_NOFILE) {
513  status = SWITCH_STATUS_SUCCESS;
514  }
515 
516  if (our_pool) {
518  }
519 
520  return status;
521 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:631
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:640
switch_memory_pool_t * pool
#define zstr(x)
Definition: switch_utils.h:281
switch_status_t
Common return values.
struct apr_pool_t switch_memory_pool_t
char * filename
switch_size_t switch_file_get_size ( switch_file_t thefile)

Definition at line 468 of file switch_apr.c.

References SWITCH_STATUS_SUCCESS.

469 {
470  struct apr_finfo_t finfo;
471  return apr_file_info_get(&finfo, APR_FINFO_SIZE, thefile) == SWITCH_STATUS_SUCCESS ? (switch_size_t) finfo.size : 0;
472 }
uintptr_t switch_size_t
switch_status_t switch_file_lock ( switch_file_t thefile,
int  type 
)

Definition at line 419 of file switch_apr.c.

Referenced by main().

420 {
421  return apr_file_lock(thefile, type);
422 }
switch_status_t switch_file_mktemp ( switch_file_t **  thefile,
char *  templ,
int32_t  flags,
switch_memory_pool_t pool 
)

Definition at line 463 of file switch_apr.c.

464 {
465  return apr_file_mktemp(thefile, templ, flags, pool);
466 }
switch_memory_pool_t * pool
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 apr_os_file_t handle in apr_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,
                                                                                apr_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 388 of file switch_apr.c.

Referenced by main().

390 {
391  return apr_file_open(newf, fname, flag, perm, pool);
392 }
switch_memory_pool_t * pool
int switch_file_printf ( switch_file_t thefile,
const char *  format,
  ... 
)

Definition at line 444 of file switch_apr.c.

References switch_file_write(), and switch_vasprintf().

445 {
446  va_list ap;
447  int ret;
448  char *data;
449 
450  va_start(ap, format);
451 
452  if ((ret = switch_vasprintf(&data, format, ap)) != -1) {
453  switch_size_t bytes = strlen(data);
454  switch_file_write(thefile, data, &bytes);
455  free(data);
456  }
457 
458  va_end(ap);
459 
460  return ret;
461 }
switch_status_t switch_file_write(switch_file_t *thefile, const void *buf, switch_size_t *nbytes)
Definition: switch_apr.c:439
uintptr_t switch_size_t
int switch_vasprintf(char **ret, const char *fmt, va_list ap)
Definition: switch_apr.c:1166
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
apr_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 434 of file switch_apr.c.

Referenced by main().

435 {
436  return apr_file_read(thefile, buf, nbytes);
437 }
switch_byte_t switch_byte_t * buf
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 429 of file switch_apr.c.

Referenced by record_callback().

430 {
431  return apr_file_remove(path, pool);
432 }
switch_memory_pool_t * pool
switch_status_t switch_file_rename ( const char *  from_path,
const char *  to_path,
switch_memory_pool_t pool 
)

Definition at line 424 of file switch_apr.c.

Referenced by switch_ivr_insert_file().

425 {
426  return apr_file_rename(from_path, to_path, pool);
427 }
switch_memory_pool_t * pool
switch_status_t switch_file_seek ( switch_file_t thefile,
switch_seek_where_t  where,
int64_t *  offset 
)

Definition at line 394 of file switch_apr.c.

395 {
396  apr_status_t rv;
397  apr_off_t off = (apr_off_t) (*offset);
398  rv = apr_file_seek(thefile, where, &off);
399  *offset = (int64_t) off;
400  return rv;
401 }
switch_status_t switch_file_trunc ( switch_file_t thefile,
int64_t  offset 
)

Definition at line 414 of file switch_apr.c.

415 {
416  return apr_file_trunc(thefile, offset);
417 }
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
apr_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 439 of file switch_apr.c.

Referenced by main(), and switch_file_printf().

440 {
441  return apr_file_write(thefile, buf, nbytes);
442 }
switch_byte_t switch_byte_t * buf