0.6.0-1 release
[manu/suphp.git] / src / File.cpp
1 /*
2     suPHP - (c)2002-2005 Sebastian Marsching <sebastian@marsching.com>
3
4     This file is part of suPHP.
5
6     suPHP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     suPHP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with suPHP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "API_Helper.hpp"
22
23 #include "File.hpp"
24
25 using namespace suPHP;
26
27 bool suPHP::File::hasPermissionBit(FileMode perm) const throw (SystemException) {
28     return API_Helper::getSystemAPI().File_hasPermissionBit(*this, perm);
29 }
30
31 suPHP::File::File(std::string path) {
32     this->path = path;
33 }
34
35 std::string suPHP::File::getPath() const{
36     return this->path;
37 }
38
39 SmartPtr<std::ifstream> suPHP::File::getInputStream() throw (IOException) {
40     std::ifstream* infile = new std::ifstream();
41     infile->open(this->path.c_str());
42     if (infile->bad() || infile->fail()) {
43         throw IOException("Could not open file " + 
44                           this->path + " for reading", __FILE__, __LINE__);
45     }
46     return SmartPtr<std::ifstream>(infile);
47 }
48
49 bool suPHP::File::exists() const {
50     return API_Helper::getSystemAPI().File_exists(*this);
51 }
52
53 std::string suPHP::File::getRealPath() const throw (SystemException) {
54     return API_Helper::getSystemAPI().File_getRealPath(*this);
55 }
56
57 File suPHP::File::getParentDirectory() const {
58     std::string path = this->getPath();
59     path = path.substr(0, path.rfind('/'));
60     return File(path);
61 }
62
63 bool suPHP::File::hasUserReadBit() const throw (SystemException) {
64     return this->hasPermissionBit(FILEMODE_USER_READ);
65 }
66
67 bool suPHP::File::hasUserWriteBit() const throw (SystemException) {
68     return this->hasPermissionBit(FILEMODE_USER_WRITE);
69 }
70
71 bool suPHP::File::hasUserExecuteBit() const throw (SystemException) {
72     return this->hasPermissionBit(FILEMODE_USER_EXEC);
73 }
74
75 bool suPHP::File::hasGroupReadBit() const throw (SystemException) {
76     return this->hasPermissionBit(FILEMODE_GROUP_READ);
77 }
78
79 bool suPHP::File::hasGroupWriteBit() const throw (SystemException) {
80     return this->hasPermissionBit(FILEMODE_GROUP_WRITE);
81 }
82
83 bool suPHP::File::hasGroupExecuteBit() const throw (SystemException) {
84     return this->hasPermissionBit(FILEMODE_GROUP_EXEC);
85 }
86
87 bool suPHP::File::hasOthersReadBit() const throw (SystemException) {
88     return this->hasPermissionBit(FILEMODE_OTHERS_READ);
89 }
90
91 bool suPHP::File::hasOthersWriteBit() const throw (SystemException) {
92     return this->hasPermissionBit(FILEMODE_OTHERS_WRITE);
93 }
94
95 bool suPHP::File::hasOthersExecuteBit() const throw (SystemException) {
96     return this->hasPermissionBit(FILEMODE_OTHERS_EXEC);
97 }
98
99
100 UserInfo suPHP::File::getUser() const throw (SystemException) {
101     return API_Helper::getSystemAPI().File_getUser(*this);
102 }
103
104 GroupInfo suPHP::File::getGroup() const throw (SystemException) {
105     return API_Helper::getSystemAPI().File_getGroup(*this);
106 }