File: tmp_mgr.cc

package info (click to toggle)
zbackup 1.5-4
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 868 kB
  • sloc: cpp: 6,957; ansic: 468; python: 207; makefile: 10
file content (66 lines) | stat: -rw-r--r-- 1,419 bytes parent folder | download | duplicates (2)
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 & )
  {
  }
}