/
fileio.h
119 lines (96 loc) · 3.69 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
// This copies the memory, so you may free (ptr) after this function returns.
MojoInput *MojoInput_newFromMemory(const uint8 *ptr, uint32 len, int constant);
49
50
MojoInput *MojoInput_newFromFile(const char *fname);
51
52
53
typedef enum
{
54
55
MOJOARCHIVE_ENTRY_UNKNOWN = 0,
MOJOARCHIVE_ENTRY_FILE,
56
57
58
MOJOARCHIVE_ENTRY_DIR,
MOJOARCHIVE_ENTRY_SYMLINK,
} MojoArchiveEntryType;
59
60
// Abstract archive interface. Archives, directories, etc.
61
typedef struct MojoArchiveEntry
62
{
63
char *filename;
64
char *linkdest;
65
MojoArchiveEntryType type;
66
int64 filesize;
67
uint16 perms;
68
} MojoArchiveEntry;
69
70
void MojoArchive_resetEntry(MojoArchiveEntry *info);
71
72
73
74
75
typedef struct MojoArchive MojoArchive;
struct MojoArchive
{
76
// public
77
boolean (*enumerate)(MojoArchive *ar);
78
const MojoArchiveEntry* (*enumNext)(MojoArchive *ar);
79
80
81
MojoInput* (*openCurrentEntry)(MojoArchive *ar);
void (*close)(MojoArchive *ar);
82
// private
83
MojoInput *io;
84
MojoArchiveEntry prevEnum;
85
int64 offsetOfStart; // byte offset in MojoInput where archive starts.
86
87
88
void *opaque;
};
89
MojoArchive *MojoArchive_newFromDirectory(const char *dirname);
90
MojoArchive *MojoArchive_newFromInput(MojoInput *io, const char *origfname);
91
92
93
94
95
96
97
98
// 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);
99
extern MojoArchive *GBaseArchive;
100
extern const char *GBaseArchivePath;
101
102
103
MojoArchive *MojoArchive_initBaseArchive(void);
void MojoArchive_deinitBaseArchive(void);
104
105
typedef boolean (*MojoInput_FileCopyCallback)(uint32 ticks, int64 justwrote,
int64 bw, int64 total, void *data);
106
boolean MojoInput_toPhysicalFile(MojoInput *in, const char *fname, uint16 perms,
107
MojoChecksums *checksums, int64 maxbytes,
108
MojoInput_FileCopyCallback cb, void *data);
109
110
MojoInput *MojoInput_newFromURL(const char *url);
111
112
113
114
115
#ifdef __cplusplus
}
#endif
116
117
#endif
118
// end of fileio.h ...