forked from PolMine/RcppCWB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileutils.c
More file actions
486 lines (439 loc) · 12.6 KB
/
Copy pathfileutils.c
File metadata and controls
486 lines (439 loc) · 12.6 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* IMS Open Corpus Workbench (CWB)
* Copyright (C) 1993-2006 by IMS, University of Stuttgart
* Copyright (C) 2007- by the respective contributers (see file AUTHORS)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details (in the file "COPYING", or available via
* WWW at http://www.gnu.org/copyleft/gpl.html).
*/
void Rprintf(const char *, ...);
#include <sys/stat.h>
#include <fcntl.h>
#include <glib.h>
#ifndef __MINGW__
#include <signal.h> /* added by Andreas Blaette */
#include <sys/socket.h> /* added by Andreas Blaette */
#endif
#include "globals.h"
#include "fileutils.h"
/**
* Gets the size of the specified file; returns EOF for error.
*
* @param filename The file to size up.
* @return Size of file in bytes (or EOF if call to stat() failed)
*/
off_t
file_length(char *filename)
{
struct stat stat_buf;
if (EOF == stat(filename, &stat_buf))
return EOF;
else
return stat_buf.st_size;
}
/**
* Gets the size of the specified file; returns EOF for error.
*
* As file_length, but the file is specified by file handle, not name.
*
* @see file_length
* @param fh The file to size up.
* @return Size of file in bytes.
*/
off_t
fh_file_length(FILE *fh)
{
struct stat stat_buf;
if (EOF == fstat(fileno(fh), &stat_buf))
return EOF;
else
return stat_buf.st_size;
}
/**
* Gets the size of the specified file; returns EOF for error.
*
* As file_length, but the file is specified by number, not name.
*
* @see file_length
* @param fd The file to size up.
* @return Size of file in bytes.
*/
off_t
fd_file_length(int fd)
{
struct stat stat_buf;
if (EOF == fstat(fd, &stat_buf))
return EOF ;
else
return stat_buf.st_size;
}
/**
* Gets the size of the specified file; returns EOF for error.
*
* Duplicates functionality of file_length, but return is long
* instead of off_t.
*
* @see file_length
* @param fname The file to size up.
* @return Size of file in bytes.
*/
long
fprobe(char *fname)
{
struct stat stat_buf;
if (EOF == stat(fname, &stat_buf))
return (long) EOF;
else
return stat_buf.st_size;
}
/**
* Checks whether the specified path indicates a directory.
*
* @param path Path to check.
* @return Boolean. (Also false if there's an error.)
*/
int
is_directory(char *path)
{
struct stat stat_buf;
if (0 > stat(path, &stat_buf))
return 0;
else
return S_ISDIR(stat_buf.st_mode) ;
}
/**
* Checks whether the specified path indicates a regular file.
*
* @param path Path to check.
* @return Boolean. (Also false if there's an error.)
*/
int
is_file(char *path)
{
struct stat stat_buf;
if (0 > stat(path, &stat_buf))
return 0;
else
return S_ISREG(stat_buf.st_mode);
}
/**
* Checks whether the specified path indicates a link.
*
* Note this function always returns false in Windows, because Windows
* doesn't have Unix-style links. (.lnk files don't count.)
*
* As of 2020: Windows 10 does have symlinks. They were added in Vista but
* required elevated permissions to work with. Windows 10 makes it possible
* to use symlinks without elevated permissions (but it might be necessary
* to dop some configuration twiddling to get that to work; it's not 100%
* clear). Even so, though, it's not clear whether stat() in MinGW will
* correctly report what is and is not a symlink.
*
* See https://blogs.windows.com/windowsdeveloper/2016/12/02/symlinks-windows-10/
*
* @param path Path to check.
* @return Boolean. (Also false if there's an error.)
*/
int
is_link(char *path)
{
#ifndef __MINGW__
struct stat stat_buf;
if (0 > stat(path, &stat_buf))
return 0;
else
return S_ISLNK(stat_buf.st_mode);
#else
return 0;
#endif
}
/**
* Implementation of automagic I/O streams.
*
* In order to make streams completely transparent to the caller
* and return them as standard FILE* objects, the CL keeps a list
* of all open streams with the necessary metadata information.
*/
CLStream open_streams;
/**
* SIGPIPE handler and global status variable
*/
int cl_broken_pipe = 0;
#ifndef __MINGW__
static void
cl_handle_sigpipe(int signum)
{
#ifndef __MINGW__
cl_broken_pipe = 1;
/* Rprintf("Handle broken pipe signal\n"); */
if (SIG_ERR == signal(SIGPIPE, cl_handle_sigpipe))
perror("CL: Can't reinstall SIGPIPE handler (ignored)"); /* Is this still necessary on modern platforms? */
#endif
}
#endif
/** check whether stream type involves a pipe */
#define STREAM_IS_PIPE(type) (type == CL_STREAM_PIPE || type == CL_STREAM_GZIP || type == CL_STREAM_BZIP2)
/**
* Open stream of specified (or guessed) type for reading or writing
*
* I/O streams opened with this function must always be closed with cl_close_stream()!
*
* @param filename Filename or shell command
* @param mode Open for reading (CL_STREAM_READ) or writing (CL_STREAM_WRITE)
* @param type Type of stream (see above), or guess automagically from [filename] (CL_STREAM_MAGIC)
*
* @return Standard C stream, or NULL on error (with details available via cl_errno or cl_error())
*/
FILE *
cl_open_stream(const char *filename, int mode, int type)
{
char *point, *mode_spec;
int l = strlen(filename);
FILE *handle;
CLStream stream;
char command[2 * CL_MAX_FILENAME_LENGTH]; /* may be longer than CL_MAX_FILENAME_LENGTH */
char expanded_filename[2 * CL_MAX_FILENAME_LENGTH];
if (l > CL_MAX_FILENAME_LENGTH) {
Rprintf("CL: filename '%s' too long (limit: %d bytes)\n", filename, CL_MAX_FILENAME_LENGTH);
cl_errno = CDA_EBUFFER;
return NULL;
}
/* validate read/write/append mode */
switch (mode) {
case CL_STREAM_READ:
mode_spec = "r";
break;
case CL_STREAM_WRITE:
mode_spec = "w";
break;
case CL_STREAM_APPEND:
mode_spec = "a";
break;
#ifdef __MINGW__
/* Binary modes only take effect on Windows.
* Their only effect is to add "b" to the spec,
* thereafter, they collapse back to the
* same thing as the constants without _BIN.
*/
case CL_STREAM_READ_BIN:
mode_spec = "rb";
mode = CL_STREAM_READ;
break;
case CL_STREAM_WRITE_BIN:
mode_spec = "wb";
mode = CL_STREAM_WRITE;
break;
case CL_STREAM_APPEND_BIN:
mode_spec = "ab";
mode = CL_STREAM_APPEND;
break;
#endif
default:
Rprintf("CL: invalid I/O stream mode = %d\n", mode);
cl_errno = CDA_EARGS;
return NULL;
}
/* apply magic */
if (type == CL_STREAM_MAGIC || type == CL_STREAM_MAGIC_NOPIPE) {
/* expand ~/ or $HOME/ */
if (0 == strncmp(filename, "~/", 2) || 0 == strncasecmp(filename, "$home/", 6)) {
char *home = getenv("HOME");
if (home && home[0] != '\0') {
filename = (filename[0] == '~') ? filename + 2 : filename + 6;
snprintf(expanded_filename, 2 * CL_MAX_FILENAME_LENGTH, "%s/%s", home, filename);
filename = expanded_filename;
l = strlen(filename); /* don't forget to update string length */
}
}
/* guess type of stream */
type = CL_STREAM_FILE; /* default */
/* "-" = STDIN or STDOUT */
if (cl_str_is(filename, "-"))
type = CL_STREAM_STDIO;
else {
point = (char *) filename + strspn(filename, " \t");
/* " | ..." = read or write pipe to shell command */
if (*point == '|') {
if (type == CL_STREAM_MAGIC_NOPIPE) {
cl_errno = CDA_EACCESS;
return NULL;
}
type = CL_STREAM_PIPE;
point++;
filename = point + strspn(point, " \t");
}
/* *.gz = gzip-compressed file */
else if (l > 3 && 0 == strcasecmp(filename + l - 3, ".gz"))
type = CL_STREAM_GZIP;
/* *.bz2 = bzip2-compressed file */
else if (l > 4 && 0 == strcasecmp(filename + l - 4, ".bz2"))
type = CL_STREAM_BZIP2;
}
}
/* file access errors are delayed when reading/writing through pipe to gzip or bzip2,
* so check first that we can access the file in the appropriate mode */
if (type == CL_STREAM_GZIP || type == CL_STREAM_BZIP2) {
handle = fopen(filename, mode_spec);
if (handle == NULL) {
cl_errno = CDA_EPOSIX;
return NULL;
}
fclose(handle);
handle = NULL;
}
/* open file or pipe */
switch (type) {
case CL_STREAM_FILE:
handle = fopen(filename, mode_spec);
break;
case CL_STREAM_GZIP:
point = g_shell_quote(filename);
if (mode == CL_STREAM_APPEND) {
sprintf(command, "gzip >> %s", point);
mode_spec = (mode_spec[1] == 'b' ? "wb" : "w");
}
else if (mode == CL_STREAM_WRITE)
sprintf(command, "gzip > %s", point);
else
sprintf(command, "gzip -cd %s", point);
handle = popen(command, mode_spec);
g_free(point);
break;
case CL_STREAM_BZIP2:
point = g_shell_quote(filename);
if (mode == CL_STREAM_APPEND) {
sprintf(command, "bzip2 >> %s", point);
mode_spec = (mode_spec[1] == 'b' ? "wb" : "w");
}
else if (mode == CL_STREAM_WRITE)
sprintf(command, "bzip2 > %s", point);
else
sprintf(command, "bzip2 -cd %s", point);
handle = popen(command, mode_spec);
g_free(point);
break;
case CL_STREAM_PIPE:
if (mode == CL_STREAM_APPEND)
mode_spec = (mode_spec[1] == 'b' ? "wb" : "w");
handle = popen(filename, mode_spec);
break;
case CL_STREAM_STDIO:
handle = (mode == CL_STREAM_READ) ? stdin : stdout;
break;
default:
Rprintf("CL: invalid I/O stream type = %d\n", type);
cl_errno = CDA_EARGS;
return NULL;
}
if (handle == NULL) {
cl_errno = CDA_EPOSIX;
return NULL;
}
/* add to list of managed streams */
stream = (CLStream) cl_malloc(sizeof(struct CLStream));
stream->handle = handle;
stream->mode = mode;
stream->type = type;
stream->next = open_streams;
open_streams = stream;
/* install SIGPIPE handler if opening a pipe stream */
#ifndef __MINGW__
if (STREAM_IS_PIPE(type))
if (SIG_ERR == signal(SIGPIPE, cl_handle_sigpipe))
perror("CL: can't install SIGPIPE handler (ignored)");
#endif
cl_broken_pipe = 0;
cl_errno = CDA_OK;
return handle;
}
/**
* Close I/O stream.
*
* This function can only be used for FILE* objects opened with cl_open_stream()!
*
* @param handle An I/O stream that has been opened with cl_open_stream()
* @return 0 on success, otherwise the error code returned by fclose() or pclose()
*/
int
cl_close_stream(FILE *handle)
{
CLStream stream, point;
#ifndef __MINGW__
int was_pipe;
#endif
int result = 0;
for (stream = open_streams ; stream ; stream = stream->next)
if (stream->handle == handle)
break;
if (!stream) {
Rprintf("CL: attempt to close non-managed I/O stream with cl_close_stream() [ignored]\n");
return CDA_EATTTYPE;
}
/* close stream appropriately, depending on type */
switch (stream->type) {
case CL_STREAM_STDIO:
break;
case CL_STREAM_FILE:
result = fclose(stream->handle);
break;
case CL_STREAM_GZIP:
case CL_STREAM_BZIP2:
case CL_STREAM_PIPE:
result = pclose(stream->handle);
#ifndef __MINGW__
was_pipe = 1;
#endif
break;
default:
Rprintf("CL: internal error, managed I/O stream has invalid type = %d\n", stream->type);
exit(1);
}
/* remove stream from list */
if (stream == open_streams)
open_streams = stream->next;
else {
for (point = open_streams; point->next != stream; point = point->next)
/* pass */ ;
point->next = stream->next;
}
cl_free(stream);
/* if stream was a pipe, check whether we can uninstall the SIGPIPE handler */
#ifndef __MINGW__
if (was_pipe) {
int any_pipe = 0;
for (point = open_streams; point; point = point->next)
if (STREAM_IS_PIPE(point->type))
any_pipe = 1;
/* if last pipe stream closed, uninstall SIGPIPE handler */
if (!any_pipe)
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
perror("CL: can't uninstall SIGPIPE handler (ignored)");
}
#endif
cl_broken_pipe = 0;
cl_errno = result ? CDA_EPOSIX : CDA_OK;
return result;
}
/**
* Tests whether a file handle has been opened via cl_test_stream.
*
* @param handle The FILE pointer to check.
* @return Boolean. True iff the "cl_stream" knows about this handle.
*/
int
cl_test_stream(FILE *handle)
{
CLStream stream;
for (stream = open_streams ; stream ; stream = stream->next)
if (stream->handle == handle)
return 1;
return 0;
}