Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_files.c File Reference

Cross-platform directory scanning (Linux/Solaris/Windows), optionnal recursion. More...

#include "nilorea/n_files.h"
#include "nilorea/n_common.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <dirent.h>
Include dependency graph for n_files.c:

Go to the source code of this file.

Macros

#define N_FILE_COPY_BLOCK   262144
 Block size for the streaming copy and compare.

Functions

int n_comp_file_info (const void *a, const void *b)
 Comparison function for sorting N_FILE_INFO by time (oldest first).
int n_file_copy_verified (const char *from, const char *to, size_t *copied)
 Copy a file, check the copy against the original, and only then give it its name.
int n_file_read_bounded (const char *path, size_t max_size, unsigned char **out, size_t *out_len)
 read a whole file into a freshly allocated, NUL-terminated buffer, refusing a file larger than max_size bytes.
int n_file_same_content (const char *a, const char *b)
 Do two files hold the same bytes.
static int n_file_streams_equal (FILE *a, FILE *b)
 Compare two open streams to their ends.
int n_file_write_atomic (const char *path, const unsigned char *data, size_t len, int mode)
 durably and atomically write len bytes to path: write a "<path>.tmp" sibling created with mode from the outset (POSIX; 0 or Windows = leave default), flush it to stable storage, then atomically rename it over path (MoveFileEx REPLACE_EXISTING|WRITE_THROUGH on Windows, which replaces an existing target where the CRT rename() would fail).
void n_free_file_info (void *ptr)
 Cross-platform directory scanning (Linux / Solaris / Windows via MinGW) WITHOUT chdir().
char * n_path_join (const char *dir, const char *name)
 Build "dir + '/' + name" (or without extra slash if dir already ends with '/' or '\').
int n_scan_dir (const char *dir, LIST *result, const int recurse)
 Scan a directory and append only regular files into a sorted list (oldest first).
void n_set_time_from_stat (N_FILE_INFO *file, const struct stat *st)
 Fill filetime and filetime_nsec from a struct stat (mtime).

Detailed Description

Cross-platform directory scanning (Linux/Solaris/Windows), optionnal recursion.

Author
Castagnier Mickael
Version
1.0
Date
02/11/23

Definition in file n_files.c.

Macro Definition Documentation

◆ N_FILE_COPY_BLOCK

#define N_FILE_COPY_BLOCK   262144

Block size for the streaming copy and compare.

Definition at line 327 of file n_files.c.

Referenced by n_file_copy_verified(), and n_file_streams_equal().

Function Documentation

◆ n_comp_file_info()

int n_comp_file_info ( const void * a,
const void * b )

Comparison function for sorting N_FILE_INFO by time (oldest first).

Orders by: 1) filetime (seconds) ascending 2) filetime_nsec (nanoseconds) ascending

Return values:

  • < 0 if a is older than b
  • = 0 if equal timestamps
  • > 0 if a is newer than b
Parameters
aPointer to first N_FILE_INFO
bPointer to second N_FILE_INFO
Returns
Ordering result.

Definition at line 137 of file n_files.c.

References N_FILE_INFO::filetime, and N_FILE_INFO::filetime_nsec.

Referenced by n_scan_dir().

Here is the caller graph for this function:

◆ n_file_streams_equal()

int n_file_streams_equal ( FILE * a,
FILE * b )
static

Compare two open streams to their ends.

Parameters
afirst stream, positioned at its start
bsecond stream, positioned at its start
Returns
TRUE when they hold the same bytes

Definition at line 335 of file n_files.c.

References Free, Malloc, and N_FILE_COPY_BLOCK.

Referenced by n_file_same_content().

Here is the caller graph for this function:

◆ n_path_join()

char * n_path_join ( const char * dir,
const char * name )

Build "dir + '/' + name" (or without extra slash if dir already ends with '/' or '\').

Uses '/' as a separator for portability. Windows (MinGW) accepts forward slashes.

Parameters
dirBase directory path (non-NULL)
nameEntry name (non-NULL)
Returns
Newly allocated full path (free() by caller), or NULL on error.

Definition at line 94 of file n_files.c.

References last.

Referenced by n_scan_dir().

Here is the caller graph for this function:

◆ n_set_time_from_stat()

void n_set_time_from_stat ( N_FILE_INFO * file,
const struct stat * st )

Fill filetime and filetime_nsec from a struct stat (mtime).

On Linux and Solaris, uses st_mtim (seconds + nanoseconds). On other POSIX environments, falls back to st_mtime (seconds only).

Parameters
fileN_FILE_INFO to fill (non-NULL)
ststruct stat source (non-NULL)

Definition at line 162 of file n_files.c.

References N_FILE_INFO::filetime, and N_FILE_INFO::filetime_nsec.

Referenced by n_scan_dir().

Here is the caller graph for this function: