From bfdac5b274d8ca371307d2b4b417092ba25f11ab Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 28 Aug 2013 17:54:19 +0200 Subject: [SERVER] Copy CRC-32 list from uplink server if available Split up helper.c, move file/disk related functions to fileutil.c Uplink: Make sure relayed requests are at least 1MiB --- src/server/fileutil.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/server/fileutil.c (limited to 'src/server/fileutil.c') diff --git a/src/server/fileutil.c b/src/server/fileutil.c new file mode 100644 index 0000000..ea247f0 --- /dev/null +++ b/src/server/fileutil.c @@ -0,0 +1,75 @@ +#include "fileutil.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +int file_isReadable(char *file) +{ + int fd = open( file, O_RDONLY ); + if ( fd < 0 ) return FALSE; + close( fd ); + return TRUE; +} + +int file_isWritable(char *file) +{ + int fd = open( file, O_WRONLY ); + if ( fd >= 0 ) { + close( fd ); + return TRUE; + } + fd = open( file, O_WRONLY | O_CREAT, 0600 ); + if ( fd < 0 ) return FALSE; + close( fd ); + unlink( file ); + return TRUE; +} + +int mkdir_p(const char* path) +{ + assert( path != NULL ); + if ( *path == '\0' ) return TRUE; + char buffer[strlen( path ) + 1]; + strcpy( buffer, path ); + char *current = buffer; + char *slash; + while ( (slash = strchr( current, '/' )) != NULL ) { + *slash = '\0'; + if ( *buffer != '\0' && mkdir( buffer, 0750 ) != 0 && errno != EEXIST ) return FALSE; + *slash = '/'; + current = slash + 1; + } + if ( mkdir( buffer, 0750 ) != 0 && errno != EEXIST ) return FALSE; + return TRUE; +} + +int file_alloc(int fd, uint64_t offset, uint64_t size) +{ + if ( fallocate( fd, 0, offset, size ) == 0 ) return TRUE; // fast way + if ( posix_fallocate( fd, offset, size ) == 0 ) return TRUE; // slow way + if ( lseek( fd, offset + size - 1, SEEK_SET ) != offset ) return FALSE; // dumb way + if ( write( fd, "", 1 ) != 1 ) return FALSE; + return TRUE; +} + +uint64_t file_freeDiskSpace(const char * const path) +{ + struct statvfs fiData; + if ( (statvfs( path, &fiData )) < 0 ) { + return 0; + } + return ((uint64_t)fiData.f_bfree * (uint64_t)fiData.f_bsize * 95LL) / 100LL; // Assume 5% root reservation is active +} + +time_t file_lastModification(const char * const file) +{ + struct stat st; + if ( stat( file, &st ) != 0 ) return 0; + return st.st_mtime; +} -- cgit v1.2.3-55-g7522