/
fileio.h
159 lines (130 loc) · 5.83 KB
1
2
3
4
5
6
7
8
/**
* MojoSetup; a portable, flexible installation application.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
9
10
11
12
13
#ifndef _INCL_FILEIO_H_
#define _INCL_FILEIO_H_
#include "universal.h"
14
15
16
17
#ifdef __cplusplus
extern "C" {
#endif
18
19
20
21
22
23
24
/*
* File i/o may go through multiple layers: the archive attached to the binary,
* then an archive in there that's being read entirely out of memory that's
* being uncompressed to on the fly, or it might be a straight read from a
* regular uncompressed file on physical media. It might be a single file
* compressed with bzip2. As such, we have to have an abstraction over the
* usual channels...basically what we need here is archives-within-archives,
25
* done transparently and with arbitrary depth, although usually not more
26
27
28
29
* than one deep. This also works as a general transport layer, so the
* abstraction could be extended to network connections and such, too.
*/
30
// Abstract input interface. Files, memory, archive entries, etc.
31
32
33
typedef struct MojoInput MojoInput;
struct MojoInput
{
34
// public
35
boolean (*ready)(MojoInput *io);
36
int64 (*read)(MojoInput *io, void *buf, uint32 bufsize);
37
boolean (*seek)(MojoInput *io, uint64 pos);
38
39
40
int64 (*tell)(MojoInput *io);
int64 (*length)(MojoInput *io);
MojoInput* (*duplicate)(MojoInput *io);
41
42
void (*close)(MojoInput *io);
43
// private
44
45
46
void *opaque;
};
47
48
// If constant == 0, then this copies the memory, so you may free (ptr) after
// this function returns in that case.
49
MojoInput *MojoInput_newFromMemory(const uint8 *ptr, uint32 len, int constant);
50
51
// Get a MojoInput for a real file in the physical filesystem.
52
53
MojoInput *MojoInput_newFromFile(const char *fname);
54
55
56
57
58
// Make a subset range of (io) look like the entire file. This will take over
// control of (io), closing it when done, so never reference (io) directly
// again, if this call succeeds.
MojoInput *MojoInput_newFromSubset(MojoInput *io, const uint64 start,
const uint64 end);
59
60
61
typedef enum
{
62
63
MOJOARCHIVE_ENTRY_UNKNOWN = 0,
MOJOARCHIVE_ENTRY_FILE,
64
65
66
MOJOARCHIVE_ENTRY_DIR,
MOJOARCHIVE_ENTRY_SYMLINK,
} MojoArchiveEntryType;
67
68
// Abstract archive interface. Archives, directories, etc.
69
typedef struct MojoArchiveEntry
70
{
71
char *filename;
72
char *linkdest;
73
MojoArchiveEntryType type;
74
int64 filesize;
75
uint16 perms;
76
} MojoArchiveEntry;
77
78
void MojoArchive_resetEntry(MojoArchiveEntry *info);
79
80
81
82
83
typedef struct MojoArchive MojoArchive;
struct MojoArchive
{
84
// public
85
boolean (*enumerate)(MojoArchive *ar);
86
const MojoArchiveEntry* (*enumNext)(MojoArchive *ar);
87
88
89
MojoInput* (*openCurrentEntry)(MojoArchive *ar);
void (*close)(MojoArchive *ar);
90
// private
91
MojoInput *io;
92
MojoArchiveEntry prevEnum;
93
int64 offsetOfStart; // byte offset in MojoInput where archive starts.
94
95
96
void *opaque;
};
97
MojoArchive *MojoArchive_newFromDirectory(const char *dirname);
98
MojoArchive *MojoArchive_newFromInput(MojoInput *io, const char *origfname);
99
100
101
102
103
104
105
106
// This will reset enumeration in the archive, don't use it while iterating!
// Also, this can be very slow depending on the archive in question, so
// try to limit your random access filename lookups to known-fast quantities
// (like directories on the physical filesystem or a zipfile...tarballs and
// zipfiles-in-zipfiles will bog down here, for example).
MojoInput *MojoInput_newFromArchivePath(MojoArchive *ar, const char *fname);
107
108
109
110
111
112
113
114
115
// Wrap (origio) in a new MojoInput that decompresses a compressed stream
// on the fly. Returns NULL on error or if (origio) isn't a supported
// compressed format. The returned MojoInput wraps the original input;
// closing the returned MojoInput will close (origio), too, and you should
// consider origio lost. If this function returns non-NULL, you should not,
// under any circumstances, interact directly with origio again, as the
// new MojoInput now owns it.
MojoInput *MojoInput_newCompressedStream(MojoInput *origio);
116
// !!! FIXME: fill in missing documentation here.
117
extern MojoArchive *GBaseArchive;
118
extern const char *GBaseArchivePath;
119
120
121
MojoArchive *MojoArchive_initBaseArchive(void);
void MojoArchive_deinitBaseArchive(void);
122
123
typedef boolean (*MojoInput_FileCopyCallback)(uint32 ticks, int64 justwrote,
int64 bw, int64 total, void *data);
124
boolean MojoInput_toPhysicalFile(MojoInput *in, const char *fname, uint16 perms,
125
MojoChecksums *checksums, int64 maxbytes,
126
MojoInput_FileCopyCallback cb, void *data);
127
128
MojoInput *MojoInput_newFromURL(const char *url);
129
130
131
132
133
// Read a littleendian, unsigned 16-bit integer from (io), swapping it to
// the correct byteorder for the platform, and moving the file pointer
// ahead 2 bytes. Returns true on successful read and fills the swapped
// value into (*ui16), false on i/o error or EOF.
134
boolean MojoInput_readui16(MojoInput *io, uint16 *ui16);
135
136
137
138
139
// Read a littleendian, unsigned 32-bit integer from (io), swapping it to
// the correct byteorder for the platform, and moving the file pointer
// ahead 4 bytes. Returns true on successful read and fills the swapped
// value into (*ui32), false on i/o error or EOF.
140
boolean MojoInput_readui32(MojoInput *io, uint32 *ui32);
141
142
143
144
145
146
147
// Read a littleendian, unsigned 64-bit integer from (io), swapping it to
// the correct byteorder for the platform, and moving the file pointer
// ahead 8 bytes. Returns true on successful read and fills the swapped
// value into (*ui64), false on i/o error or EOF.
boolean MojoInput_readui64(MojoInput *io, uint64 *ui64);
148
149
150
151
// (Please note that there are not bigendian versions of MojoInput_readuiXX()
// at the moment, as we don't need them for our current feature set. However,
// they could be added easily enough.)
152
153
154
155
#ifdef __cplusplus
}
#endif
156
157
#endif
158
// end of fileio.h ...