forked from zbackup/zbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.hh
91 lines (67 loc) · 2.14 KB
/
dir.hh
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
// Copyright (c) 2012-2014 Konstantin Isakov <[email protected]> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
#ifndef DIR_HH_INCLUDED
#define DIR_HH_INCLUDED
#include <dirent.h>
#include <sys/types.h>
#include <exception>
#include <string>
#include "ex.hh"
#include "nocopy.hh"
using std::string;
/// Directory-related operations
namespace Dir {
DEF_EX( Ex, "Directory exception", std::exception )
DEF_EX_STR( exCantCreate, "Can't create directory", Ex )
DEF_EX_STR( exCantRemove, "Can't remove directory", Ex )
DEF_EX_STR( exCantList, "Can't list directory", Ex )
DEF_EX_STR( exCantGetRealPath, "Can't real path of", Ex )
/// Checks whether the given dir exists or not
bool exists( string const & );
/// Creates the given directory
void create( string const & );
/// Removes the given directory. It must be empty to be removed
void remove( string const & );
/// Adds one path to another, e.g. for /hello/world and baz/bar, returns
/// /hello/world/baz/bar
string addPath( string const & first, string const & second );
/// Returns the canonicalized absolute pathname with symlinks resolved
string getRealPath( string const & );
/// Returns the directory part of the given path
string getDirName( string const & );
/// Returns the final path component part of the given path
string getBaseName( string const & );
/// Checkes whether directory is empty
bool isDirEmpty( string const & );
/// A separator used to separate names in the path.
inline char separator()
{ return '/'; }
class Entry
{
string fileName;
bool dir;
bool symlink;
public:
Entry() {}
Entry( string const & fileName, bool dir, bool symlink ):
fileName( fileName ), dir( dir ), symlink( symlink ) {}
string const & getFileName() const
{ return fileName; }
bool isDir() const
{ return dir; }
bool isSymLink() const
{ return symlink; }
};
/// Allows listing the directory
class Listing: NoCopy
{
string dirName;
DIR * dir;
public:
Listing( string const & dirName );
~Listing();
/// Return true if entry was filled, false if end of dir was encountered
bool getNext( Entry & );
};
}
#endif