]>
Commit | Line | Data |
---|---|---|
b24c88b3 RC |
1 | /* |
2 | * Copyright (c) 2001, Robert Collins. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by Robert Collins <rbtcollins@hotmail.com> | |
13 | * | |
14 | */ | |
15 | ||
c93bc6d0 MB |
16 | #ifndef SETUP_IO_STREAM_H |
17 | #define SETUP_IO_STREAM_H | |
b24c88b3 RC |
18 | |
19 | /* this is the parent class for all IO operations. It's flexable enough to be cover for | |
20 | * HTTP access, local file access, and files being extracted from archives. | |
21 | * It also encapsulates the idea of an archive, and all non-archives become the special | |
22 | * case. | |
23 | */ | |
24 | ||
3c054baf RC |
25 | #include <stdio.h> |
26 | #include "String++.h" | |
19911586 | 27 | class IOStreamProvider; |
3c054baf | 28 | |
b24c88b3 RC |
29 | /* Some things don't fit cleanly just - TODO |
30 | * make mkdir_p fit in the hierarchy | |
31 | */ | |
32 | ||
33 | //Where is this defined? | |
ac949c48 | 34 | #if defined(_WIN32) && ! defined(__CYGWIN__) |
b24c88b3 | 35 | typedef signed long ssize_t; |
ac949c48 | 36 | #endif |
b24c88b3 RC |
37 | |
38 | #if __GNUC__ | |
39 | #define _ATTR_(foo) __attribute__ foo | |
40 | #else | |
41 | #define _ATTR_(foo) | |
42 | #endif | |
43 | ||
44 | typedef enum | |
45 | { | |
46 | PATH_TO_DIR, | |
47 | PATH_TO_FILE | |
48 | } | |
49 | path_type_t; | |
50 | ||
51 | typedef enum | |
52 | { | |
53 | IO_STREAM_INVALID, | |
54 | IO_STREAM_STREAM, | |
55 | IO_STREAM_COMPRESS, | |
56 | IO_STREAM_ARCHIVE | |
57 | } | |
58 | io_stream_type_t; | |
59 | ||
60 | typedef enum | |
61 | { | |
62 | IO_STREAM_SYMLINK, | |
63 | IO_STREAM_HARDLINK | |
64 | } | |
65 | io_stream_link_t; | |
66 | ||
ca9506cc RC |
67 | typedef enum |
68 | { | |
69 | IO_SEEK_SET = SEEK_SET, | |
70 | IO_SEEK_END = SEEK_END, | |
71 | IO_SEEK_CUR = SEEK_CUR | |
7c7034e8 RC |
72 | } |
73 | io_stream_seek_t; | |
ca9506cc | 74 | |
b24c88b3 RC |
75 | class io_stream |
76 | { | |
77 | public: | |
19911586 RC |
78 | /* Register a new io_stream provider */ |
79 | static void registerProvider (IOStreamProvider &, String const &urlscheme); | |
b24c88b3 RC |
80 | /* create a new stream from an existing one - used to get |
81 | * decompressed data | |
82 | * or open archives. | |
83 | * will return NULL if there is no sub-stream available (ie (peek() | |
84 | * didn't match any known magic number) && nextfilename () = NULL | |
85 | */ | |
86 | static io_stream *factory (io_stream *); | |
87 | /* open a stream by url. The particular stream type returned | |
88 | * will depend on the url passed. | |
89 | * ie for file:// it will be a disk file. | |
90 | * for ftp:// it will perform an upload to a ftp site. | |
91 | * the second parameter - mode can specify r|w && t|b. Other flags are not currently | |
92 | * supported. | |
93 | * Automatic decompression does not occur. Compressed files will return a io_stream | |
94 | * from archive::decompress. This behaviour is by design - to allow deliberate access | |
95 | * to the compressed data. | |
96 | * To create a stream that will be compressed, you should open the url, and then get a new stream | |
97 | * from archive::compress. | |
98 | * If a stream is opened for reading, and it's an archive, the next_file_name method | |
99 | * will return non-NULL. To access the files within the archive use io_stream::factory | |
100 | * to create a new stream that will read from the archive. | |
101 | */ | |
3c054baf RC |
102 | static io_stream *open (String const &, String const &); |
103 | static int remove (String const &); | |
104 | static int exists (String const &); | |
7c7034e8 RC |
105 | /* moves physical stream source to dest. A copy will be attempted if a |
106 | * pointer flip fails. | |
107 | */ | |
3c054baf | 108 | static int move (String const &, String const &); |
b24c88b3 RC |
109 | /* ensure that we have access to the entire path */ |
110 | /* Create a directory, and any needed parent directories. | |
111 | * returns 1 on failure. | |
112 | */ | |
3c054baf | 113 | static int mkpath_p (path_type_t, String const &); |
b24c88b3 | 114 | /* link from, to, type. Returns 1 on failure */ |
3c054baf | 115 | static int mklink (String const &, String const &, io_stream_link_t); |
341988b9 RC |
116 | /* copy from stream to stream - 0 on success */ |
117 | static ssize_t copy (io_stream *, io_stream *); | |
b24c88b3 RC |
118 | /* TODO: we may need two versions of each of these: |
119 | 1 for external use - when the path is known | |
120 | 1 for inline use, for example to set the mtime of a file being written | |
121 | into a tarball | |
122 | */ | |
123 | /* set the modification time of a file - returns 1 on failure | |
124 | * may distrupt internal state - use after all important io is complete | |
125 | */ | |
126 | virtual int set_mtime (int) = 0; | |
127 | /* get the mtime for a file TODO make this a stat(0 style call */ | |
128 | virtual int get_mtime () = 0; | |
341988b9 RC |
129 | /* How long is the file? 0 means check error(). if error() is 0, the file |
130 | * is 0 bytes long. Otherwise the file length cannot be determined | |
131 | */ | |
132 | virtual size_t get_size () = 0; | |
b24c88b3 RC |
133 | /* read data (duh!) */ |
134 | virtual ssize_t read (void *buffer, size_t len) = 0; | |
135 | /* provide data to (double duh!) */ | |
ca9506cc | 136 | virtual ssize_t write (const void *buffer, size_t len) = 0; |
b24c88b3 RC |
137 | /* read data without removing it from the class's internal buffer */ |
138 | virtual ssize_t peek (void *buffer, size_t len) = 0; | |
139 | /* ever read the f* functions from libc ? */ | |
140 | virtual long tell () = 0; | |
ca9506cc | 141 | virtual int seek (long, io_stream_seek_t) = 0; |
b24c88b3 RC |
142 | /* try guessing this one */ |
143 | virtual int error () = 0; | |
144 | /* hmm, yet another for the guessing books */ | |
145 | virtual char *gets (char *, size_t len); | |
146 | /* what sort of stream is this? | |
147 | * known types are: | |
148 | * IO_STREAM_INVALID - not a valid stream. | |
149 | * IO_STREAM_STREAM - just another stream. | |
150 | * IO_STREAM_COMPRESS - a compressed or compressing stream. | |
151 | * IO_STREAM_ARCHIVE - an archive of some sort, with > 0 files. | |
152 | * this is a crutch for real runtime type evaluation. | |
153 | */ | |
154 | /* Find out the next stream name - | |
155 | * ie for foo.tar.gz, at offset 0, next_file_name = foo.tar | |
156 | * for foobar that is an archive, next_file_name is the next | |
157 | * extractable filename. | |
158 | */ | |
159 | // virtual const char* next_file_name() = NULL; | |
160 | /* if you are still needing these hints... give up now! */ | |
3c054baf RC |
161 | virtual ~ io_stream () = 0; |
162 | ||
163 | io_stream& operator << (io_stream&); | |
164 | ||
cbfc4215 | 165 | protected: |
df62e023 | 166 | void operator= (const io_stream &); |
6b68e703 | 167 | io_stream() {}; |
df62e023 | 168 | io_stream (const io_stream &); |
7c7034e8 | 169 | private: |
3c054baf | 170 | static int move_copy (String const &, String const &); |
b24c88b3 RC |
171 | }; |
172 | ||
c93bc6d0 | 173 | #endif /* SETUP_IO_STREAM_H */ |