summaryrefslogtreecommitdiffstats
path: root/src/server/fileutil.c
blob: 336ab683c9337add7709c313f4b1ab4c024f2414 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "fileutil.h"
#include "helper.h"

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/statvfs.h>

bool file_isReadable(char *file)
{
	int fd = open( file, O_RDONLY );
	if ( fd < 0 ) return false;
	close( fd );
	return true;
}

bool 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 );
	remove( file );
	return true;
}

bool 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, 0755 ) != 0 && errno != EEXIST ) return false;
		*slash = '/';
		current = slash + 1;
	}
	if ( mkdir( buffer, 0755 ) != 0 && errno != EEXIST ) return false;
	return true;
}

bool file_alloc(int fd, uint64_t offset, uint64_t size)
{
#ifdef __linux__
	if ( fallocate( fd, 0, offset, size ) == 0 ) return true; // fast way
#elif defined(__FreeBSD__)
	if ( posix_fallocate( fd, offset, size ) == 0 ) return true; // slow way
#endif
	return false;
}

bool file_setSize(int fd, uint64_t size)
{
	if ( ftruncate( fd, size ) == 0 ) return true;

	// Try really hard... image loading logic relies on the file
	// having the proper apparent size
	uint8_t byte = 0;
	pread( fd, &byte, 1, size - 1 );
	if ( pwrite( fd, &byte, 1, size - 1 ) == 1 ) return true;
	return false;
}

bool file_freeDiskSpace(const char * const path, uint64_t *total, uint64_t *avail)
{
	struct statvfs fiData;
	if ( statvfs( path, &fiData ) < 0 ) {
		return false;
	}
	if ( avail != NULL ) {
		*avail = ((uint64_t)fiData.f_bavail * (uint64_t)fiData.f_frsize);
	}
	if ( total != NULL ) {
		*total = ((uint64_t)fiData.f_blocks * (uint64_t)fiData.f_frsize);
	}
	return true;
}

time_t file_lastModification(const char * const file)
{
	struct stat st;
	if ( stat( file, &st ) != 0 ) return 0;
	return st.st_mtime;
}

int file_loadLineBased(const char * const file, int minFields, int maxFields, void (*cb)(int argc, char **argv, void *data), void *data)
{
	char buffer[1000], *line;
	char *items[20];
	int count = 0, itemCount;

	if ( file == NULL || cb == NULL ) return -1;
	FILE *fp = fopen( file, "r" );
	if ( fp == NULL ) return -1;
	while ( fgets( buffer, sizeof(buffer), fp ) != NULL ) {
		itemCount = 0;
		for (line = buffer; *line != '\0' && itemCount < 20; ) { // Trim left and scan for "-" prefix
			while ( *line == ' ' || *line == '\t' ) ++line;
			if ( *line == '\r' || *line == '\n' || *line == '\0' ) break; // Ignore empty lines
			items[itemCount++] = line;
			if ( itemCount >= maxFields ) {
				trim_right( line );
				break;
			}
			while ( *line != '\0' && *line != ' ' && *line != '\t' && *line != '\r' && *line != '\n' ) ++line;
			if ( *line != '\0' ) *line++ = '\0';
		}
		if ( itemCount >= minFields ) {
			cb( itemCount, items, data );
			count++;
		}
	}
	fclose( fp );
	return count;
}