17#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__HAIKU__)
19# include <sys/statvfs.h>
20# define USE_STATFS_STATVFS
23#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
24 defined(__DragonFly__)
26# include <sys/mount.h>
27# include <sys/param.h>
30#if defined(__linux__) || defined(__hpux) || defined(__GNU__) || defined(__GLIBC__)
47# include <sys/ioctl.h>
62#if !defined(__APPLE__)
75 return _wchdir(wdir) == 0;
88 wchar_t path[MAX_PATH];
89 if (_wgetcwd(path, MAX_PATH)) {
99 if (srclen != maxncpy) {
100 memcpy(dir, pwd, srclen + 1);
105 return getcwd(dir, maxncpy);
112 const char *home_dir;
124 if (home_dir ==
nullptr) {
125 if (
const passwd *pwuser = getpwuid(getuid())) {
126 home_dir = pwuser->pw_dir;
137 DWORD sectorspc, bytesps, freec, clusters;
142 if (
ELEM(dir[0],
'/',
'\\')) {
146 else if (dir[1] ==
':') {
153 GetDiskFreeSpace(tmp, §orspc, &bytesps, &freec, &clusters);
155 return double(freec * bytesps * sectorspc);
158# ifdef USE_STATFS_STATVFS
165 int len = strlen(dir);
185# if defined(USE_STATFS_STATVFS)
189# elif defined(USE_STATFS_4ARGS)
190 if (statfs(
dirname, &disk,
sizeof(
struct statfs), 0)) {
199 return double(disk.f_bsize) * double(disk.f_bfree);
206 return _ftelli64(stream);
208 return ftell(stream);
215 return _fseeki64(stream, offset, whence);
217 return fseek(stream, offset, whence);
224 return _lseeki64(fd, offset, whence);
226 return lseek(fd, offset, whence);
233 if ((file < 0) || (
BLI_fstat(file, &st) == -1)) {
245 return stats.st_size;
265 DWORD attr = GetFileAttributesW(wline);
266 if (attr == INVALID_FILE_ATTRIBUTES) {
268 "BLI_file_attributes should only be called on existing files.");
272 if (attr & FILE_ATTRIBUTE_READONLY) {
275 if (attr & FILE_ATTRIBUTE_HIDDEN) {
278 if (attr & FILE_ATTRIBUTE_SYSTEM) {
281 if (attr & FILE_ATTRIBUTE_ARCHIVE) {
284 if (attr & FILE_ATTRIBUTE_COMPRESSED) {
287 if (attr & FILE_ATTRIBUTE_ENCRYPTED) {
290 if (attr & FILE_ATTRIBUTE_TEMPORARY) {
293 if (attr & FILE_ATTRIBUTE_SPARSE_FILE) {
296 if (attr & FILE_ATTRIBUTE_OFFLINE || attr & FILE_ATTRIBUTE_RECALL_ON_OPEN ||
297 attr & FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS)
301 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
330 HRESULT hr = CoInitializeEx(
nullptr, COINIT_MULTITHREADED);
335 IShellLinkW *Shortcut =
nullptr;
336 hr = CoCreateInstance(
337 CLSID_ShellLink,
nullptr, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (LPVOID *)&Shortcut);
339 bool success =
false;
341 IPersistFile *PersistFile;
342 hr = Shortcut->QueryInterface(IID_IPersistFile, (LPVOID *)&PersistFile);
346 hr = PersistFile->Load(path_utf16, STGM_READ);
348 hr = Shortcut->Resolve(0, SLR_NO_UI | SLR_UPDATE | SLR_NOSEARCH);
351 hr = Shortcut->GetPath(target_utf16,
FILE_MAXDIR,
nullptr, 0);
356 PersistFile->Release();
364 return (success && r_targetpath[0]);
380 len = wcslen(tmp_16);
384 tmp_16[
len - 1] =
'\0';
390 if ((
len >= 3) && (tmp_16[0] ==
L'\\') && (tmp_16[1] ==
L'\\')) {
391 BLI_path_normalize_unc_16(tmp_16);
394 if ((tmp_16[1] ==
L':') && (tmp_16[2] ==
L'\0')) {
399 res = BLI_wstat(tmp_16, &st);
408 if (stat(path, &st)) {
418# if defined(_MSC_VER)
419 return _fstat64(fd, buffer);
421 return _fstat(fd, buffer);
430 r = BLI_wstat(path_16, buffer);
436int BLI_wstat(
const wchar_t *path,
BLI_stat_t *buffer)
438# if defined(_MSC_VER)
439 return _wstat64(path, buffer);
441 return _wstat(path, buffer);
447 return fstat(fd, buffer);
452 return stat(path, buffer);
464 return (mode && !
S_ISDIR(mode));
468 bool read_size_exact,
493 void *mem =
MEM_mallocN(filelen + pad_bytes, __func__);
494 if (mem ==
nullptr) {
498 const long int filelen_read = fread(mem, 1, filelen, fp);
499 if ((filelen_read < 0) || ferror(fp)) {
504 if (read_size_exact) {
505 if (filelen_read != filelen) {
511 if (filelen_read < filelen) {
513 if (mem ==
nullptr) {
519 *r_size = filelen_read;
547 bool trim_trailing_space,
552 if (mem !=
nullptr) {
553 char *mem_end = mem + *r_size;
554 if (pad_bytes != 0) {
557 for (
char *p = mem, *p_next; p != mem_end; p = p_next) {
558 p_next =
static_cast<char *
>(memchr(p,
'\n', mem_end - p));
559 if (p_next !=
nullptr) {
560 if (trim_trailing_space) {
561 for (
char *p_trim = p_next - 1; p_trim > p &&
ELEM(*p_trim,
' ',
'\t'); p_trim--) {
607 if (
i ==
size || buf[
i] ==
'\n') {
636 return (st1.st_mtime < st2.st_mtime);
#define BLI_assert_msg(a, msg)
File and directory operations.
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
@ FILE_ATTR_REPARSE_POINT
void BLI_kdtree_nd_ free(KDTree *tree)
void BLI_linklist_freeN(LinkNode *list)
void void void void BLI_linklist_append(LinkNodePair *list_pair, void *ptr) ATTR_NONNULL(1)
void BLI_setenv(const char *env, const char *val) ATTR_NONNULL(1)
const char * BLI_getenv(const char *env) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_extension_check(const char *path, const char *ext) ATTR_NONNULL(1
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
int char char int int int int size_t BLI_strnlen(const char *str, size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
size_t BLI_strncpy_wchar_as_utf8(char *__restrict dst, const wchar_t *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
int BLI_thread_is_main(void)
Compatibility-like things for windows.
const char * dirname(char *path)
Read Guarded memory(de)allocation.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
#define MEM_reallocN(vmemh, len)
void * MEM_mallocN(size_t len, const char *str)
void * MEM_calloc_arrayN(size_t len, size_t size, const char *str)
void MEM_freeN(void *vmemh)
bool BLI_change_working_dir(const char *dir)
const char * BLI_dir_home()
eFileAttributes BLI_file_attributes(const char *path)
int BLI_exists(const char *path)
bool BLI_file_older(const char *file1, const char *file2)
void BLI_file_free_lines(LinkNode *lines)
int BLI_fstat(int fd, struct stat *buffer)
LinkNode * BLI_file_read_as_lines(const char *filepath)
bool BLI_file_alias_target(const char *filepath, char r_targetpath[FILE_MAXDIR])
int BLI_stat(const char *path, struct stat *buffer)
size_t BLI_file_descriptor_size(int file)
void * BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
int64_t BLI_lseek(int fd, int64_t offset, int whence)
double BLI_dir_free_space(const char *dir)
size_t BLI_file_size(const char *path)
bool BLI_is_dir(const char *path)
int BLI_fseek(FILE *stream, int64_t offset, int whence)
void * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
void * 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)
char * BLI_current_working_dir(char *dir, const size_t maxncpy)
void * BLI_file_read_data_as_mem_from_handle(FILE *fp, bool read_size_exact, size_t pad_bytes, size_t *r_size)
bool BLI_is_file(const char *path)
int64_t BLI_ftell(FILE *stream)
wchar_t * alloc_utf16_from_8(const char *in8, size_t add)
int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16)
int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
#define UTF16_ENCODE(in8str)
#define UTF16_UN_ENCODE(in8str)