aboutsummaryrefslogtreecommitdiffstats
path: root/util/folder/dirut.h
blob: ccdfc94ed85fd48418b38cf543f3422c13340a79 (plain) (blame)
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
#pragma once

#include <util/system/defaults.h>
#include <util/system/sysstat.h>
#include <util/system/fs.h>
#include <util/generic/string.h>
#include <util/generic/yexception.h>

#include <sys/types.h>

#include <cerrno>
#include <cstdlib>

#ifdef _win32_
    #include <util/system/winint.h>
    #include <direct.h>
    #include <malloc.h>
    #include <time.h>
    #include <io.h>
    #include "dirent_win.h"

// these live in mktemp_system.cpp
extern "C" int mkstemps(char* path, int slen);
char* mkdtemp(char* path);

#else
    #ifdef _sun_
        #include <alloca.h>

char* mkdtemp(char* path);
    #endif
    #include <unistd.h>
    #include <pwd.h>
    #include <dirent.h>
    #ifndef DT_DIR
        #include <sys/stat.h>
    #endif
#endif

bool IsDir(const TString& path);

int mkpath(char* path, int mode = 0777);

TString GetHomeDir();

void MakeDirIfNotExist(const char* path, int mode = 0777);

inline void MakeDirIfNotExist(const TString& path, int mode = 0777) {
    MakeDirIfNotExist(path.data(), mode);
}

/// Create path making parent directories as needed
void MakePathIfNotExist(const char* path, int mode = 0777);

void SlashFolderLocal(TString& folder);
bool correctpath(TString& filename);
bool resolvepath(TString& folder, const TString& home);

char GetDirectorySeparator();
const char* GetDirectorySeparatorS();

void RemoveDirWithContents(TString dirName);

const char* GetFileNameComponent(const char* f);

inline TString GetFileNameComponent(const TString& f) {
    return GetFileNameComponent(f.data());
}

/// RealPath doesn't guarantee trailing separator to be stripped or left in place for directories.
TString RealPath(const TString& path);     // throws
TString RealLocation(const TString& path); /// throws; last file name component doesn't need to exist

TString GetSystemTempDir();

int MakeTempDir(char path[/*FILENAME_MAX*/], const char* prefix);

int ResolvePath(const char* rel, const char* abs, char res[/*FILENAME_MAX*/], bool isdir = false);
TString ResolvePath(const char* rel, const char* abs, bool isdir = false);
TString ResolvePath(const char* path, bool isDir = false);

TString ResolveDir(const char* path);

bool SafeResolveDir(const char* path, TString& result);

TString GetDirName(const TString& path);

TString GetBaseName(const TString& path);

TString StripFileComponent(const TString& fileName);

class TExistenceChecker {
public:
    TExistenceChecker(bool strict = false)
        : Strict(strict)
    {
    }

    void SetStrict(bool strict) {
        Strict = strict;
    }

    bool IsStrict() const {
        return Strict;
    }

    const char* Check(const char* fname) const {
        if (!fname || !*fname) {
            return nullptr;
        }
        if (Strict) {
            NFs::EnsureExists(fname);
        } else if (!NFs::Exists(fname)) {
            fname = nullptr;
        }
        return fname;
    }

private:
    bool Strict;
};