1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup bli
22 *
23 * Some really low-level file operations.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29
30 #include <sys/stat.h>
31
32 #if defined(__NetBSD__) || defined(__DragonFly__) || defined(__HAIKU__)
33 /* Other modern unix os's should probably use this also */
34 # include <sys/statvfs.h>
35 # define USE_STATFS_STATVFS
36 #endif
37
38 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
39 defined(__DragonFly__)
40 /* For statfs */
41 # include <sys/mount.h>
42 # include <sys/param.h>
43 #endif
44
45 #if defined(__linux__) || defined(__hpux) || defined(__GNU__) || defined(__GLIBC__)
46 # include <sys/vfs.h>
47 #endif
48
49 #include <fcntl.h>
50 #include <string.h> /* strcpy etc.. */
51
52 #ifdef WIN32
53 # include "BLI_string_utf8.h"
54 # include "BLI_winstuff.h"
55 # include "utfconv.h"
56 # include <ShObjIdl.h>
57 # include <direct.h>
58 # include <io.h>
59 # include <stdbool.h>
60 #else
61 # include <pwd.h>
62 # include <sys/ioctl.h>
63 # include <unistd.h>
64 #endif
65
66 /* lib includes */
67 #include "MEM_guardedalloc.h"
68
69 #include "BLI_fileops.h"
70 #include "BLI_linklist.h"
71 #include "BLI_path_util.h"
72 #include "BLI_string.h"
73 #include "BLI_utildefines.h"
74
75 /**
76 * Copies the current working directory into *dir (max size maxncpy), and
77 * returns a pointer to same.
78 *
79 * \note can return NULL when the size is not big enough
80 */
BLI_current_working_dir(char * dir,const size_t maxncpy)81 char *BLI_current_working_dir(char *dir, const size_t maxncpy)
82 {
83 #if defined(WIN32)
84 wchar_t path[MAX_PATH];
85 if (_wgetcwd(path, MAX_PATH)) {
86 if (BLI_strncpy_wchar_as_utf8(dir, path, maxncpy) != maxncpy) {
87 return dir;
88 }
89 }
90 return NULL;
91 #else
92 const char *pwd = BLI_getenv("PWD");
93 if (pwd) {
94 size_t srclen = BLI_strnlen(pwd, maxncpy);
95 if (srclen != maxncpy) {
96 memcpy(dir, pwd, srclen + 1);
97 return dir;
98 }
99 return NULL;
100 }
101 return getcwd(dir, maxncpy);
102 #endif
103 }
104
105 /**
106 * Returns the number of free bytes on the volume containing the specified pathname. */
107 /* Not actually used anywhere.
108 */
BLI_dir_free_space(const char * dir)109 double BLI_dir_free_space(const char *dir)
110 {
111 #ifdef WIN32
112 DWORD sectorspc, bytesps, freec, clusters;
113 char tmp[4];
114
115 tmp[0] = '\\';
116 tmp[1] = 0; /* Just a failsafe */
117 if (ELEM(dir[0] == '/', '\\')) {
118 tmp[0] = '\\';
119 tmp[1] = 0;
120 }
121 else if (dir[1] == ':') {
122 tmp[0] = dir[0];
123 tmp[1] = ':';
124 tmp[2] = '\\';
125 tmp[3] = 0;
126 }
127
128 GetDiskFreeSpace(tmp, §orspc, &bytesps, &freec, &clusters);
129
130 return (double)(freec * bytesps * sectorspc);
131 #else
132
133 # ifdef USE_STATFS_STATVFS
134 struct statvfs disk;
135 # else
136 struct statfs disk;
137 # endif
138
139 char name[FILE_MAXDIR], *slash;
140 int len = strlen(dir);
141
142 if (len >= FILE_MAXDIR) {
143 /* path too long */
144 return -1;
145 }
146
147 strcpy(name, dir);
148
149 if (len) {
150 slash = strrchr(name, '/');
151 if (slash) {
152 slash[1] = 0;
153 }
154 }
155 else {
156 strcpy(name, "/");
157 }
158
159 # if defined(USE_STATFS_STATVFS)
160 if (statvfs(name, &disk)) {
161 return -1;
162 }
163 # elif defined(USE_STATFS_4ARGS)
164 if (statfs(name, &disk, sizeof(struct statfs), 0)) {
165 return -1;
166 }
167 # else
168 if (statfs(name, &disk)) {
169 return -1;
170 }
171 # endif
172
173 return (((double)disk.f_bsize) * ((double)disk.f_bfree));
174 #endif
175 }
176
BLI_ftell(FILE * stream)177 int64_t BLI_ftell(FILE *stream)
178 {
179 #ifdef WIN32
180 return _ftelli64(stream);
181 #else
182 return ftell(stream);
183 #endif
184 }
185
BLI_fseek(FILE * stream,int64_t offset,int whence)186 int BLI_fseek(FILE *stream, int64_t offset, int whence)
187 {
188 #ifdef WIN32
189 return _fseeki64(stream, offset, whence);
190 #else
191 return fseek(stream, offset, whence);
192 #endif
193 }
194
BLI_lseek(int fd,int64_t offset,int whence)195 int64_t BLI_lseek(int fd, int64_t offset, int whence)
196 {
197 #ifdef WIN32
198 return _lseeki64(fd, offset, whence);
199 #else
200 return lseek(fd, offset, whence);
201 #endif
202 }
203
204 /**
205 * Returns the file size of an opened file descriptor.
206 */
BLI_file_descriptor_size(int file)207 size_t BLI_file_descriptor_size(int file)
208 {
209 BLI_stat_t st;
210 if ((file < 0) || (BLI_fstat(file, &st) == -1)) {
211 return -1;
212 }
213 return st.st_size;
214 }
215
216 /**
217 * Returns the size of a file.
218 */
BLI_file_size(const char * path)219 size_t BLI_file_size(const char *path)
220 {
221 BLI_stat_t stats;
222 if (BLI_stat(path, &stats) == -1) {
223 return -1;
224 }
225 return stats.st_size;
226 }
227
228 /* Return file attributes. Apple version of this function is defined in storage_apple.mm */
229 #ifndef __APPLE__
BLI_file_attributes(const char * path)230 eFileAttributes BLI_file_attributes(const char *path)
231 {
232 int ret = 0;
233
234 # ifdef WIN32
235
236 if (BLI_path_extension_check(path, ".lnk")) {
237 return FILE_ATTR_ALIAS;
238 }
239
240 WCHAR wline[FILE_MAXDIR];
241 if (conv_utf_8_to_16(path, wline, ARRAY_SIZE(wline)) != 0) {
242 return ret;
243 }
244 DWORD attr = GetFileAttributesW(wline);
245 if (attr & FILE_ATTRIBUTE_READONLY) {
246 ret |= FILE_ATTR_READONLY;
247 }
248 if (attr & FILE_ATTRIBUTE_HIDDEN) {
249 ret |= FILE_ATTR_HIDDEN;
250 }
251 if (attr & FILE_ATTRIBUTE_SYSTEM) {
252 ret |= FILE_ATTR_SYSTEM;
253 }
254 if (attr & FILE_ATTRIBUTE_ARCHIVE) {
255 ret |= FILE_ATTR_ARCHIVE;
256 }
257 if (attr & FILE_ATTRIBUTE_COMPRESSED) {
258 ret |= FILE_ATTR_COMPRESSED;
259 }
260 if (attr & FILE_ATTRIBUTE_ENCRYPTED) {
261 ret |= FILE_ATTR_ENCRYPTED;
262 }
263 if (attr & FILE_ATTRIBUTE_TEMPORARY) {
264 ret |= FILE_ATTR_TEMPORARY;
265 }
266 if (attr & FILE_ATTRIBUTE_SPARSE_FILE) {
267 ret |= FILE_ATTR_SPARSE_FILE;
268 }
269 if (attr & FILE_ATTRIBUTE_OFFLINE) {
270 ret |= FILE_ATTR_OFFLINE;
271 }
272 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
273 ret |= FILE_ATTR_REPARSE_POINT;
274 }
275
276 # else
277
278 UNUSED_VARS(path);
279
280 /* TODO:
281 * If Immutable set FILE_ATTR_READONLY
282 * If Archived set FILE_ATTR_ARCHIVE
283 */
284 # endif
285 return ret;
286 }
287 #endif
288
289 /* Return alias/shortcut file target. Apple version is defined in storage_apple.mm */
290 #ifndef __APPLE__
BLI_file_alias_target(const char * filepath,char r_targetpath[FILE_MAXDIR])291 bool BLI_file_alias_target(const char *filepath,
292 /* This parameter can only be `const` on Linux since
293 * redirections are not supported there.
294 * NOLINTNEXTLINE: readability-non-const-parameter. */
295 char r_targetpath[FILE_MAXDIR])
296 {
297 # ifdef WIN32
298 if (!BLI_path_extension_check(filepath, ".lnk")) {
299 return false;
300 }
301
302 IShellLinkW *Shortcut = NULL;
303 bool success = false;
304 CoInitializeEx(NULL, COINIT_MULTITHREADED);
305
306 HRESULT hr = CoCreateInstance(
307 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLinkW, (LPVOID *)&Shortcut);
308 if (SUCCEEDED(hr)) {
309 IPersistFile *PersistFile;
310 hr = Shortcut->lpVtbl->QueryInterface(Shortcut, &IID_IPersistFile, (LPVOID *)&PersistFile);
311 if (SUCCEEDED(hr)) {
312 WCHAR path_utf16[FILE_MAXDIR] = {0};
313 if (conv_utf_8_to_16(filepath, path_utf16, ARRAY_SIZE(path_utf16)) == 0) {
314 hr = PersistFile->lpVtbl->Load(PersistFile, path_utf16, STGM_READ);
315 if (SUCCEEDED(hr)) {
316 hr = Shortcut->lpVtbl->Resolve(Shortcut, 0, SLR_NO_UI | SLR_UPDATE);
317 if (SUCCEEDED(hr)) {
318 wchar_t target_utf16[FILE_MAXDIR] = {0};
319 hr = Shortcut->lpVtbl->GetPath(Shortcut, target_utf16, FILE_MAXDIR, NULL, 0);
320 if (SUCCEEDED(hr)) {
321 success = (conv_utf_16_to_8(target_utf16, r_targetpath, FILE_MAXDIR) == 0);
322 }
323 }
324 PersistFile->lpVtbl->Release(PersistFile);
325 }
326 }
327 }
328 Shortcut->lpVtbl->Release(Shortcut);
329 }
330
331 return (success && r_targetpath[0]);
332 # else
333 UNUSED_VARS(r_targetpath, filepath);
334 /* File-based redirection not supported. */
335 return false;
336 # endif
337 }
338 #endif
339
340 /**
341 * Returns the st_mode from stat-ing the specified path name, or 0 if stat fails
342 * (most likely doesn't exist or no access).
343 */
BLI_exists(const char * path)344 int BLI_exists(const char *path)
345 {
346 #if defined(WIN32)
347 BLI_stat_t st;
348 wchar_t *tmp_16 = alloc_utf16_from_8(path, 1);
349 int len, res;
350
351 len = wcslen(tmp_16);
352 /* in Windows #stat doesn't recognize dir ending on a slash
353 * so we remove it here */
354 if ((len > 3) && ELEM(tmp_16[len - 1], L'\\', L'/')) {
355 tmp_16[len - 1] = '\0';
356 }
357 /* two special cases where the trailing slash is needed:
358 * 1. after the share part of a UNC path
359 * 2. after the C:\ when the path is the volume only
360 */
361 if ((len >= 3) && (tmp_16[0] == L'\\') && (tmp_16[1] == L'\\')) {
362 BLI_path_normalize_unc_16(tmp_16);
363 }
364
365 if ((tmp_16[1] == L':') && (tmp_16[2] == L'\0')) {
366 tmp_16[2] = L'\\';
367 tmp_16[3] = L'\0';
368 }
369
370 res = BLI_wstat(tmp_16, &st);
371
372 free(tmp_16);
373 if (res == -1) {
374 return 0;
375 }
376 #else
377 struct stat st;
378 BLI_assert(!BLI_path_is_rel(path));
379 if (stat(path, &st)) {
380 return (0);
381 }
382 #endif
383 return (st.st_mode);
384 }
385
386 #ifdef WIN32
BLI_fstat(int fd,BLI_stat_t * buffer)387 int BLI_fstat(int fd, BLI_stat_t *buffer)
388 {
389 # if defined(_MSC_VER)
390 return _fstat64(fd, buffer);
391 # else
392 return _fstat(fd, buffer);
393 # endif
394 }
395
BLI_stat(const char * path,BLI_stat_t * buffer)396 int BLI_stat(const char *path, BLI_stat_t *buffer)
397 {
398 int r;
399 UTF16_ENCODE(path);
400
401 r = BLI_wstat(path_16, buffer);
402
403 UTF16_UN_ENCODE(path);
404 return r;
405 }
406
BLI_wstat(const wchar_t * path,BLI_stat_t * buffer)407 int BLI_wstat(const wchar_t *path, BLI_stat_t *buffer)
408 {
409 # if defined(_MSC_VER)
410 return _wstat64(path, buffer);
411 # else
412 return _wstat(path, buffer);
413 # endif
414 }
415 #else
BLI_fstat(int fd,struct stat * buffer)416 int BLI_fstat(int fd, struct stat *buffer)
417 {
418 return fstat(fd, buffer);
419 }
420
BLI_stat(const char * path,struct stat * buffer)421 int BLI_stat(const char *path, struct stat *buffer)
422 {
423 return stat(path, buffer);
424 }
425 #endif
426
427 /**
428 * Does the specified path point to a directory?
429 * \note Would be better in fileops.c except that it needs stat.h so add here
430 */
BLI_is_dir(const char * file)431 bool BLI_is_dir(const char *file)
432 {
433 return S_ISDIR(BLI_exists(file));
434 }
435
436 /**
437 * Does the specified path point to a non-directory?
438 */
BLI_is_file(const char * path)439 bool BLI_is_file(const char *path)
440 {
441 const int mode = BLI_exists(path);
442 return (mode && !S_ISDIR(mode));
443 }
444
445 /**
446 * Use for both text and binary file reading.
447 */
file_read_data_as_mem_impl(FILE * fp,bool read_size_exact,size_t pad_bytes,size_t * r_size)448 static void *file_read_data_as_mem_impl(FILE *fp,
449 bool read_size_exact,
450 size_t pad_bytes,
451 size_t *r_size)
452 {
453 BLI_stat_t st;
454 if (BLI_fstat(fileno(fp), &st) == -1) {
455 return NULL;
456 }
457 if (S_ISDIR(st.st_mode)) {
458 return NULL;
459 }
460 if (BLI_fseek(fp, 0L, SEEK_END) == -1) {
461 return NULL;
462 }
463 /* Don't use the 'st_size' because it may be the symlink. */
464 const long int filelen = BLI_ftell(fp);
465 if (filelen == -1) {
466 return NULL;
467 }
468 if (BLI_fseek(fp, 0L, SEEK_SET) == -1) {
469 return NULL;
470 }
471
472 void *mem = MEM_mallocN(filelen + pad_bytes, __func__);
473 if (mem == NULL) {
474 return NULL;
475 }
476
477 const long int filelen_read = fread(mem, 1, filelen, fp);
478 if ((filelen_read < 0) || ferror(fp)) {
479 MEM_freeN(mem);
480 return NULL;
481 }
482
483 if (read_size_exact) {
484 if (filelen_read != filelen) {
485 MEM_freeN(mem);
486 return NULL;
487 }
488 }
489 else {
490 if (filelen_read < filelen) {
491 mem = MEM_reallocN(mem, filelen_read + pad_bytes);
492 if (mem == NULL) {
493 return NULL;
494 }
495 }
496 }
497
498 *r_size = filelen_read;
499
500 return mem;
501 }
502
BLI_file_read_text_as_mem(const char * filepath,size_t pad_bytes,size_t * r_size)503 void *BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
504 {
505 FILE *fp = BLI_fopen(filepath, "r");
506 void *mem = NULL;
507 if (fp) {
508 mem = file_read_data_as_mem_impl(fp, false, pad_bytes, r_size);
509 fclose(fp);
510 }
511 return mem;
512 }
513
BLI_file_read_binary_as_mem(const char * filepath,size_t pad_bytes,size_t * r_size)514 void *BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
515 {
516 FILE *fp = BLI_fopen(filepath, "rb");
517 void *mem = NULL;
518 if (fp) {
519 mem = file_read_data_as_mem_impl(fp, true, pad_bytes, r_size);
520 fclose(fp);
521 }
522 return mem;
523 }
524
525 /**
526 * Return the text file data with:
527
528 * - Newlines replaced with '\0'.
529 * - Optionally trim white-space, replacing trailing <space> & <tab> with '\0'.
530 *
531 * This is an alternative to using #BLI_file_read_as_lines,
532 * allowing us to loop over lines without converting it into a linked list
533 * with individual allocations.
534 *
535 * \param trim_trailing_space: Replace trailing spaces & tabs with nil.
536 * This arguments prevents the caller from counting blank lines (if that's important).
537 * \param pad_bytes: When this is non-zero, the first byte is set to nil,
538 * to simplify parsing the file.
539 * It's recommended to pass in 1, so all text is nil terminated.
540 *
541 * Example looping over lines:
542 *
543 * \code{.c}
544 * size_t data_len;
545 * char *data = BLI_file_read_text_as_mem_with_newline_as_nil(filepath, true, 1, &data_len);
546 * char *data_end = data + data_len;
547 * for (char *line = data; line != data_end; line = strlen(line) + 1) {
548 * printf("line='%s'\n", line);
549 * }
550 * \endcode
551 */
BLI_file_read_text_as_mem_with_newline_as_nil(const char * filepath,bool trim_trailing_space,size_t pad_bytes,size_t * r_size)552 void *BLI_file_read_text_as_mem_with_newline_as_nil(const char *filepath,
553 bool trim_trailing_space,
554 size_t pad_bytes,
555 size_t *r_size)
556 {
557 char *mem = BLI_file_read_text_as_mem(filepath, pad_bytes, r_size);
558 if (mem != NULL) {
559 char *mem_end = mem + *r_size;
560 if (pad_bytes != 0) {
561 *mem_end = '\0';
562 }
563 for (char *p = mem, *p_next; p != mem_end; p = p_next) {
564 p_next = memchr(p, '\n', mem_end - p);
565 if (p_next != NULL) {
566 if (trim_trailing_space) {
567 for (char *p_trim = p_next - 1; p_trim > p && ELEM(*p_trim, ' ', '\t'); p_trim--) {
568 *p_trim = '\0';
569 }
570 }
571 *p_next = '\0';
572 p_next++;
573 }
574 else {
575 p_next = mem_end;
576 }
577 }
578 }
579 return mem;
580 }
581
582 /**
583 * Reads the contents of a text file and returns the lines in a linked list.
584 */
BLI_file_read_as_lines(const char * filepath)585 LinkNode *BLI_file_read_as_lines(const char *filepath)
586 {
587 FILE *fp = BLI_fopen(filepath, "r");
588 LinkNodePair lines = {NULL, NULL};
589 char *buf;
590 size_t size;
591
592 if (!fp) {
593 return NULL;
594 }
595
596 BLI_fseek(fp, 0, SEEK_END);
597 size = (size_t)BLI_ftell(fp);
598 BLI_fseek(fp, 0, SEEK_SET);
599
600 if (UNLIKELY(size == (size_t)-1)) {
601 fclose(fp);
602 return NULL;
603 }
604
605 buf = MEM_mallocN(size, "file_as_lines");
606 if (buf) {
607 size_t i, last = 0;
608
609 /*
610 * size = because on win32 reading
611 * all the bytes in the file will return
612 * less bytes because of `CRNL` changes.
613 */
614 size = fread(buf, 1, size, fp);
615 for (i = 0; i <= size; i++) {
616 if (i == size || buf[i] == '\n') {
617 char *line = BLI_strdupn(&buf[last], i - last);
618 BLI_linklist_append(&lines, line);
619 last = i + 1;
620 }
621 }
622
623 MEM_freeN(buf);
624 }
625
626 fclose(fp);
627
628 return lines.list;
629 }
630
631 /*
632 * Frees memory from a previous call to BLI_file_read_as_lines.
633 */
BLI_file_free_lines(LinkNode * lines)634 void BLI_file_free_lines(LinkNode *lines)
635 {
636 BLI_linklist_freeN(lines);
637 }
638
639 /** is file1 older than file2 */
BLI_file_older(const char * file1,const char * file2)640 bool BLI_file_older(const char *file1, const char *file2)
641 {
642 #ifdef WIN32
643 struct _stat st1, st2;
644
645 UTF16_ENCODE(file1);
646 UTF16_ENCODE(file2);
647
648 if (_wstat(file1_16, &st1)) {
649 return false;
650 }
651 if (_wstat(file2_16, &st2)) {
652 return false;
653 }
654
655 UTF16_UN_ENCODE(file2);
656 UTF16_UN_ENCODE(file1);
657 #else
658 struct stat st1, st2;
659
660 if (stat(file1, &st1)) {
661 return false;
662 }
663 if (stat(file2, &st2)) {
664 return false;
665 }
666 #endif
667 return (st1.st_mtime < st2.st_mtime);
668 }
669