forked from zbackup/zbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmp_mgr.hh
62 lines (49 loc) · 1.86 KB
/
tmp_mgr.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
// 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 TMP_MGR_HH_INCLUDED
#define TMP_MGR_HH_INCLUDED
#include <exception>
#include <string>
#include "dir.hh"
#include "ex.hh"
#include "file.hh"
#include "nocopy.hh"
#include "sptr.hh"
/// A temporary file
class TemporaryFile: NoCopy
{
public:
/// Returns the temporary file's file name. The file may already be existent -
/// it is supposed to be overwritten then
string const & getFileName() const;
/// Renames this temporary file over the given file name. If the destination
/// file exists already, it gets replaced if mayOverwrite is true, or throws
/// an exception otherwise
void moveOverTo( string const & destinationFileName, bool mayOverwrite = false );
/// Removes the file from the disk, unless moveOverTo() was called previously
~TemporaryFile();
private:
/// Use TmpMgr::makeTemporaryFile() instead of this constructor
TemporaryFile( string const & fileName );
string fileName;
friend class TmpMgr;
};
/// Allows creating temporary files and later either removing them or moving
/// them over to the target ones
class TmpMgr: NoCopy
{
string path;
public:
DEF_EX( Ex, "Temporary file manager exception", std::exception )
DEF_EX_STR( exCantCreate, "Can't create a temporary file in dir", Ex )
DEF_EX_STR( exWontOverwrite, "Won't overwrite existing file", Ex )
/// Creates the given directory if it doesn't exist already and uses it to
/// store temporary files.
TmpMgr( string const & path );
/// Creates an new empty temporary file and returns its full file name,
/// including the path. The file is then supposed to be overwritten
sptr< TemporaryFile > makeTemporaryFile();
/// Removes the temporary directory, if possible
~TmpMgr();
};
#endif