summaryrefslogtreecommitdiff
path: root/include/sqfs/io.h
blob: cf6f1fa7d7f88ad01940c2ec5109804f08ba9a9e (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * io.h - This file is part of libsquashfs
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#ifndef SQFS_IO_H
#define SQFS_IO_H

#include "sqfs/predef.h"

/**
 * @file io.h
 *
 * @brief Contains the @ref sqfs_file_t interface for abstracting file I/O
 */

/**
 * @enum E_SQFS_FILE_OPEN_FLAGS
 *
 * @brief Flags for @ref sqfs_open_file
 */
typedef enum {
	/**
	 * @brief If set, access the file for reading only
	 *
	 * If not set, the file is expected to have a zero size after opening
	 * which can be grown with successive writes to end of the file.
	 *
	 * Opening an existing file with this flag cleared results in failure,
	 * unless the @ref SQFS_FILE_OPEN_OVERWRITE flag is also set.
	 */
	SQFS_FILE_OPEN_READ_ONLY = 0x01,

	/**
	 * @brief If the read only flag is not set, overwrite any
	 *        existing file.
	 *
	 * If the file alrady exists, it is truncated to zero bytes size and
	 * overwritten.
	 */
	SQFS_FILE_OPEN_OVERWRITE = 0x02,

	SQFS_FILE_OPEN_ALL_FLAGS = 0x03,
} E_SQFS_FILE_OPEN_FLAGS;

/**
 * @interface sqfs_file_t
 *
 * @brief Abstracts file I/O to make it easy to embedd SquashFS.
 */
struct sqfs_file_t {
	/**
	 * @brief Close the file and destroy the interface implementation.
	 *
	 * @param file A pointer to the file object.
	 */
	void (*destroy)(sqfs_file_t *file);

	/**
	 * @brief Read a chunk of data from an absolute position.
	 *
	 * @param file A pointer to the file object.
	 * @param offset An absolute offset to read data from.
	 * @param buffer A pointer to a buffer to copy the data to.
	 * @param size The number of bytes to read from the file.
	 *
	 * @return Zero on success, an @ref E_SQFS_ERROR identifier on failure
	 *         that the data structures in libsquashfs that use this return
	 *         directly to the caller.
	 */
	int (*read_at)(sqfs_file_t *file, uint64_t offset,
		       void *buffer, size_t size);

	/**
	 * @brief Write a chunk of data at an absolute position.
	 *
	 * @param file A pointer to the file object.
	 * @param offset An absolute offset to write data to.
	 * @param buffer A pointer to a buffer to write to the file.
	 * @param size The number of bytes to write from the buffer.
	 *
	 * @return Zero on success, an @ref E_SQFS_ERROR identifier on failure
	 *         that the data structures in libsquashfs that use this return
	 *         directly to the caller.
	 */
	int (*write_at)(sqfs_file_t *file, uint64_t offset,
			const void *buffer, size_t size);

	/**
	 * @brief Get the number of bytes currently stored in the file.
	 *
	 * @param file A pointer to the file object.
	 */
	uint64_t (*get_size)(const sqfs_file_t *file);

	/**
	 * @brief Extend or shrink a file to a specified size.
	 *
	 * @param file A pointer to the file object.
	 * @param size The new capacity of the file in bytes.
	 *
	 * @return Zero on success, an @ref E_SQFS_ERROR identifier on failure
	 *         that the data structures in libsquashfs that use this return
	 *         directly to the caller.
	 */
	int (*truncate)(sqfs_file_t *file, uint64_t size);
};

/**
 * @struct sqfs_sparse_map_t
 *
 * @brief Describes the layout of a sparse file.
 *
 * This structure is part of a linked list that indicates where the actual
 * data is located in a sparse file.
 */
struct sqfs_sparse_map_t {
	sqfs_sparse_map_t *next;
	uint64_t offset;
	uint64_t count;
};

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Open a file through the operating systems filesystem API
 *
 * This function internally creates an instance of a reference implementation
 * of the @ref sqfs_file_t interface that uses the operating systems native
 * API for file I/O.
 *
 * On Unix-like systems, if the open call fails, this function makes sure to
 * preserves the value in errno indicating the underlying problem.
 *
 * @param filename The name of the file to open.
 * @param flags A set of @ref E_SQFS_FILE_OPEN_FLAGS.
 *
 * @return A pointer to a file object on success, NULL on allocation failure,
 *         failure to open the file or if an unknown flag was set.
 */
SQFS_API sqfs_file_t *sqfs_open_file(const char *filename, int flags);

/**
 * @brief Get a read-only file implementation that represents standard input
 *
 * This function creates a read-only file that represents STDIN. The file
 * supports reading up to a specified number of bytes and only allows
 * reading sequentially.
 *
 * @param size The alleged "size" of the file.
 *
 * @return A pointer to a file object on success, NULL on allocation failure.
 */
SQFS_API sqfs_file_t *sqfs_get_stdin_file(uint64_t size);


/**
 * @brief Read a chunk from a file and turn it into a block that can be
 *        fed to a block processor.
 *
 * @member sqfs_file_t
 *
 * @param file A pointer to a file implementation.
 * @param offset A byte offset into the file.
 * @param size The number of bytes to read, starting at the given offset.
 * @param user A user pointer to set for the block.
 * @param flags The flags to store in the newly created block.
 * @param out Returns a pointer to a block on success.
 *
 * @return Zero on success, an @ref E_SQFS_ERROR identifier on failure.
 */
SQFS_API int sqfs_file_create_block(sqfs_file_t *file, uint64_t offset,
				    size_t size, void *user, uint32_t flags,
				    sqfs_block_t **out);

#ifdef __cplusplus
}
#endif

#endif /* SQFS_IO_H */