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
|
/*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* 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
* of the License, 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "zlib.h"
#ifdef WIN32
#include <io.h>
#include "BLI_winstuff.h"
#else
#include <unistd.h> // for read close
#include <sys/param.h>
#endif
#include "BLI_blenlib.h"
#include "BKE_utildefines.h"
#include "BLO_sys_types.h" // for intptr_t support
/* implementations: */
char *first_slash(char *string) {
char *ffslash, *fbslash;
ffslash= strchr(string, '/');
fbslash= strchr(string, '\\');
if (!ffslash) return fbslash;
else if (!fbslash) return ffslash;
if ((intptr_t)ffslash < (intptr_t)fbslash) return ffslash;
else return fbslash;
}
char *BLI_last_slash(const char *string) {
char *lfslash, *lbslash;
lfslash= strrchr(string, '/');
lbslash= strrchr(string, '\\');
if (!lfslash) return lbslash;
else if (!lbslash) return lfslash;
if ((intptr_t)lfslash < (intptr_t)lbslash) return lbslash;
else return lfslash;
}
/* adds a slash if there isnt one there alredy */
int BLI_add_slash(char *string) {
int len = strlen(string);
#ifdef WIN32
if (len==0 || string[len-1]!='\\') {
string[len] = '\\';
string[len+1] = '\0';
return len+1;
}
#else
if (len==0 || string[len-1]!='/') {
string[len] = '/';
string[len+1] = '\0';
return len+1;
}
#endif
return len;
}
/* removes a slash if there is one */
void BLI_del_slash(char *string) {
int len = strlen(string);
while (len) {
#ifdef WIN32
if (string[len-1]=='\\') {
#else
if (string[len-1]=='/') {
#endif
string[len-1] = '\0';
len--;
} else {
break;
}
}
}
/* gzip the file in from and write it to "to".
return -1 if zlib fails, -2 if the originating file does not exist
note: will remove the "from" file
*/
int BLI_gzip(char *from, char *to) {
char buffer[10240];
int file;
int readsize = 0;
int rval= 0, err;
gzFile gzfile;
gzfile = gzopen(to, "wb");
if(gzfile == NULL)
return -1;
file = open(from, O_BINARY|O_RDONLY);
if(file < 0)
return -2;
while(1) {
readsize = read(file, buffer, 10240);
if(readsize < 0) {
rval= -2; /* error happened in reading */
fprintf(stderr, "Error reading file %s: %s.\n", from, strerror(errno));
break;
}
else if(readsize == 0)
break; /* done reading */
if(gzwrite(gzfile, buffer, readsize) <= 0) {
rval= -1; /* error happened in writing */
fprintf(stderr, "Error writing gz file %s: %s.\n", to, gzerror(gzfile, &err));
break;
}
}
gzclose(gzfile);
close(file);
return rval;
}
/* return 1 when file can be written */
int BLI_is_writable(char *filename)
{
int file;
/* first try to open without creating */
file = open(filename, O_BINARY | O_RDWR, 0666);
if (file < 0) {
/* now try to open and create. a test without actually
* creating a file would be nice, but how? */
file = open(filename, O_BINARY | O_RDWR | O_CREAT, 0666);
if(file < 0) {
return 0;
}
else {
/* success, delete the file we create */
close(file);
BLI_delete(filename, 0, 0);
return 1;
}
}
else {
close(file);
return 1;
}
}
int BLI_touch(const char *file)
{
FILE *f = fopen(file,"r+b");
if (f != NULL) {
char c = getc(f);
rewind(f);
putc(c,f);
} else {
f = fopen(file,"wb");
}
if (f) {
fclose(f);
return 1;
}
return 0;
}
#ifdef WIN32
static char str[MAXPATHLEN+12];
int BLI_delete(char *file, int dir, int recursive) {
int err;
if (recursive) {
callLocalErrorCallBack("Recursive delete is unsupported on Windows");
err= 1;
} else if (dir) {
err= !RemoveDirectory(file);
if (err) printf ("Unable to remove directory");
} else {
err= !DeleteFile(file);
if (err) callLocalErrorCallBack("Unable to delete file");
}
return err;
}
int BLI_move(char *file, char *to) {
int err;
// windows doesn't support moveing to a directory
// it has to be 'mv filename filename' and not
// 'mv filename destdir'
strcpy(str, to);
// points 'to' to a directory ?
if (BLI_last_slash(str) == (str + strlen(str) - 1)) {
if (BLI_last_slash(file) != NULL) {
strcat(str, BLI_last_slash(file) + 1);
}
}
err= !MoveFile(file, str);
if (err) {
callLocalErrorCallBack("Unable to move file");
printf(" Move from '%s' to '%s' failed\n", file, str);
}
return err;
}
int BLI_copy_fileops(char *file, char *to) {
int err;
// windows doesn't support copying to a directory
// it has to be 'cp filename filename' and not
// 'cp filename destdir'
strcpy(str, to);
// points 'to' to a directory ?
if (BLI_last_slash(str) == (str + strlen(str) - 1)) {
if (BLI_last_slash(file) != NULL) {
strcat(str, BLI_last_slash(file) + 1);
}
}
err= !CopyFile(file,str,FALSE);
if (err) {
callLocalErrorCallBack("Unable to copy file!");
printf(" Copy from '%s' to '%s' failed\n", file, str);
}
return err;
}
int BLI_link(char *file, char *to) {
callLocalErrorCallBack("Linking files is unsupported on Windows");
return 1;
}
int BLI_exists(char *file) {
return (GetFileAttributes(file) != 0xFFFFFFFF);
}
void BLI_recurdir_fileops(char *dirname) {
char *lslash;
char tmp[MAXPATHLEN];
// First remove possible slash at the end of the dirname.
// This routine otherwise tries to create
// blah1/blah2/ (with slash) after creating
// blah1/blah2 (without slash)
strcpy(tmp, dirname);
lslash= BLI_last_slash(tmp);
if (lslash == tmp + strlen(tmp) - 1) {
*lslash = 0;
}
if (BLI_exists(tmp)) return;
lslash= BLI_last_slash(tmp);
if (lslash) {
/* Split about the last slash and recurse */
*lslash = 0;
BLI_recurdir_fileops(tmp);
}
if(dirname[0]) /* patch, this recursive loop tries to create a nameless directory */
if (!CreateDirectory(dirname, NULL))
callLocalErrorCallBack("Unable to create directory\n");
}
int BLI_rename(char *from, char *to) {
if (!BLI_exists(from)) return 0;
/* make sure the filenames are different (case insensitive) before removing */
if (BLI_exists(to) && BLI_strcasecmp(from, to))
if(BLI_delete(to, 0, 0)) return 1;
return rename(from, to);
}
#else /* The sane UNIX world */
/*
* but the sane UNIX world is tied to the interface, and the system
* timer, and... We implement a callback mechanism. The system will
* have to initialise the callback before the functions will work!
* */
static char str[MAXPATHLEN+12];
int BLI_delete(char *file, int dir, int recursive)
{
if(strchr(file, '"')) {
printf("Error: not deleted file %s because of quote!\n", file);
}
else {
if (recursive) {
sprintf(str, "/bin/rm -rf \"%s\"", file);
return system(str);
}
else if (dir) {
sprintf(str, "/bin/rmdir \"%s\"", file);
return system(str);
}
else {
return remove(file); //sprintf(str, "/bin/rm -f \"%s\"", file);
}
}
return -1;
}
int BLI_move(char *file, char *to) {
sprintf(str, "/bin/mv -f \"%s\" \"%s\"", file, to);
return system(str);
}
int BLI_copy_fileops(char *file, char *to) {
sprintf(str, "/bin/cp -rf \"%s\" \"%s\"", file, to);
return system(str);
}
int BLI_link(char *file, char *to) {
sprintf(str, "/bin/ln -f \"%s\" \"%s\"", file, to);
return system(str);
}
int BLI_exists(char *file) {
return BLI_exist(file);
}
void BLI_recurdir_fileops(char *dirname) {
char *lslash;
char tmp[MAXPATHLEN];
if (BLI_exists(dirname)) return;
strcpy(tmp, dirname);
lslash= BLI_last_slash(tmp);
if (lslash) {
/* Split about the last slash and recurse */
*lslash = 0;
BLI_recurdir_fileops(tmp);
}
mkdir(dirname, 0777);
}
int BLI_rename(char *from, char *to) {
if (!BLI_exists(from)) return 0;
if (BLI_exists(to)) if(BLI_delete(to, 0, 0)) return 1;
return rename(from, to);
}
#endif
|