forked from zbackup/zbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmp_mgr.cc
66 lines (53 loc) · 1.39 KB
/
tmp_mgr.cc
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
// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
#include "tmp_mgr.hh"
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include "dir.hh"
#include "file.hh"
TemporaryFile::TemporaryFile( string const & fileName ): fileName( fileName )
{
}
void TemporaryFile::moveOverTo( string const & destinationFileName,
bool mayOverwrite )
{
if ( !mayOverwrite && File::exists( destinationFileName ) )
throw TmpMgr::exWontOverwrite( destinationFileName );
File::rename( fileName, destinationFileName );
fileName.clear();
}
TemporaryFile::~TemporaryFile()
{
if ( !fileName.empty() )
File::erase( fileName );
}
string const & TemporaryFile::getFileName() const
{
return fileName;
}
TmpMgr::TmpMgr( string const & path ): path( path )
{
if ( !Dir::exists( path ) )
Dir::create( path );
}
sptr< TemporaryFile > TmpMgr::makeTemporaryFile()
{
string name( Dir::addPath( path, "XXXXXX") );
int fd = mkstemp( &name[ 0 ] );
if ( fchmod ( fd, S_IRUSR | S_IWUSR | S_IRGRP ) != 0 )
throw exCantCreate( path );
if ( fd == -1 || close( fd ) != 0 )
throw exCantCreate( path );
return new TemporaryFile( name );
}
TmpMgr::~TmpMgr()
{
try
{
Dir::remove( path );
}
catch( Dir::exCantRemove & )
{
}
}