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