]>
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 | /* this is the parent class for all IO operations. It's flexable enough to be cover for | |
17 | * HTTP access, local file access, and files being extracted from archives. | |
18 | * It also encapsulates the idea of an archive, and all non-archives become the special | |
19 | * case. | |
20 | */ | |
21 | ||
22 | #if 0 | |
23 | static const char *cvsid = | |
24 | "\n%%% $Id$\n"; | |
25 | #endif | |
26 | ||
27 | #include "win32.h" | |
28 | #include <stdio.h> | |
29 | #include "log.h" | |
30 | #include "port.h" | |
31 | ||
32 | #include "io_stream.h" | |
33 | #include "io_stream_file.h" | |
34 | #include "io_stream_cygfile.h" | |
35 | #include "mkdir.h" | |
36 | ||
37 | /* Static members */ | |
38 | io_stream * | |
39 | io_stream::factory (io_stream * parent) | |
40 | { | |
41 | /* something like, | |
42 | * if !next_file_name | |
43 | * return NULL | |
44 | * switch (magic_id(peek (parent), max_magic_length)) | |
45 | * case io_stream * foo = new tar | |
46 | * case io_stream * foo = new bz2 | |
47 | * return foo | |
48 | */ | |
49 | log (LOG_TIMESTAMP, "io_stream::factory has been called"); | |
50 | return NULL; | |
51 | } | |
52 | ||
53 | io_stream * | |
54 | io_stream::open (const char *name, const char *mode) | |
55 | { | |
56 | if (!name || IsBadStringPtr (name, MAX_PATH) || !name[0] || | |
57 | !mode || IsBadStringPtr (mode, 5) || !mode[0]) | |
58 | return NULL; | |
59 | /* iterate through the known url prefix's */ | |
60 | if (!strncasecmp ("file://", name, 7)) | |
61 | { | |
62 | io_stream_file *rv = new io_stream_file (&name[7], mode); | |
63 | if (!rv->error ()) | |
64 | return rv; | |
65 | delete rv; | |
66 | return NULL; | |
67 | } | |
68 | if (!strncasecmp ("cygfile://", name, 10)) | |
69 | { | |
70 | io_stream_cygfile *rv = new io_stream_cygfile (&name[10], mode); | |
71 | if (!rv->error ()) | |
72 | return rv; | |
73 | delete rv; | |
74 | return NULL; | |
75 | } | |
76 | return NULL; | |
77 | } | |
78 | ||
79 | int | |
80 | io_stream::mkpath_p (path_type_t isadir, const char *name) | |
81 | { | |
82 | if (!name || IsBadStringPtr (name, MAX_PATH) || !name[0]) | |
83 | return 1; | |
84 | /* iterate through the known url prefix's */ | |
85 | if (!strncasecmp ("file://", name, 7)) | |
86 | { | |
87 | return mkdir_p (isadir == PATH_TO_DIR ? 1 : 0, &name[7]); | |
88 | } | |
89 | if (!strncasecmp ("cygfile://", name, 10)) | |
90 | { | |
91 | return cygmkdir_p (isadir == PATH_TO_DIR ? 1 : 0, &name[10]); | |
92 | } | |
93 | return 1; | |
94 | } | |
95 | ||
96 | /* remove a file or directory. */ | |
97 | int | |
98 | io_stream::remove (const char *name) | |
99 | { | |
100 | if (!name || IsBadStringPtr (name, MAX_PATH) || !name[0]) | |
101 | return 1; | |
102 | /* iterate through the known url prefix's */ | |
103 | if (!strncasecmp ("file://", name, 7)) | |
104 | return io_stream_file::remove (&name[7]); | |
105 | if (!strncasecmp ("cygfile://", name, 10)) | |
106 | return io_stream_cygfile::remove (&name[10]); | |
107 | return 1; | |
108 | } | |
109 | ||
110 | int | |
111 | io_stream::mklink (const char *from, const char *to, | |
112 | io_stream_link_t linktype) | |
113 | { | |
114 | if (!from || IsBadStringPtr (from, MAX_PATH) || | |
115 | !to || IsBadStringPtr (to, MAX_PATH)) | |
116 | { | |
117 | log (LOG_TIMESTAMP, "invalid string in from or to parameters to mklink"); | |
118 | return 1; | |
119 | } | |
120 | /* iterate through the known url prefixes */ | |
121 | if (!strncasecmp ("file://", from, 7)) | |
122 | { | |
123 | /* file urls can symlink or hardlink to file url's. */ | |
124 | /* TODO: allow linking to cygfile url's */ | |
125 | if (!strncasecmp ("file://", to, 7)) | |
126 | return io_stream_file::mklink (&from[7], &to[7], linktype); | |
127 | log (LOG_TIMESTAMP, "Attempt to link across url providers"); | |
128 | return 1; | |
129 | } | |
130 | if (!strncasecmp ("cygfile://", from, 10)) | |
131 | { | |
132 | /* cygfile urls can symlink or hardlink to cygfile urls's. */ | |
133 | /* TODO: allow -> file urls */ | |
134 | if (!strncasecmp ("cygfile://", to, 10)) | |
135 | return io_stream_cygfile::mklink (&from[10], &to[10], linktype); | |
136 | log (LOG_TIMESTAMP, "Attempt to link across url providers"); | |
137 | return 1; | |
138 | } | |
139 | #if 0 | |
140 | if (!strmcasecmp ("http://", from, 7)) | |
141 | { | |
142 | /* http urls can symlink to http or ftp url's */ | |
143 | } | |
144 | #endif | |
145 | log (LOG_TIMESTAMP, "Unsupported url providers for %s", from); | |
146 | return 1; | |
147 | } | |
148 | ||
149 | char * | |
150 | io_stream::gets (char *buffer, size_t length) | |
151 | { | |
152 | char *pos = buffer; | |
153 | size_t count = 0; | |
154 | while (count + 1 < length && read (pos, 1) == 1) | |
155 | { | |
156 | count++; | |
157 | pos++; | |
158 | if (*(pos - 1) == '\n') | |
159 | { | |
160 | /* end of line */ | |
161 | /* TODO: remove the \r if it is present depending on the | |
162 | * file's mode | |
163 | */ | |
164 | break; | |
165 | } | |
166 | } | |
167 | if (count == 0 || error ()) | |
168 | /* EOF when no chars found, or an error */ | |
169 | return NULL; | |
170 | *pos = '\0'; | |
171 | return buffer; | |
172 | } | |
173 | ||
174 | int | |
175 | io_stream::exists (const char *name) | |
176 | { | |
177 | if (!name || IsBadStringPtr (name, MAX_PATH) || !name[0]) | |
178 | return 1; | |
179 | /* iterate through the known url prefix's */ | |
180 | if (!strncasecmp ("file://", name, 7)) | |
181 | return io_stream_file::exists (&name[7]); | |
182 | if (!strncasecmp ("cygfile://", name, 10)) | |
183 | return io_stream_cygfile::exists (&name[10]); | |
184 | return 1; | |
185 | } | |
186 | ||
187 | /* virtual members */ | |
188 | ||
189 | io_stream::~io_stream () | |
190 | { | |
191 | log (LOG_TIMESTAMP, "io_stream::~io_stream called"); | |
192 | return; | |
193 | } |