Welcome to mirror list, hosted at ThFree Co, Russian Federation.

storage.c « intern « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6b45c6f1cb32a691310ff6970c392e7c249adb41 (plain)
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
 * ***** 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 *****
 * Reorganised mar-01 nzc
 * Some really low-level file thingies.
 */

/** \file blender/blenlib/intern/storage.c
 *  \ingroup bli
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>

#ifndef WIN32
#  include <dirent.h>
#endif

#include <time.h>
#include <sys/stat.h>

#if defined(__sun__) || defined(__sun) || defined(__NetBSD__)
#  include <sys/statvfs.h> /* Other modern unix os's should probably use this also */
#elif !defined(__FreeBSD__) && !defined(linux) && (defined(__sparc) || defined(__sparc__))
#  include <sys/statfs.h>
#endif

#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
#  include <sys/param.h>
#  include <sys/mount.h>
#endif

#if defined(linux) || defined(__CYGWIN32__) || defined(__hpux) || defined(__GNU__) || defined(__GLIBC__)
#include <sys/vfs.h>
#endif

#ifdef __APPLE__
   /* For statfs */
#  include <sys/param.h>
#  include <sys/mount.h>
#endif /* __APPLE__ */


#include <fcntl.h>
#include <string.h>  /* strcpy etc.. */

#ifdef WIN32
#  include <io.h>
#  include <direct.h>
#  include <limits.h>  /* PATH_MAX */
#  include "BLI_winstuff.h"
#  include "utfconv.h"
#else
#  include <sys/ioctl.h>
#  include <unistd.h>
#  include <pwd.h>
#endif

/* lib includes */
#include "MEM_guardedalloc.h"

#include "DNA_listBase.h"

#include "BLI_listbase.h"
#include "BLI_linklist.h"
#include "BLI_string.h"
#include "BLI_fileops.h"
#include "BLI_fileops_types.h"
#include "BLI_path_util.h"

#include "../imbuf/IMB_imbuf.h"

/**
 * Copies the current working directory into *dir (max size maxncpy), and
 * returns a pointer to same.
 *
 * \note can return NULL when the size is not big enough
 */
char *BLI_current_working_dir(char *dir, const size_t maxncpy)
{
	const char *pwd = getenv("PWD");
	if (pwd) {
		BLI_strncpy(dir, pwd, maxncpy);
		return dir;
	}

	return getcwd(dir, maxncpy);
}

/*
 * Ordering function for sorting lists of files/directories. Returns -1 if
 * entry1 belongs before entry2, 0 if they are equal, 1 if they should be swapped.
 */
static int bli_compare(struct direntry *entry1, struct direntry *entry2)
{
	/* type is equal to stat.st_mode */

	/* directories come before non-directories */
	if (S_ISDIR(entry1->type)) {
		if (S_ISDIR(entry2->type) == 0) return (-1);
	}
	else {
		if (S_ISDIR(entry2->type)) return (1);
	}
	/* non-regular files come after regular files */
	if (S_ISREG(entry1->type)) {
		if (S_ISREG(entry2->type) == 0) return (-1);
	}
	else {
		if (S_ISREG(entry2->type)) return (1);
	}
	/* arbitrary, but consistent, ordering of different types of non-regular files */
	if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
	if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);

	/* OK, now we know their S_IFMT fields are the same, go on to a name comparison */
	/* make sure "." and ".." are always first */
	if (strcmp(entry1->relname, ".") == 0) return (-1);
	if (strcmp(entry2->relname, ".") == 0) return (1);
	if (strcmp(entry1->relname, "..") == 0) return (-1);
	if (strcmp(entry2->relname, "..") == 0) return (1);

	return (BLI_natstrcmp(entry1->relname, entry2->relname));
}

/**
 * Returns the number of free bytes on the volume containing the specified pathname. */
/* Not actually used anywhere.
 */
double BLI_dir_free_space(const char *dir)
{
#ifdef WIN32
	DWORD sectorspc, bytesps, freec, clusters;
	char tmp[4];
	
	tmp[0] = '\\'; tmp[1] = 0; /* Just a failsafe */
	if (dir[0] == '/' || dir[0] == '\\') {
		tmp[0] = '\\';
		tmp[1] = 0;
	}
	else if (dir[1] == ':') {
		tmp[0] = dir[0];
		tmp[1] = ':';
		tmp[2] = '\\';
		tmp[3] = 0;
	}

	GetDiskFreeSpace(tmp, &sectorspc, &bytesps, &freec, &clusters);

	return (double) (freec * bytesps * sectorspc);
#else

#if defined(__sun__) || defined(__sun) || defined(__NetBSD__)
	struct statvfs disk;
#else
	struct statfs disk;
#endif
	char name[FILE_MAXDIR], *slash;
	int len = strlen(dir);
	
	if (len >= FILE_MAXDIR) /* path too long */
		return -1;
	
	strcpy(name, dir);

	if (len) {
		slash = strrchr(name, '/');
		if (slash) slash[1] = 0;
	}
	else {
		strcpy(name, "/");
	}

#if defined(__FreeBSD__) || defined(linux) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__GNU__) || defined(__GLIBC__)
	if (statfs(name, &disk)) return(-1);
#endif

#if defined(__sun__) || defined(__sun) || defined(__NetBSD__)
	if (statvfs(name, &disk)) return(-1);
#elif !defined(__FreeBSD__) && !defined(linux) && (defined(__sparc) || defined(__sparc__))
	/* WARNING - This may not be supported by geeneric unix os's - Campbell */
	if (statfs(name, &disk, sizeof(struct statfs), 0)) return(-1);
#endif

	return ( ((double) disk.f_bsize) * ((double) disk.f_bfree));
#endif
}

struct BuildDirCtx {
	struct direntry *files; /* array[nrfiles] */
	int nrfiles;
};

/**
 * Scans the directory named *dirname and appends entries for its contents to files.
 */
static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname)
{
	struct ListBase dirbase = {NULL, NULL};
	int newnum = 0;
	DIR *dir;

	if ((dir = opendir(dirname)) != NULL) {

		const struct dirent *fname;
		while ((fname = readdir(dir)) != NULL) {
			struct dirlink * const dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
			if (dlink != NULL) {
				dlink->name = BLI_strdup(fname->d_name);
				BLI_addhead(&dirbase, dlink);
				newnum++;
			}
		}

		if (newnum) {

			if (dir_ctx->files) {
				void * const tmp = realloc(dir_ctx->files, (dir_ctx->nrfiles + newnum) * sizeof(struct direntry));
				if (tmp) {
					dir_ctx->files = (struct direntry *)tmp;
				}
				else { /* realloc fail */
					free(dir_ctx->files);
					dir_ctx->files = NULL;
				}
			}
			
			if (dir_ctx->files == NULL)
				dir_ctx->files = (struct direntry *)malloc(newnum * sizeof(struct direntry));

			if (dir_ctx->files) {
				struct dirlink * dlink = (struct dirlink *) dirbase.first;
				struct direntry *file = &dir_ctx->files[dir_ctx->nrfiles];
				while (dlink) {
					char fullname[PATH_MAX];
					memset(file, 0, sizeof(struct direntry));
					file->relname = dlink->name;
					file->path = BLI_strdupcat(dirname, dlink->name);
					BLI_join_dirfile(fullname, sizeof(fullname), dirname, dlink->name);
// use 64 bit file size, only needed for WIN32 and WIN64. 
// Excluding other than current MSVC compiler until able to test
#ifdef WIN32
					{
						wchar_t *name_16 = alloc_utf16_from_8(fullname, 0);
#if defined(_MSC_VER) && (_MSC_VER >= 1500)
						_wstat64(name_16, &file->s);
#elif defined(__MINGW32__)
						_stati64(fullname, &file->s);
#endif
						free(name_16);
					}

#else
					stat(fullname, &file->s);
#endif
					file->type = file->s.st_mode;
					file->flags = 0;
					dir_ctx->nrfiles++;
					file++;
					dlink = dlink->next;
				}
			}
			else {
				printf("Couldn't get memory for dir\n");
				exit(1);
			}

			BLI_freelist(&dirbase);
			if (dir_ctx->files) {
				qsort(dir_ctx->files, dir_ctx->nrfiles, sizeof(struct direntry), (int (*)(const void *, const void *))bli_compare);
			}
		}
		else {
			printf("%s empty directory\n", dirname);
		}

		closedir(dir);
	}
	else {
		printf("%s non-existant directory\n", dirname);
	}
}

/**
 * Fills in the "mode[123]", "size" and "string" fields in the elements of the files
 * array with descriptive details about each item. "string" will have a format similar to "ls -l".
 */
static void bli_adddirstrings(struct BuildDirCtx *dir_ctx)
{
	const char *types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
	/* symbolic display, indexed by mode field value */
	int num;
#ifdef WIN32
	__int64 st_size;
#else
	off_t st_size;
	int mode;
#endif
	
	struct direntry *file;
	struct tm *tm;
	time_t zero = 0;
	
	for (num = 0, file = dir_ctx->files; num < dir_ctx->nrfiles; num++, file++) {


		/* Mode */
#ifdef WIN32
		BLI_strncpy(file->mode1, types[0], sizeof(file->mode1));
		BLI_strncpy(file->mode2, types[0], sizeof(file->mode2));
		BLI_strncpy(file->mode3, types[0], sizeof(file->mode3));
#else
		mode = file->s.st_mode;

		BLI_strncpy(file->mode1, types[(mode & 0700) >> 6], sizeof(file->mode1));
		BLI_strncpy(file->mode2, types[(mode & 0070) >> 3], sizeof(file->mode2));
		BLI_strncpy(file->mode3, types[(mode & 0007)],      sizeof(file->mode3));
		
		if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2] == '-')) file->mode2[2] = 'l';

		if (mode & (S_ISUID | S_ISGID)) {
			if (file->mode1[2] == 'x') file->mode1[2] = 's';
			else file->mode1[2] = 'S';

			if (file->mode2[2] == 'x') file->mode2[2] = 's';
		}

		if (mode & S_ISVTX) {
			if (file->mode3[2] == 'x') file->mode3[2] = 't';
			else file->mode3[2] = 'T';
		}
#endif


		/* User */
#ifdef WIN32
		strcpy(file->owner, "user");
#else
		{
			struct passwd *pwuser;
			pwuser = getpwuid(file->s.st_uid);
			if (pwuser) {
				BLI_strncpy(file->owner, pwuser->pw_name, sizeof(file->owner));
			}
			else {
				BLI_snprintf(file->owner, sizeof(file->owner), "%d", file->s.st_uid);
			}
		}
#endif


		/* Time */
		tm = localtime(&file->s.st_mtime);
		// prevent impossible dates in windows
		if (tm == NULL) tm = localtime(&zero);
		strftime(file->time, sizeof(file->time), "%H:%M", tm);
		strftime(file->date, sizeof(file->date), "%d-%b-%y", tm);


		/* Size */
		/*
		 * Seems st_size is signed 32-bit value in *nix and Windows.  This
		 * will buy us some time until files get bigger than 4GB or until
		 * everyone starts using __USE_FILE_OFFSET64 or equivalent.
		 */
		st_size = file->s.st_size;

		/* FIXME: Either change decimal prefixes to binary ones
		 * <http://en.wikipedia.org/wiki/Binary_prefix>, or change
		 * divisor factors from 1024 to 1000. */
		if (st_size > 1024 * 1024 * 1024) {
			BLI_snprintf(file->size, sizeof(file->size), "%.2f GB", ((double)st_size) / (1024 * 1024 * 1024));
		}
		else if (st_size > 1024 * 1024) {
			BLI_snprintf(file->size, sizeof(file->size), "%.1f MB", ((double)st_size) / (1024 * 1024));
		}
		else if (st_size > 1024) {
			BLI_snprintf(file->size, sizeof(file->size), "%d KB", (int)(st_size / 1024));
		}
		else {
			BLI_snprintf(file->size, sizeof(file->size), "%d B", (int)st_size);
		}
	}
}

/**
 * Scans the contents of the directory named *dirname, and allocates and fills in an
 * array of entries describing them in *filelist. The length of the array is the function result.
 */
unsigned int BLI_dir_contents(const char *dirname,  struct direntry **filelist)
{
	struct BuildDirCtx dir_ctx;

	dir_ctx.nrfiles = 0;
	dir_ctx.files = NULL;

	bli_builddir(&dir_ctx, dirname);
	bli_adddirstrings(&dir_ctx);

	if (dir_ctx.files) {
		*filelist = dir_ctx.files;
	}
	else {
		// keep blender happy. Blender stores this in a variable
		// where 0 has special meaning.....
		*filelist = malloc(sizeof(struct direntry));
	}

	return dir_ctx.nrfiles;
}

/* frees storage for an array of direntries, including the array itself. */
void BLI_free_filelist(struct direntry *filelist, unsigned int nrentries)
{
	unsigned int i;
	for (i = 0; i < nrentries; ++i) {
		struct direntry * const entry = filelist + i;
		if (entry->image) {
			IMB_freeImBuf(entry->image);
		}
		if (entry->relname)
			MEM_freeN(entry->relname);
		if (entry->path)
			MEM_freeN(entry->path);
		/* entry->poin assumed not to point to anything needing freeing here */
	}

	free(filelist);
}


/**
 * Returns the file size of an opened file descriptor.
 */
size_t BLI_file_descriptor_size(int file)
{
	struct stat st;
	if ((file < 0) || (fstat(file, &st) == -1))
		return -1;
	return st.st_size;
}

/**
 * Returns the size of a file.
 */
size_t BLI_file_size(const char *path)
{
	struct stat stats;
	if (BLI_stat(path, &stats) == -1)
		return -1;
	return stats.st_size;
}

/**
 * Returns the st_mode from statting the specified path name, or 0 if it couldn't be statted
 * (most likely doesn't exist or no access).
 */
int BLI_exists(const char *name)
{
#if defined(WIN32) 
#ifndef __MINGW32__
	struct _stat64i32 st;
#else
	struct _stati64 st;
#endif
	/* in Windows stat doesn't recognize dir ending on a slash
	 * To not break code where the ending slash is expected we
	 * don't mess with the argument name directly here - elubie */
	wchar_t *tmp_16 = alloc_utf16_from_8(name, 0);
	int len, res;
	unsigned int old_error_mode;

	len = wcslen(tmp_16);
	if (len > 3 && (tmp_16[len - 1] == L'\\' || tmp_16[len - 1] == L'/') )
		tmp_16[len - 1] = '\0';

	/* change error mode so user does not get a "no disk in drive" popup
	 * when looking for a file on an empty CD/DVD drive */
	old_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);

#ifndef __MINGW32__
	res = _wstat(tmp_16, &st);
#else
	res = _wstati64(tmp_16, &st);
#endif

	SetErrorMode(old_error_mode);

	free(tmp_16);
	if (res == -1) return(0);
#else
	struct stat st;
	if (stat(name, &st)) return(0);
#endif
	return(st.st_mode);
}


#ifdef WIN32
int BLI_stat(const char *path, struct stat *buffer)
{
	int r;
	UTF16_ENCODE(path);

	/* workaround error in MinGW64 headers, normally, a wstat should work */
	#ifndef __MINGW64__
	r = _wstat(path_16, buffer);
	#else
	r = _wstati64(path_16, buffer);
	#endif

	UTF16_UN_ENCODE(path);
	return r;
}
#else
int BLI_stat(const char *path, struct stat *buffer)
{
	return stat(path, buffer);
}
#endif

/**
 * Does the specified path point to a directory?
 * \note Would be better in fileops.c except that it needs stat.h so add here
 */
bool BLI_is_dir(const char *file)
{
	return S_ISDIR(BLI_exists(file));
}

/**
 * Does the specified path point to a non-directory?
 */
bool BLI_is_file(const char *path)
{
	const int mode = BLI_exists(path);
	return (mode && !S_ISDIR(mode));
}

/**
 * Reads the contents of a text file and returns the lines in a linked list.
 */
LinkNode *BLI_file_read_as_lines(const char *name)
{
	FILE *fp = BLI_fopen(name, "r");
	LinkNode *lines = NULL;
	char *buf;
	size_t size;

	if (!fp) return NULL;
		
	fseek(fp, 0, SEEK_END);
	size = (size_t)ftell(fp);
	fseek(fp, 0, SEEK_SET);

	buf = MEM_mallocN(size, "file_as_lines");
	if (buf) {
		size_t i, last = 0;
		
		/*
		 * size = because on win32 reading
		 * all the bytes in the file will return
		 * less bytes because of crnl changes.
		 */
		size = fread(buf, 1, size, fp);
		for (i = 0; i <= size; i++) {
			if (i == size || buf[i] == '\n') {
				char *line = BLI_strdupn(&buf[last], i - last);

				BLI_linklist_prepend(&lines, line);
				/* faster to build singly-linked list in reverse order */
				/* alternatively, could process buffer in reverse order so
				 * list ends up right way round to start with */
				last = i + 1;
			}
		}
		
		MEM_freeN(buf);
	}
	
	fclose(fp);

	/* get them the right way round */
	BLI_linklist_reverse(&lines);
	return lines;
}

/*
 * Frees memory from a previous call to BLI_file_read_as_lines.
 */
void BLI_file_free_lines(LinkNode *lines)
{
	BLI_linklist_freeN(lines);
}

/** is file1 older then file2 */
bool BLI_file_older(const char *file1, const char *file2)
{
#ifdef WIN32
#ifndef __MINGW32__
	struct _stat st1, st2;
#else
	struct _stati64 st1, st2;
#endif

	UTF16_ENCODE(file1);
	UTF16_ENCODE(file2);
	
#ifndef __MINGW32__
	if (_wstat(file1_16, &st1)) return false;
	if (_wstat(file2_16, &st2)) return false;
#else
	if (_wstati64(file1_16, &st1)) return false;
	if (_wstati64(file2_16, &st2)) return false;
#endif


	UTF16_UN_ENCODE(file2);
	UTF16_UN_ENCODE(file1);
#else
	struct stat st1, st2;

	if (stat(file1, &st1)) return false;
	if (stat(file2, &st2)) return false;
#endif
	return (st1.st_mtime < st2.st_mtime);
}