-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.h
119 lines (83 loc) · 3.84 KB
/
Utils.h
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
#ifndef UTILS_H
#define UTILS_H
#include "Types.h"
#include "BlockList.h"
#include <vector>
#include <string>
#include <fstream>
#include <stdio.h>
#include <sys/stat.h>
using namespace std;
namespace Utils {
// split string into in parts delimited by pattern
vecstr SplitStr (string Src, const string &Pat);
// remove all white space from beginning and end of string
void TrimStr (string *Str);
string TrimStr (string Str);
// join multiple strings into one with optional separater
string JoinStrs (const vecstr &Parts, const string &Sep = "");
// create full path name to file without extraneous relatime paths (don't prepend CWD)
string CanonizeFileNameNoCWD (const string &RawName);
// create full path name to file without extraneous relatime paths
string CanonizeFileName (const string &RawName, const string &CWD = "");
// open file stream for input
fstream OpenReadStream (const string &Name);
// open file stream for binary input
FILE * OpenReadBin (const string &Name);
// open file stream for output
fstream OpenWriteStream (const string &Name);
// open file stream for binary output
FILE * OpenWriteBin (const string &Name);
// read a line from a file stream, optionally die on eof
string ReadLine (fstream &Stream, bool DieOnEOF = 1);
// read unformatted data into byte buffer
int ReadBinary (FILE *F, char *Buf, int BufSize);
// read unformatted data into string
int ReadBinary (FILE *F, string &Str, int MaxSize);
// write unformatted data to file
void WriteBinary (FILE *F, const char *Buf, unsigned BufSize);
void WriteBinary (FILE *F, const string &Str);
// create a directory - optionally create needed subdirs
void CreateDir (const string Dir, bool CreateSubs = false);
// hard link one file to another
void MakeHardLink (const string &ExistingFile, const string &NewName);
// create a hardlinked copy of an existing block directory (FInfo or Chunks)
void CloneBlockDir (const string &DirName, const string &DstName, BlockList &BL);
// return all the subdirectories and files within a directory
void SlurpDir (const string &Dir, vecstr &SubDirs, vecstr &SubFiles);
// touch a file (create or update modification time)
void Touch (const string Name);
// link a file to a new name
void Link (const string &Name, const string &Target);
// extract standard stat type from archive file stats header
struct stat ParseStatsHeader (const string &Hdr);
// create archive file stats header from standard stat type
string CreateStatsHeader (const struct stat &Stats);
// convert u64 ns time to stats time structure
timespec NsToTimeSpec (u64 ns);
// convert stats time_t to ns time
u64 TimeSpecToNs (const timespec &T);
// return true if two timespecs are equal
bool TimeSpecsEqual (const timespec &T1, const timespec &T2);
// convert unix timespec to human readable text
string TimeSpecToText (const timespec &T);
// convert epoch nanoseconds to text time
string NsToText (u64 ns);
// get current time in ns
u64 TimeNowNs ();
// create a unix domain socket
void CreateSocket (const string &Name);
// set mode bits (really just permissions) of a file
void SetMode (const string &Name, mode_t Mode);
// set owner and group of a file
void SetOwn (const string &Name, u32 Uid, u32 Gid, bool IsSLink = false);
// set modification time on a file
void SetModTime (const string &Name, timespec Time);
// get file acls of one type
string GetFileAcl (const string &Name, u16 Perm, u32 type);
// get both types of file acls
string GetFileAcls (const string &Name, u16 Perm);
// set file acls from acrhived text string version
void SetFileAcls (const string &Name, u16 Perm, const string &Acls);
}
#endif // UTILS_H