aboutsummaryrefslogtreecommitdiff
path: root/include/libtio/fs.h
blob: 11265f6a903d0a7c1d12ed268a9a80161d1c4078 (plain)
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
#ifndef  LIBTIO_FS_H
# define LIBTIO_FS_H 20190525
# include "cdefs.h"
# include "stream.h"

TIO_BEGIN_NAMESPACE
TIO_STRUCT(tio_file_cursor,           tio_file_cursor_t)
TIO_STRUCT(tio_file_cursor_functions, tio_file_cursor_functions_t)
TIO_STRUCT(tio_stat,                  tio_stat_t)

/* File type. */

typedef int tio_file_type_t;

# define TIO_FILETYPE_OTH 0
# define TIO_FILETYPE_REG 1
# define TIO_FILETYPE_DIR 2

/* File information. */

# define TIO_STAT_FLAG_TIMES  1
# define TIO_STAT_FLAG_ALL    1

struct tio_stat {
	tio_file_type_t tio_stat_type;

	tio_off_t       tio_stat_size;
	tio_time_t     *tio_stat_btime;
	tio_time_t     *tio_stat_atime;
	tio_time_t     *tio_stat_mtime;
};

/* Flags for opening a file.
 *
 * `WRITE`: allow writes on the file.
 * `CREATE`: create the regular file if it doesn't exist yet.
 * `END`: instead of setting the cursor at the beginning of the file,
 *        set it at its end (e.g. for appending). */

# define TIO_FILE_OPEN_FLAG_WRITE  1
# define TIO_FILE_OPEN_FLAG_CREATE 2
# define TIO_FILE_OPEN_FLAG_END    4

/* Filesystem functions. */

typedef TIO_HOOK_TYPE(void) tio_file_cursor_close_t
	OF((void *tio__cookie));
typedef TIO_HOOK_TYPE(int) tio_file_cursor_copy_t
	OF((void *tio__cookie, tio_file_cursor_t **tio__cursorp));

typedef TIO_HOOK_TYPE(int) tio_file_cursor_move_t
	OF((void *tio__cookie, char const *tio__name));
typedef TIO_HOOK_TYPE(int) tio_file_cursor_create_directory_t
	OF((void *tio__cookie, char const *tio__name));
typedef TIO_HOOK_TYPE(int) tio_file_cursor_remove_t
	OF((void *tio__cookie, char const *tio__name));
typedef TIO_HOOK_TYPE(int) tio_file_cursor_list_t
	OF((void *tio__cookie, tio_iter_t **tio__iterp));
typedef TIO_HOOK_TYPE(int) tio_file_cursor_stat_t
	OF((void *tio__cookie, char const *tio__name,
		tio_stat_t *tio__stat, unsigned long tio__flags));
typedef TIO_HOOK_TYPE(int) tio_file_cursor_open_t
	OF((void *tio__cookie, tio_stream_t **tio__streamp,
		char const *tio__name, unsigned long tio__flags,
		tio_off_t tio__size));

# define TIO_FILECURSORFLAG_MOVE    1
# define TIO_FILECURSORFLAG_CREATE  2
# define TIO_FILECURSORFLAG_REMOVE  4
# define TIO_FILECURSORFLAG_LIST    8
# define TIO_FILECURSORFLAG_STAT   16
# define TIO_FILECURSORFLAG_OPEN   32

struct tio_file_cursor_functions {
	tio_file_cursor_close_t  *tio_file_cursor_functions_close;
	tio_file_cursor_copy_t   *tio_file_cursor_functions_copy;

	/* Cursor interactions. */

	tio_file_cursor_move_t   *tio_file_cursor_functions_move;

	/* Directory interactions. */

	tio_file_cursor_create_directory_t
		*tio_file_cursor_functions_create_directory;
	tio_file_cursor_remove_t *tio_file_cursor_functions_remove;
	tio_file_cursor_list_t   *tio_file_cursor_functions_list;
	tio_file_cursor_stat_t   *tio_file_cursor_functions_stat;

	/* Stream interactions. */

	tio_file_cursor_open_t   *tio_file_cursor_functions_open;
};

/* ---
 * Public functions.
 * --- */

TIO_BEGIN_DECLS

# define tio_next_file(ITER, STATP) \
	tio_next((ITER), (tio_stat_t **)(void **)(STATP))

/* File cursor management. */

TIO_EXTERN(int) tio_move
	OF((tio_file_cursor_t *tio__cursor, char const *tio__name));

TIO_EXTERN(int) tio_create_directory
	OF((tio_file_cursor_t *tio__cursor, char const *tio__name));

TIO_EXTERN(int) tio_remove
	OF((tio_file_cursor_t *tio__cursor, char const *tio__name));
TIO_EXTERN(int) tio_list
	OF((tio_file_cursor_t *tio__cursor, tio_iter_t **tio__iterp));
TIO_EXTERN(int) tio_stat
	OF((tio_file_cursor_t *tio__cursor, char const *tio__name,
		tio_stat_t *tio__stat, unsigned long tio__flags));

TIO_EXTERN(int) tio_open_file
	OF((tio_file_cursor_t *tio__cursor, tio_stream_t **tio__streamp,
		char const *tio__name, unsigned long tio__flags));

TIO_END_DECLS
TIO_END_NAMESPACE

#endif