automatic safe mode!

to-do: test on Windows...
This commit is contained in:
tildearrow 2023-10-16 14:55:01 -05:00
parent 1cf519ce17
commit e633550647
6 changed files with 86 additions and 4 deletions

View file

@ -25,6 +25,7 @@
#include <shlwapi.h>
#else
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#endif
@ -100,3 +101,26 @@ bool makeDir(const char* path) {
return (mkdir(path,0755)==0);
#endif
}
int touchFile(const char* path) {
#ifdef _WIN32
HANDLE h=CreateFileW(utf8To16(path).c_str(),GENERIC_WRITE,FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_TEMPORARY,NULL);
if (h==INVALID_HANDLE_VALUE) {
switch (GetLastError()) {
case ERROR_FILE_EXISTS:
return -EEXIST;
break;
}
return -EPERM;
}
if (CloseHandle(h)==0) {
return -EBADF;
}
return 0;
#else
int fd=open(path,O_CREAT|O_WRONLY|O_TRUNC|O_EXCL,0666);
if (fd<0) return -errno;
close(fd);
return 0;
#endif
}