coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
fs_nix.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2007-2013 Mike Kinghan, imk@burroingroingjoing.com *
3  * All rights reserved. *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * *
12  * Redistributions in binary form must reproduce the above copyright *
13  * notice, this list of conditions and the following disclaimer in the *
14  * documentation and/or other materials provided with the distribution. *
15  * *
16  * Neither the name of Mike Kinghan nor the names of its contributors *
17  * may be used to endorse or promote products derived from this software *
18  * without specific prior written permission. *
19  * *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
31  * DAMAGE. *
32  * *
33  **************************************************************************/
34 
39 #include "platform.h"
40 
42 
43 #ifdef NIX
44 
45 #include "filesys.h"
46 #include "diagnostic.h"
47 #include "syserr.h"
48 #include "path.h"
49 #include <cstring>
50 #include <sys/stat.h>
51 #include <unistd.h>
52 #include <climits>
53 #include <cerrno>
54 
55 using namespace std;
56 namespace fs {
57 
58 string real_path(string const & relname)
59 {
60  char buf[PATH_MAX];
61  if (!realpath(relname.c_str(),buf)) {
62  error_cant_get_realpath() << "Can't obtain real path of \""
63  << relname << "\": " << strerror(errno) << emit();
64  }
65  return buf;
66 }
67 
68 string cwd()
69 {
70  char buf[PATH_MAX];
71  if (!getcwd(buf,PATH_MAX)) {
72  abend_cant_get_cwd() << "Can't obtain real path of "
73  "current working directory: " << strerror(errno) << emit();
74  }
75  return buf;
76 }
77 
78 bool is_absolute(std::string pathname)
79 {
80  return !pathname.empty() && pathname[0] == '/';
81 }
82 
83 obj_type_t obj_type(string const & name)
84 {
85  obj_type_t type = OBJ_NONE;
86  struct stat obj_info;
87  int res = lstat(name.c_str(),&obj_info);
88  if (!res) {
89  if (S_ISLNK(obj_info.st_mode)) {
90  type |= OBJ_SLINK;
91  }
92  res = stat(name.c_str(),&obj_info);
93  if (!res) {
94  if (S_ISREG(obj_info.st_mode)) {
95  type |= OBJ_FILE;
96  } else if (S_ISDIR(obj_info.st_mode)) {
97  type |= OBJ_DIR;
98  }
99  }
100  }
101  return type;
102 }
103 
104 void make_dir(std::string const & abs_path, bool recursive)
105 {
106  mode_t mode = S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH;
107  int result = 0;
108  if (recursive) {
109  path_t path(abs_path);
110  int elements = path.elements() - 1;
111  errno = 0;
112  for (int element = 0; !result && element < elements;) {
113  string prefix = path.segment(0,++element).c_str();
114  result = mkdir(prefix.c_str(),mode);
115  result *= (errno != EEXIST && errno != EISDIR);
116  }
117  }
118  if (!result) {
119  result = mkdir(abs_path.c_str(),mode);
120  result *= (errno != EEXIST && errno != EISDIR);
121  }
122  if (result) {
123  abend_cant_create_dir() << "Can't create directory \"" << abs_path
124  << "\": " << system_error_message(errno) << emit();
125  }
126 }
127 
128 permissions get_permissions(std::string const & filename)
129 {
130  permissions p = -1;
131  struct stat obj_info;
132  int res = stat(filename.c_str(),&obj_info);
133  if (!res) {
134  p = obj_info.st_mode & 0777;
135  }
136  return p;
137 }
138 
139 int set_permissions(std::string const & filename, permissions p)
140 {
141  return chmod(filename.c_str(),p & 0777);
142 }
143 
144 } // namespace fs
145 
146 #endif
147 
149 
150 /* EOF*/
A symbolic link.
Definition: filesys.h:53
unsigned obj_type_t
Abstract type of filesystem object types.
Definition: filesys.h:61
permissions get_permissions(std::string const &filename)
void make_dir(std::string const &abs_path, bool recursive=true)
Create a directory given an absolute path name.
A file.
Definition: filesys.h:55
std::string segment(size_t start=0, size_t len=std::string::npos) const
Get a sub-sequence of the path's elements as a string.
Definition: path.h:165
bool is_absolute(std::string pathname)
Say whether a filename is absolute or relative.
abend_msg< 16 > abend_cant_create_dir
Report can't create directory.
Definition: diagnostic.h:818
abend_msg< 15 > abend_cant_get_cwd
Report can't get the current working directory.
Definition: diagnostic.h:816
std::string cwd()
Get the absolute real pathname of the current working directory.
chew_mode::name const name
An exemplar chew_mode::name
Definition: chew.h:229
The tag class is inserted in a diagnostic_base to tell it to emit itself.
Definition: diagnostic.h:77
size_t elements() const
Get the number of elements in the path
Definition: path.h:101
int set_permissions(std::string const &filename, permissions p)
std::string abs_path(std::string const &filename)
Get the absolute pathname for a filename.
std::string real_path(std::string const &relname)
Get the absolute real pathname of a file or directory name.
obj_type_t obj_type(std::string const &name)
Get the type of the object putatively designated by a filename.
A directory.
Definition: filesys.h:57
error_msg< 16 > error_cant_get_realpath
Report failure from realpath()
Definition: diagnostic.h:756
No such object.
Definition: filesys.h:51
int permissions
Type of file permissions mask.
Definition: filesys.h:147
string system_error_message(unsigned errnum)
Get the system error message for a system error number.
Definition: syserr.cpp:89