-
Notifications
You must be signed in to change notification settings - Fork 4
/
cpl_vsil_unix_stdio_64.cpp
353 lines (283 loc) · 11.5 KB
/
cpl_vsil_unix_stdio_64.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**********************************************************************
* $Id: cpl_vsil_unix_stdio_64.cpp,v 1.11 2006/03/27 15:24:41 fwarmerdam Exp $
*
* Project: CPL - Common Portability Library
* Purpose: Implement VSI large file api for Unix platforms with fseek64()
* and ftell64() such as IRIX.
* Author: Frank Warmerdam, [email protected]
*
**********************************************************************
* Copyright (c) 2001, Frank Warmerdam
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**********************************************************************
*
* $Log: cpl_vsil_unix_stdio_64.cpp,v $
* Revision 1.11 2006/03/27 15:24:41 fwarmerdam
* buffer in FWrite is const
*
* Revision 1.10 2006/02/19 21:54:34 mloskot
* [WINCE] Changes related to Windows CE port of CPL. Most changes are #ifdef wrappers.
*
* Revision 1.9 2006/01/10 17:03:56 fwarmerdam
* added VSI Rename support
*
* Revision 1.8 2005/09/11 18:01:28 fwarmerdam
* preliminary implementatin of fully virtualized large file api
*
* Revision 1.7 2005/04/12 00:27:39 fwarmerdam
* added macos large file support
*
* Revision 1.6 2002/06/17 14:00:16 warmerda
* segregate VSIStatL() and VSIStatBufL.
*
* Revision 1.5 2002/06/15 00:07:23 aubin
* mods to enable 64bit file i/o
*
* Revision 1.4 2001/12/15 17:12:08 warmerda
* pass 64bit seek/tell functions via VSI_F{SEEK,TELL}64 macros
*
* Revision 1.3 2001/07/18 04:00:49 warmerda
* added CPL_CVSID
*
* Revision 1.2 2001/06/21 21:39:32 warmerda
* Fixed typo with rewind.
*
* Revision 1.1 2001/06/21 21:17:15 warmerda
* New
*
*/
#include "cpl_port.h"
#if !defined(WIN32) && !defined(WIN32CE)
#include "cpl_vsi_private.h"
#include "cpl_string.h"
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
CPL_CVSID("$Id: cpl_vsil_unix_stdio_64.cpp,v 1.11 2006/03/27 15:24:41 fwarmerdam Exp $");
#if defined(UNIX_STDIO_64)
#ifndef VSI_FTELL64
#define VSI_FTELL64 ftell64
#endif
#ifndef VSI_FSEEK64
#define VSI_FSEEK64 fseek64
#endif
#ifndef VSI_FOPEN64
#define VSI_FOPEN64 fopen64
#endif
#ifndef VSI_STAT64
#define VSI_STAT64 stat64
#endif
#ifndef VSI_STAT64_T
#define VSI_STAT64_T stat64
#endif
#else /* not UNIX_STDIO_64 */
#ifndef VSI_FTELL64
#define VSI_FTELL64 ftell
#endif
#ifndef VSI_FSEEK64
#define VSI_FSEEK64 fseek
#endif
#ifndef VSI_FOPEN64
#define VSI_FOPEN64 fopen
#endif
#ifndef VSI_STAT64
#define VSI_STAT64 stat
#endif
#ifndef VSI_STAT64_T
#define VSI_STAT64_T stat
#endif
#endif /* ndef UNIX_STDIO_64 */
/************************************************************************/
/* ==================================================================== */
/* VSIUnixStdioFilesystemHandler */
/* ==================================================================== */
/************************************************************************/
class VSIUnixStdioFilesystemHandler : public VSIFilesystemHandler
{
public:
virtual VSIVirtualHandle *Open( const char *pszFilename,
const char *pszAccess);
virtual int Stat( const char *pszFilename, VSIStatBufL *pStatBuf );
virtual int Unlink( const char *pszFilename );
virtual int Rename( const char *oldpath, const char *newpath );
virtual int Mkdir( const char *pszDirname, long nMode );
virtual int Rmdir( const char *pszDirname );
virtual char **ReadDir( const char *pszDirname );
};
/************************************************************************/
/* ==================================================================== */
/* VSIUnixStdioHandle */
/* ==================================================================== */
/************************************************************************/
class VSIUnixStdioHandle : public VSIVirtualHandle
{
public:
FILE *fp;
virtual int Seek( vsi_l_offset nOffset, int nWhence );
virtual vsi_l_offset Tell();
virtual size_t Read( void *pBuffer, size_t nSize, size_t nMemb );
virtual size_t Write( const void *pBuffer, size_t nSize, size_t nMemb );
virtual int Eof();
virtual int Flush();
virtual int Close();
};
/************************************************************************/
/* Close() */
/************************************************************************/
int VSIUnixStdioHandle::Close()
{
return fclose( fp );
}
/************************************************************************/
/* Seek() */
/************************************************************************/
int VSIUnixStdioHandle::Seek( vsi_l_offset nOffset, int nWhence )
{
return( VSI_FSEEK64( fp, nOffset, nWhence ) );
}
/************************************************************************/
/* Tell() */
/************************************************************************/
vsi_l_offset VSIUnixStdioHandle::Tell()
{
return( VSI_FTELL64( fp ) );
}
/************************************************************************/
/* Flush() */
/************************************************************************/
int VSIUnixStdioHandle::Flush()
{
return fflush( fp );
}
/************************************************************************/
/* Read() */
/************************************************************************/
size_t VSIUnixStdioHandle::Read( void * pBuffer, size_t nSize, size_t nCount )
{
return fread( pBuffer, nSize, nCount, fp );
}
/************************************************************************/
/* Write() */
/************************************************************************/
size_t VSIUnixStdioHandle::Write( const void * pBuffer, size_t nSize,
size_t nCount )
{
return fwrite( pBuffer, nSize, nCount, fp );
}
/************************************************************************/
/* Eof() */
/************************************************************************/
int VSIUnixStdioHandle::Eof()
{
return feof( fp );
}
/************************************************************************/
/* ==================================================================== */
/* VSIUnixStdioFilesystemHandler */
/* ==================================================================== */
/************************************************************************/
/************************************************************************/
/* Open() */
/************************************************************************/
VSIVirtualHandle *
VSIUnixStdioFilesystemHandler::Open( const char *pszFilename,
const char *pszAccess )
{
FILE *fp = VSI_FOPEN64( pszFilename, pszAccess );
if( fp == NULL )
return NULL;
VSIUnixStdioHandle *poHandle = new VSIUnixStdioHandle;
poHandle->fp = fp;
return poHandle;
}
/************************************************************************/
/* Stat() */
/************************************************************************/
int VSIUnixStdioFilesystemHandler::Stat( const char * pszFilename,
VSIStatBufL * pStatBuf )
{
return( VSI_STAT64( pszFilename, pStatBuf ) );
}
/************************************************************************/
/* Unlink() */
/************************************************************************/
int VSIUnixStdioFilesystemHandler::Unlink( const char * pszFilename )
{
return unlink( pszFilename );
}
/************************************************************************/
/* Rename() */
/************************************************************************/
int VSIUnixStdioFilesystemHandler::Rename( const char *oldpath,
const char *newpath )
{
return rename( oldpath, newpath );
}
/************************************************************************/
/* Mkdir() */
/************************************************************************/
int VSIUnixStdioFilesystemHandler::Mkdir( const char * pszPathname,
long nMode )
{
return mkdir( pszPathname, nMode );
}
/************************************************************************/
/* Rmdir() */
/************************************************************************/
int VSIUnixStdioFilesystemHandler::Rmdir( const char * pszPathname )
{
return rmdir( pszPathname );
}
/************************************************************************/
/* ReadDir() */
/************************************************************************/
char **VSIUnixStdioFilesystemHandler::ReadDir( const char *pszPath )
{
DIR *hDir;
struct dirent *psDirEntry;
char **papszDir = NULL;
if (strlen(pszPath) == 0)
pszPath = ".";
if ( (hDir = opendir(pszPath)) != NULL )
{
while( (psDirEntry = readdir(hDir)) != NULL )
{
papszDir = CSLAddString(papszDir, psDirEntry->d_name);
}
closedir( hDir );
}
else
{
/* Should we generate an error???
* For now we'll just return NULL (at the end of the function)
*/
}
return papszDir;
}
/************************************************************************/
/* VSIInstallLargeFileHandler() */
/************************************************************************/
void VSIInstallLargeFileHandler()
{
VSIFileManager::InstallHandler( string(""),
new VSIUnixStdioFilesystemHandler );
}
#endif /* ndef WIN32 */