aboutsummaryrefslogtreecommitdiff
path: root/src/FileUtil.cpp
blob: 53c1132ce98d6388dc4f7295d028688d8e470cc4 (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
#include "../include/FileUtil.hpp"
#include <cstdio>

#if OS_FAMILY == OS_FAMILY_POSIX
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <fcntl.h>
#else
#include <UserEnv.h>
// Copied from linux libc sys/stat.h:
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#pragma comment(lib, "Userenv.lib")
#endif

using namespace std;

namespace sibs
{
#if OS_FAMILY == OS_FAMILY_POSIX
#define toUtf8(input) input
    FileString toFileString(const std::string &utf8Str)
    {
        return utf8Str;
    }
    
    FileString toFileString(const StringView &utf8Str)
    {
        return FileString(utf8Str.data, utf8Str.size);
    }
#else
    std::string toUtf8(const sibs::FileString &input)
    {
        std::string result;
        utf8::utf16to8(input.data(), input.data() + input.size(), std::back_inserter(result));
        return result;
    }

    std::string toUtf8(const TCHAR *input)
    {
        size_t inputSize = wcslen(input);
        std::string result;
        utf8::utf16to8(input, input + inputSize, std::back_inserter(result));
        return result;
    }

    FileString utf8To16(const StringView &utf8Str)
    {
        FileString result;
        utf8::utf8to16(utf8Str.data, utf8Str.data + utf8Str.size, std::back_inserter(result));
        return result;
    }

    FileString utf8To16(const std::string &utf8Str)
    {
        FileString result;
        utf8::utf8to16(utf8Str.data(), utf8Str.data() + utf8Str.size(), std::back_inserter(result));
        return result;
    }

    FileString toFileString(const std::string &utf8Str)
    {
        return utf8To16(utf8Str);
    }
    
    FileString toFileString(const StringView &utf8Str)
    {
        FileString result;
        utf8::utf8to16(utf8Str.data, utf8Str.data + utf8Str.size, std::back_inserter(result));
        return result;
    }

    FileString getLastErrorAsString()
    {
        DWORD errorMessageId = GetLastError();
        if (errorMessageId == 0) return TINYDIR_STRING("");
        LPWSTR messageBuffer = nullptr;
        size_t size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, errorMessageId, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, nullptr);
        FileString message(messageBuffer, size);
        LocalFree(messageBuffer);
        return message;
    }

    void replaceChar(FileString &input, wchar_t charToReplace, wchar_t charToReplaceWith)
    {
        for (int i = 0; i < input.size(); ++i)
        {
            wchar_t c = input[i];
            if (c == charToReplace)
                input[i] = charToReplaceWith;
        }
    }
#endif

#if OS_FAMILY == OS_FAMILY_POSIX
    FileType getFileType(const _tinydir_char_t *path)
    {
        struct stat64 fileStat;
        if (stat64(path, &fileStat) == 0)
            return S_ISREG(fileStat.st_mode) ? FileType::REGULAR : FileType::DIRECTORY;
        else
            return FileType::FILE_NOT_FOUND;
    }
    
    Result<u64> getFileLastModifiedTime(const _tinydir_char_t *path)
    {
        struct stat64 fileStat;
        if (stat64(path, &fileStat) == 0)
            return Result<u64>::Ok(fileStat.st_mtim.tv_sec);
        else
        {
            string errMsg = "File not found: ";
            errMsg += toUtf8(path);
            return Result<u64>::Err(errMsg);
        }
    }
#else
    FileType getFileType(const _tinydir_char_t *path)
    {
        struct _stat64i32 fileStat;
        if (_wstat(path, &fileStat) == 0)
            return S_ISREG(fileStat.st_mode) ? FileType::REGULAR : FileType::DIRECTORY;
        else
            return FileType::FILE_NOT_FOUND;
    }
    
    Result<u64> getFileLastModifiedTime(const _tinydir_char_t *path)
    {
        struct _stat64i32 fileStat;
        if (_wstat(path, &fileStat) == 0)
            return Result<u64>::Ok(fileStat.st_mtime);
        else
        {
            string errMsg = "File not found: ";
            errMsg += toUtf8(path);
            return Result<u64>::Err(errMsg);
        }
    }
#endif

    // TODO: Handle failure (directory doesn't exist, no permission etc)
    void walkDir(const _tinydir_char_t *directory, FileWalkCallbackFunc callbackFunc)
    {
        tinydir_dir dir;
        tinydir_open(&dir, directory);

        while (dir.has_next)
        {
            tinydir_file file;
            tinydir_readfile(&dir, &file);
            if(_tinydir_strncmp(file.name, TINYDIR_STRING("."), 1) != 0)
                callbackFunc(&file);
            tinydir_next(&dir);
        }

        tinydir_close(&dir);
    }

    // TODO: Handle failure (directory doesn't exist, no permission etc)
    void walkDirFiles(const _tinydir_char_t *directory, FileWalkCallbackFunc callbackFunc)
    {
        tinydir_dir dir;
        tinydir_open(&dir, directory);

        while (dir.has_next)
        {
            tinydir_file file;
            tinydir_readfile(&dir, &file);
            if(file.is_reg)
                callbackFunc(&file);
            tinydir_next(&dir);
        }

        tinydir_close(&dir);
    }

    // TODO: Handle failure (directory doesn't exist, no permission etc)
    void walkDirFilesRecursive(const _tinydir_char_t *directory, FileWalkCallbackFunc callbackFunc)
    {
        tinydir_dir dir;
        tinydir_open(&dir, directory);

        while (dir.has_next)
        {
            tinydir_file file;
            tinydir_readfile(&dir, &file);
            if(file.is_reg)
                callbackFunc(&file);
            else if(_tinydir_strncmp(file.name, TINYDIR_STRING("."), 1) != 0)
                walkDirFilesRecursive(file.path, callbackFunc);
            tinydir_next(&dir);
        }

        tinydir_close(&dir);
    }

    Result<StringView> getFileContent(const _tinydir_char_t *filepath)
    {
#if OS_FAMILY == OS_FAMILY_POSIX
        FILE *file = fopen(filepath, "rb");
#else
        FILE *file = _wfopen(filepath, TINYDIR_STRING("rb"));
#endif
        if(!file)
        {
            int error = errno;
            string errMsg = "Failed to open file: ";
            errMsg += toUtf8(filepath);
            errMsg += "; reason: ";
            errMsg += strerror(error);
            return Result<StringView>::Err(errMsg);
        }

        fseek(file, 0, SEEK_END);
        size_t fileSize = ftell(file);
        fseek(file, 0, SEEK_SET);

        // TODO: Change this to string so it can be deallocated and use std::move to prevent copies
        char *result = (char*)malloc(fileSize + 1);
        if(!result)
        {
            std::string errMsg = "Failed to load file content from file: ";
            errMsg += toUtf8(filepath);
            throw std::runtime_error(errMsg);
        }
        result[fileSize] = '\0';
        fread(result, 1, fileSize, file);
        fclose(file);
        return Result<StringView>::Ok(StringView(result, fileSize));
    }

    Result<bool> fileOverwrite(const _tinydir_char_t *filepath, StringView data)
    {
#if OS_FAMILY == OS_FAMILY_POSIX
        FILE *file = fopen(filepath, "wb");
#else
        FILE *file = _wfopen(filepath, TINYDIR_STRING("wb"));
#endif
        if(!file)
        {
            int error = errno;
            string errMsg = "Failed to overwrite file: ";
            errMsg += toUtf8(filepath);
            errMsg += "; reason: ";
            errMsg += strerror(error);
            return Result<bool>::Err(errMsg);
        }
        setbuf(file, NULL);
        fwrite(data.data, 1, data.size, file);
        fclose(file);
        return Result<bool>::Ok(true);
    }
#if OS_FAMILY == OS_FAMILY_POSIX
    Result<FileString> getHomeDir()
    {
        const char *homeDir = getenv("HOME");
        if(!homeDir)
        {
            passwd *pw = getpwuid(getuid());
            homeDir = pw->pw_dir;
        }
        return Result<FileString>::Ok(homeDir);
    }

    Result<FileString> getCwd()
    {
        FileString cwd;
        cwd.resize(_TINYDIR_PATH_MAX);
        if(getcwd(&cwd[0], _TINYDIR_PATH_MAX) != 0)
        {
            if(cwd.empty()) cwd = ".";
            cwd.resize(_tinydir_strlen(cwd.c_str()));
            return Result<FileString>::Ok(cwd);
        }
        return Result<FileString>::Err(strerror(errno));
    }

    Result<bool> createDirectoryRecursive(const _tinydir_char_t *path)
    {
        char pathBuffer[_TINYDIR_PATH_MAX];
        size_t pathLength = strlen(path);
        if(pathLength > sizeof(pathBuffer) - 1)
        {
            string errMsg = "Directory path too long: ";
            errMsg += string(path, pathLength);
            return Result<bool>::Err(errMsg, ENAMETOOLONG);
        }
        strcpy(pathBuffer, path);

        char *p = pathBuffer;
        for(size_t i = 0; i < pathLength; ++i)
        {
            if(i > 0 && *p == '/')
            {
                *p = '\0';
                if(mkdir(pathBuffer, S_IRWXU) != 0)
                {
                    int error = errno;
                    if(error != EEXIST)
                    {
                        string errMsg = "Failed to create directory: ";
                        errMsg += pathBuffer;
                        errMsg += "; reason: ";
                        errMsg += strerror(error);
                        return Result<bool>::Err(errMsg, error);
                    }
                }
                *p = '/';
            }
            ++p;
        }

        if(mkdir(pathBuffer, S_IRWXU) != 0)
        {
            int error = errno;
            if(error != EEXIST)
            {
                string errMsg = "Failed to create directory: ";
                errMsg += pathBuffer;
                errMsg += "; reason: ";
                errMsg += strerror(error);
                return Result<bool>::Err(errMsg, error);
            }
        }

        return Result<bool>::Ok(true);
    }

    Result<FileString> getRealPath(const _tinydir_char_t *path)
    {
        // TODO: Verify NULL can be passed as 'resolved' argument with different compilers and operating systems (clang, freebsd etc)
        char *resolved = realpath(path, nullptr);
        if(!resolved)
        {
            int error = errno;
            FileString errMsg = "Failed to get real path for \"";
            errMsg += path;
            errMsg += "\": ";
            errMsg += strerror(error);
            return Result<FileString>::Err(errMsg, error);
        }

        string result = resolved;
        free(resolved);
        return Result<FileString>::Ok(result);
    }
#else

	Result<FileString> getHomeDir()
	{
        BOOL ret;
        HANDLE hToken;
        FileString homeDir;
        DWORD homeDirLen = _TINYDIR_PATH_MAX;
        homeDir.resize(homeDirLen);

        if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hToken))
            return Result<FileString>::Err("Failed to open process token");

        if (!GetUserProfileDirectory(hToken, &homeDir[0], &homeDirLen))
        {
            CloseHandle(hToken);
            return Result<FileString>::Err("Failed to get home directory");
        }
        
        CloseHandle(hToken);
        homeDir.resize(_tinydir_strlen(homeDir.c_str()));
        return Result<FileString>::Ok(homeDir);
	}

	Result<FileString> getCwd()
	{
		FileString cwd;
		cwd.resize(_TINYDIR_PATH_MAX);
		if (GetCurrentDirectory(_TINYDIR_PATH_MAX, &cwd[0]) == 0)
		{
            FileString lastErrStr = getLastErrorAsString();
            return Result<FileString>::Err(toUtf8(lastErrStr));
		}
        cwd.resize(_tinydir_strlen(cwd.c_str()));
        return Result<FileString>::Ok(cwd);
	}

	Result<bool> createDirectoryRecursive(const _tinydir_char_t *path)
	{
        _tinydir_char_t pathBuffer[_TINYDIR_PATH_MAX];
		size_t pathLength = _tinydir_strlen(path);
		if (pathLength > sizeof(pathBuffer) - 1)
		{
			string errMsg = "Directory path too long: ";
			errMsg += toUtf8(FileString(path, pathLength));
			return Result<bool>::Err(errMsg, ENAMETOOLONG);
		}
		_tinydir_strcpy(pathBuffer, path);

        _tinydir_char_t *p = pathBuffer;
		for (size_t i = 0; i < pathLength; ++i)
		{
			if (i > 0 && *p == '/')
			{
				*p = '\0';
				if (_wmkdir(pathBuffer) != 0)
				{
					int error = errno;
					if (error != EEXIST)
					{
						string errMsg = "Failed to create directory: ";
						errMsg += toUtf8(pathBuffer);
						errMsg += "; reason: ";
						errMsg += strerror(error);
						return Result<bool>::Err(errMsg, error);
					}
				}
				*p = '/';
			}
			++p;
		}

		if (_wmkdir(pathBuffer) != 0)
		{
			int error = errno;
			if (error != EEXIST)
			{
				string errMsg = "Failed to create directory: ";
				errMsg += toUtf8(pathBuffer);
				errMsg += "; reason: ";
				errMsg += strerror(error);
				return Result<bool>::Err(errMsg, error);
			}
		}

		return Result<bool>::Ok(true);
	}

	Result<FileString> getRealPath(const _tinydir_char_t *path)
	{
        FileString fullPath;
        fullPath.resize(_TINYDIR_PATH_MAX);
        if (GetFullPathName(path, _TINYDIR_PATH_MAX, &fullPath[0], nullptr) == 0)
        {
            int error = GetLastError();
			string errMsg = "Failed to get real path for \"";
			errMsg += toUtf8(path);
			errMsg += "\": ";
			errMsg += toUtf8(getLastErrorAsString());
			return Result<FileString>::Err(errMsg, error);
		}
        fullPath.resize(_tinydir_strlen(fullPath.c_str()));
		return Result<FileString>::Ok(fullPath);
	}
#endif

    // TODO: Support better path equality check. For example if path contains several slashes in a row: /home/userName/.sibs//lib////libraryName
    // then it should equal: /home/userName/.sibs/lib/libraryName
    // Maybe check with OS operation if they refer to the same inode?
    bool pathEquals(const std::string &path, const std::string &otherPath)
    {
        if(path.size() != otherPath.size())
            return false;
        
        size_t size = path.size();
        for(size_t i = 0; i < size; ++i)
        {
            char c = path[i];
            char otherC = otherPath[i];
            if(c == '\\') c = '/';
            if(otherC == '\\') otherC = '/';
            if(c != otherC)
                return false;
        }
        
        return true;
    }
}