~ubuntu-branches/ubuntu/quantal/config-manager/quantal

« back to all changes in this revision

Viewing changes to File.cc

  • Committer: Bazaar Package Importer
  • Author(s): Anand Kumria
  • Date: 2004-07-19 22:27:50 UTC
  • mto: (3.1.1 dapper)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040719222750-sztqdj1aoj2r6frr
Tags: upstream-0.1p83
ImportĀ upstreamĀ versionĀ 0.1p83

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (c) 2002 Robert Collins.
3
 
 *
4
 
 *     This program is free software; you can redistribute it and/or modify
5
 
 *     it under the terms of the GNU General Public License as published by
6
 
 *     the Free Software Foundation; either version 2 of the License, or
7
 
 *     (at your option) any later version.
8
 
 *
9
 
 *     A copy of the GNU General Public License can be found at
10
 
 *     http://www.gnu.org/
11
 
 *
12
 
 * Written by Robert Collins <robertc@hotmail.com>
13
 
 *
14
 
 */
15
 
 
16
 
#include "File.h"
17
 
#include "FileSystemVisitor.h"
18
 
#include <sys/stat.h>
19
 
#include <sys/types.h>
20
 
#include <sys/unistd.h>
21
 
#include <errno.h>
22
 
#include <iostream>
23
 
#include <fstream>
24
 
#include "Directory.h"
25
 
 
26
 
File::File (string const &path, bool const isASymlink, mode_t const &protection) : path_(path), symlink_(isASymlink), mode_(protection)
27
 
{
28
 
}
29
 
 
30
 
void
31
 
File::visit (FileSystemVisitor &aVisitor)
32
 
{
33
 
    aVisitor.visitFile (*this);
34
 
}
35
 
 
36
 
/* XXX: todo: optional chmod on the copied file.
37
 
 *            copy in binary mode
38
 
 *            do the Right Thing with Symlinks.
39
 
 */
40
 
void
41
 
File::Copy (string const &source, string const& target)
42
 
{
43
 
    if (Directory::Exists(source))
44
 
        throw (string) "Source was a directory (" + source + ")";
45
 
    ifstream ifs(source.c_str(), ios::in);
46
 
    if (!ifs.is_open())
47
 
        throw (string) "Can't open source file (" + source + ")";
48
 
 
49
 
    fstream ofs (target.c_str(), ios::out | ios::trunc);
50
 
    if (!ofs.is_open())
51
 
        throw (string) "Can't open target file (" + target + ")";
52
 
 
53
 
    /* string based copy */
54
 
    string s;
55
 
    while(getline(ifs, s))
56
 
      {
57
 
        if (!ifs.good() || !ofs.good())
58
 
            throw (string) "File::Copy Failed during copy";
59
 
        ofs << s << "\n";
60
 
      }
61
 
}