2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
26 * Reorganised mar-01 nzc
27 * Some really low-level file thingies.
30 /** \file blender/blenlib/intern/storage.c
34 #include <sys/types.h>
40 #if defined(__NetBSD__) || defined(__DragonFly__) || defined(__HAIKU__)
41 /* Other modern unix os's should probably use this also */
42 # include <sys/statvfs.h>
43 # define USE_STATFS_STATVFS
46 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
48 # include <sys/param.h>
49 # include <sys/mount.h>
52 #if defined(__linux__) || defined(__hpux) || defined(__GNU__) || defined(__GLIBC__)
57 #include <string.h> /* strcpy etc.. */
62 # include "BLI_winstuff.h"
65 # include <sys/ioctl.h>
71 #include "MEM_guardedalloc.h"
73 #include "BLI_utildefines.h"
74 #include "BLI_linklist.h"
75 #include "BLI_string.h"
76 #include "BLI_fileops.h"
77 #include "BLI_fileops_types.h"
78 #include "BLI_path_util.h"
81 * Copies the current working directory into *dir (max size maxncpy), and
82 * returns a pointer to same.
84 * \note can return NULL when the size is not big enough
86 char *BLI_current_working_dir(char *dir, const size_t maxncpy)
88 const char *pwd = getenv("PWD");
90 size_t srclen = BLI_strnlen(pwd, maxncpy);
91 if (srclen != maxncpy) {
92 memcpy(dir, pwd, srclen + 1);
100 return getcwd(dir, maxncpy);
104 * Returns the number of free bytes on the volume containing the specified pathname. */
105 /* Not actually used anywhere.
107 double BLI_dir_free_space(const char *dir)
110 DWORD sectorspc, bytesps, freec, clusters;
113 tmp[0] = '\\'; tmp[1] = 0; /* Just a failsafe */
114 if (dir[0] == '/' || dir[0] == '\\') {
118 else if (dir[1] == ':') {
125 GetDiskFreeSpace(tmp, §orspc, &bytesps, &freec, &clusters);
127 return (double) (freec * bytesps * sectorspc);
130 #ifdef USE_STATFS_STATVFS
136 char name[FILE_MAXDIR], *slash;
137 int len = strlen(dir);
139 if (len >= FILE_MAXDIR) /* path too long */
145 slash = strrchr(name, '/');
146 if (slash) slash[1] = 0;
152 #if defined(USE_STATFS_STATVFS)
153 if (statvfs(name, &disk)) return -1;
154 #elif defined(USE_STATFS_4ARGS)
155 if (statfs(name, &disk, sizeof(struct statfs), 0)) return -1;
157 if (statfs(name, &disk)) return -1;
160 return ( ((double) disk.f_bsize) * ((double) disk.f_bfree));
166 * Returns the file size of an opened file descriptor.
168 size_t BLI_file_descriptor_size(int file)
171 if ((file < 0) || (fstat(file, &st) == -1))
177 * Returns the size of a file.
179 size_t BLI_file_size(const char *path)
182 if (BLI_stat(path, &stats) == -1)
184 return stats.st_size;
188 * Returns the st_mode from stat-ing the specified path name, or 0 if stat fails
189 * (most likely doesn't exist or no access).
191 int BLI_exists(const char *name)
195 wchar_t *tmp_16 = alloc_utf16_from_8(name, 1);
197 unsigned int old_error_mode;
199 len = wcslen(tmp_16);
200 /* in Windows #stat doesn't recognize dir ending on a slash
201 * so we remove it here */
202 if (len > 3 && (tmp_16[len - 1] == L'\\' || tmp_16[len - 1] == L'/')) {
203 tmp_16[len - 1] = '\0';
205 /* two special cases where the trailing slash is needed:
206 * 1. after the share part of a UNC path
207 * 2. after the C:\ when the path is the volume only
209 if ((len >= 3) && (tmp_16[0] == L'\\') && (tmp_16[1] == L'\\')) {
210 BLI_cleanup_unc_16(tmp_16);
213 if ((tmp_16[1] == L':') && (tmp_16[2] == L'\0')) {
219 /* change error mode so user does not get a "no disk in drive" popup
220 * when looking for a file on an empty CD/DVD drive */
221 old_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
223 res = BLI_wstat(tmp_16, &st);
225 SetErrorMode(old_error_mode);
228 if (res == -1) return(0);
231 BLI_assert(!BLI_path_is_rel(name));
232 if (stat(name, &st)) return(0);
239 int BLI_stat(const char *path, BLI_stat_t *buffer)
244 r = BLI_wstat(path_16, buffer);
246 UTF16_UN_ENCODE(path);
250 int BLI_wstat(const wchar_t *path, BLI_stat_t *buffer)
252 #if defined(_MSC_VER)
253 return _wstat64(path, buffer);
255 return _wstat(path, buffer);
259 int BLI_stat(const char *path, struct stat *buffer)
261 return stat(path, buffer);
266 * Does the specified path point to a directory?
267 * \note Would be better in fileops.c except that it needs stat.h so add here
269 bool BLI_is_dir(const char *file)
271 return S_ISDIR(BLI_exists(file));
275 * Does the specified path point to a non-directory?
277 bool BLI_is_file(const char *path)
279 const int mode = BLI_exists(path);
280 return (mode && !S_ISDIR(mode));
283 void *BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
285 FILE *fp = BLI_fopen(filepath, "r");
289 fseek(fp, 0L, SEEK_END);
290 const long int filelen = ftell(fp);
294 fseek(fp, 0L, SEEK_SET);
296 mem = MEM_mallocN(filelen + pad_bytes, __func__);
301 const long int filelen_read = fread(mem, 1, filelen, fp);
302 if ((filelen_read < 0) || ferror(fp)) {
308 if (filelen_read < filelen) {
309 mem = MEM_reallocN(mem, filelen_read + pad_bytes);
315 *r_size = filelen_read;
324 void *BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
326 FILE *fp = BLI_fopen(filepath, "rb");
330 fseek(fp, 0L, SEEK_END);
331 const long int filelen = ftell(fp);
335 fseek(fp, 0L, SEEK_SET);
337 mem = MEM_mallocN(filelen + pad_bytes, __func__);
342 const long int filelen_read = fread(mem, 1, filelen, fp);
343 if ((filelen_read != filelen) || ferror(fp)) {
349 *r_size = filelen_read;
359 * Reads the contents of a text file and returns the lines in a linked list.
361 LinkNode *BLI_file_read_as_lines(const char *name)
363 FILE *fp = BLI_fopen(name, "r");
364 LinkNodePair lines = {NULL, NULL};
368 if (!fp) return NULL;
370 fseek(fp, 0, SEEK_END);
371 size = (size_t)ftell(fp);
372 fseek(fp, 0, SEEK_SET);
374 if (UNLIKELY(size == (size_t)-1)) {
379 buf = MEM_mallocN(size, "file_as_lines");
384 * size = because on win32 reading
385 * all the bytes in the file will return
386 * less bytes because of `CRNL` changes.
388 size = fread(buf, 1, size, fp);
389 for (i = 0; i <= size; i++) {
390 if (i == size || buf[i] == '\n') {
391 char *line = BLI_strdupn(&buf[last], i - last);
392 BLI_linklist_append(&lines, line);
406 * Frees memory from a previous call to BLI_file_read_as_lines.
408 void BLI_file_free_lines(LinkNode *lines)
410 BLI_linklist_freeN(lines);
413 /** is file1 older then file2 */
414 bool BLI_file_older(const char *file1, const char *file2)
417 struct _stat st1, st2;
422 if (_wstat(file1_16, &st1)) return false;
423 if (_wstat(file2_16, &st2)) return false;
425 UTF16_UN_ENCODE(file2);
426 UTF16_UN_ENCODE(file1);
428 struct stat st1, st2;
430 if (stat(file1, &st1)) return false;
431 if (stat(file2, &st2)) return false;
433 return (st1.st_mtime < st2.st_mtime);