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

« back to all changes in this revision

Viewing changes to src/FileSystemComposite.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 "FileSystemComposite.h"
 
17
#include "Directory.h"
 
18
#include "File.h"
 
19
#include <sys/types.h>
 
20
#include <sys/stat.h>
 
21
#include <unistd.h>
 
22
#include <errno.h>
 
23
 
 
24
FileSystemComposite *
 
25
FileSystemComposite::CreateFromPath (string const &path)
 
26
{
 
27
    /* stat the path */
 
28
    struct stat statBuf;
 
29
    int statrv = lstat(path.c_str(), &statBuf);
 
30
    if (statrv)
 
31
        return NULL;
 
32
    
 
33
    /* FIXME: feed metadata into the composite constructors */
 
34
    if (S_ISDIR(statBuf.st_mode))
 
35
        return new Directory (path, statBuf.st_mode);
 
36
 
 
37
    if (S_ISREG(statBuf.st_mode))
 
38
        return new File (path, false, statBuf.st_mode);
 
39
 
 
40
    if (S_ISLNK(statBuf.st_mode))
 
41
        /* TODO: examine having a symlink child of File */
 
42
        return new File (path, true, statBuf.st_mode);
 
43
 
 
44
    throw (string) "FileSystemComposite::CreateFromPath: unsupported file type encountered";
 
45
}
 
46
 
 
47
bool
 
48
FileSystemComposite::Exists (string const &aPath)
 
49
{
 
50
    /* stat the path */
 
51
    struct stat statBuf;
 
52
    int statrv = stat(aPath.c_str(), &statBuf);
 
53
    if (statrv) {
 
54
        if (errno != ENOENT)
 
55
            throw (string) "Could not stat '" + aPath + "'.";
 
56
        return false;
 
57
    }
 
58
 
 
59
    return true;
 
60
}