- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gdirectory_unix.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
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 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // gdirectory_unix.cpp
19 //
20
21 #include "gdef.h"
22 #include "gdirectory.h"
23 #include "gprocess.h"
24 #include "gdatetime.h"
25 #include "gdebug.h"
26 #include "glog.h"
27 #include <sstream>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <dirent.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33
34 namespace G
35 {
36 class DirectoryIteratorImp ;
37 }
38
39 bool G::Directory::valid( bool for_creation ) const
40 {
41 bool rc = true ;
42 struct stat statbuf ;
43 if( ::stat( m_path.str().c_str() , &statbuf ) )
44 {
45 rc = false ; // doesnt exist
46 }
47 else if( !(statbuf.st_mode & S_IFDIR) )
48 {
49 rc = false ; // not a directory
50 }
51 else
52 {
53 DIR * p = ::opendir( m_path.str().c_str() ) ;
54 if( p == NULL )
55 rc = false ; // cant open directory for reading
56 else
57 ::closedir( p ) ;
58 }
59
60 if( rc && for_creation )
61 {
62 // (not definitive -- see also GNU/Linux ::euidaccess())
63 if( 0 != ::access( m_path.str().c_str() , W_OK ) )
64 rc = false ;
65 }
66 return rc ;
67 }
68
69 std::string G::Directory::tmp()
70 {
71 std::ostringstream ss ;
72 ss << "." << DateTime::now() << "." << Process::Id() << ".tmp" ;
73 return ss.str() ;
74 }
75
76 bool G::Directory::writeable( std::string tmp_filename ) const
77 {
78 G::Path test_file( m_path ) ;
79 if( tmp_filename.empty() ) tmp_filename = tmp() ;
80 test_file.pathAppend( tmp_filename ) ;
81
82 int fd = ::open( test_file.str().c_str() , O_WRONLY | O_CREAT | O_EXCL , S_IRWXU ) ;
83 if( fd == -1 )
84 {
85 return false ;
86 }
87 ::close( fd ) ;
88 bool ok = 0 == ::unlink( test_file.str().c_str() ) ;
89 return ok ;
90 }
91
G::Path::str
std::string str() const
Returns the path string.
Definition: gpath.cpp:135
G::DateTime::now
static EpochTime now()
Returns the current epoch time.
Definition: gdatetime.cpp:34
gdirectory.h
glog.h
G::Process::Id::str
std::string str() const
Definition: gprocess_unix.cpp:236
G::Directory::valid
bool valid(bool for_creating_files=false) const
Returns true if the object represents a valid directory.
Definition: gdirectory_unix.cpp:39
G::Directory::writeable
bool writeable(std::string probe_filename=tmp()) const
Tries to create and then delete an empty test file in the directory.
Definition: gdirectory_unix.cpp:76
G
Low-level classes.
gprocess.h
G::Directory::tmp
static std::string tmp()
A convenience function for constructing a filename for writeable().
Definition: gdirectory_unix.cpp:69
gdatetime.h
G::Process::Id
Process-id class.
Definition: gprocess.h:52
G::Path::pathAppend
void pathAppend(const std::string &tail)
Appends a filename to the path.
Definition: gpath.cpp:301
G::Path
A Path object represents a file system path.
Definition: gpath.h:44
gdebug.h