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