47#define READ_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[1])
48#define WRITE_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[2])
49#define EXECUTE_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[3])
50#define NOTHING_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[0])
51#define SETUID_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[4])
72 std::string::size_type colon = url.find(
COLON_CHAR);
73 if (colon==std::string::npos)
return std::string();
74 return url.substr(0,colon);
83 std::string::size_type dot = url.rfind(
DOT_CHAR);
84 if (dot==std::string::npos)
return std::string();
85 return url.substr(dot+1);
94 std::string::size_type slash = url.rfind(
SLASH_CHAR);
95 if (slash==std::string::npos)
return url;
96 return url.substr(slash+1);
107 std::string::size_type colon = url.find(
COLON_CHAR);
108 if (colon==std::string::npos)
return url;
109 return url.substr(colon+1);
124 std::string rest = path.substr(1);
125 while(! rest.empty()) {
126 std::string::size_type slash = rest.find(
SLASH_CHAR);
127 if (slash==std::string::npos)
return count;
129 rest = rest.substr(slash+1);
145 std::string::size_type nl = text.find(
'\n');
146 if (nl>0)
return text.substr(0,nl);
159 const std::string path =
uri(url);
166 char *wd_buffer = getcwd(0,MAXPATHLEN);
168 std::string
directory = std::string(wd_buffer);
172 std::string message(
"getting the current working directory");
173 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"getcwd", message.c_str() );
178 const int status = ::chdir(path);
180 std::string message =
"on directory " + dir.
full_name();
181 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"chdir", message.c_str() );
193 std::string::size_type slash = path.find(
'/');
194 const std::string user_name = path.substr(1,slash-1);
195 const std::string rest_path = path.substr(slash);
197 if (! user_name.empty()) {
200 return user.
home() + rest_path;
223 return std::string(buffer);
233 std::ostringstream flag_str;
234 if (flags & O_WRONLY) {
236 }
else if (flags & O_RDWR) {
241 if (flags & O_NONBLOCK) {
244 if (flags & O_APPEND) {
247 if (flags & O_CREAT) {
250 if (flags & O_TRUNC) {
253 if (flags & O_EXCL) {
256 flag_str <<
"(" << flags <<
")";
257 return flag_str.str();
277 if (0==
size)
return "0";
278 std::ostringstream size_str;
279 bool writen_something =
false;
280 for(
int i=3;i>=0;i--) {
281 const unsigned int size_unit =
unit(i);
282 if (
size > size_unit && ! (cut_small && writen_something)) {
283 const int amount =
size / size_unit;
286 writen_something =
true;
289 return size_str.str();
311 std::string name = std::string(c_name);
329OksSystem::File::operator std::string()
const throw() {
333OksSystem::File::operator
const char*()
const throw() {
341OksSystem::File::operator bool()
const throw() {
361OksSystem::File::operator size_t()
const {
387 const char c = name[0];
388 std::string long_path;
400 char buffer[PATH_MAX];
401 const char* result = realpath(long_path.c_str(),buffer);
435 if (slash==std::string::npos || slash==0)
return (
"/");
461 char *tmp_name = tempnam(
m_full_name.c_str(),prefix.c_str());
463 std::string message =
"while creating a valid filename in directory " +
m_full_name;
464 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"tempnam", message.c_str() );
478 struct stat file_status;
479 const int result = stat(
m_full_name.c_str(),&file_status);
480 if (0==result)
return true;
491 struct stat file_status;
492 const int result = stat(
m_full_name.c_str(),&file_status);
494 return file_status.st_mode;
496 std::string message =
"on file/directory " +
m_full_name;
497 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"stat", message.c_str() );
507 return (mode & 07777);
524 std::ostringstream stream;
525 stream.setf(std::ios::oct,std::ios::basefield);
536 struct stat file_status;
537 const int result = stat(
m_full_name.c_str(),&file_status);
538 if (0==result)
return file_status.st_size;
539 std::string message =
"on file/directory " +
m_full_name;
540 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"stat", message.c_str() );
548 struct stat file_status;
549 const int result = stat(
m_full_name.c_str(),&file_status);
550 if (0==result)
return file_status.st_uid;
551 std::string message =
"on file/directory " +
m_full_name;
552 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"stat", message.c_str() );
565 struct stat file_status;
566 const int result = stat(
m_full_name.c_str(),&file_status);
567 if (0==result)
return file_status.st_gid;
568 std::string message =
"on file/directory " +
m_full_name;
569 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"stat", message.c_str() );
580 return (mode & S_IFREG);
591 return (mode & S_IFDIR);
601 return (mode & S_IFIFO);
608 params.push_back(
"-b");
622 if (! directory_ptr) {
623 std::string message =
"on directory " +
m_full_name;
624 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"opendir", message.c_str() );
627 struct dirent* directory_entry = readdir(directory_ptr);
628 if (directory_entry) {
629 const std::string name = directory_entry->d_name;
630 if ((name!=
".") && (name!=
"..")) {
633 file_vector.push_back(file);
639 int status = closedir(directory_ptr);
641 std::string message =
"on directory " +
m_full_name;
642 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"closedir", message.c_str() );
653 if (0==result)
return;
659 if (0==result)
return;
660 std::string message =
"on directory " +
m_full_name;
661 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"rmdir", message.c_str() );
673 for (file_list_t::const_iterator p=childs.begin();p!=childs.end();p++) {
694 const int result =
::rename(source,dest);
695 if (0==result)
return;
696 throw OksSystem::RenameFileIssue(
ERS_HERE, errno, source, dest );
706 const int result = ::chmod(
m_full_name.c_str(),perm);
710 std::string message =
"on file/directory " +
m_full_name;
711 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"chmod", message.c_str());
725 const int result = ::mkdir(
m_full_name.c_str(),perm);
726 int mkdir_error = errno;
732 catch (OksSystem::OksSystemCallIssue &e){
733 std::string message =
"on directory " +
m_full_name;
734 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"mkdir -> chmod", message.c_str() );
743 catch (OksSystem::OksSystemCallIssue &e){
749 std::string message =
"on directory " +
m_full_name;
750 throw OksSystem::OksSystemCallIssue(
ERS_HERE, mkdir_error,
"mkdir", message.c_str());
769 mode_t father_permission = perm | S_IRWXU;
802 const int status = ::mkfifo(
m_full_name.c_str(),perm);
807 std::string message =
"while creating FIFO " +
m_full_name;
808 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"mkfifo", message.c_str() );
822 std::ifstream *stream =
new std::ifstream(
m_full_name.c_str());
823 stream->exceptions(std::ios::failbit | std::ios::badbit);
825 }
catch (std::ios_base::failure &ex) {
839 std::ios::openmode mode = std::ios::out;
841 mode |= std::ios::app;
843 std::ofstream *stream =
new std::ofstream(
m_full_name.c_str(),mode);
844 stream->exceptions(std::ios::failbit | std::ios::badbit);
846 }
catch (std::ios_base::failure &ex) {
867 return ! a.equals(b);
#define ERS_PRECONDITION(expression)
#define NOTHING_CHAR_CODE
#define EXECUTE_CHAR_CODE
void operator<<(TraceStreamer &x, const ers::Issue &r)
Wrapper for executable file manipulation.
std::string pipe_in(const param_collection ¶ms) const
run the executable and pipe results back
std::vector< std::string > param_collection
Wrapper for file operations.
static std::string protocol(const std::string &url)
extracts the protocol part of an url */
static const int KILOBYTE
number of bytes in a kilobyte */
std::string short_name() const
short name for file */
void unlink() const
deletes (unlinks) file */
bool is_directory() const
is the file a regular file */
void make_path(mode_t permissions) const
creates a full path */
static std::string uri(const std::string &url)
extracts the local path of an url */
static const char *const FILE_PROTOCOL
string for the file protocol */
static std::string short_name(const std::string &url)
extracts the short file name of an url */
bool exists() const
does the file exist */
User owner() const
owner of the file */
OksSystem::File parent() const
parent of the current file */
void rmdir() const
deletes directory */
file_list_t directory() const
list of file in directory */
std::string extension() const
extension for file */
mode_t permissions() const
permissions for the file */
std::istream * input() const
returns an input stream from the file*/
std::string parent_name() const
path of directory containing file */
File(const std::string &name)
static std::string pretty_open_flag(int flags)
pretty prints open flags */
static File from_url(const std::string &url)
build a file out of an URL */
static const char *const HUMAN_OPEN_STR[]
strings for pretty printing open flags */
std::string file_type() const
type of the file */
int depth() const
depth of the file */
static std::string first_line(const std::string &text)
extracts the first line of a text */
size_t size() const
size of file */
static int unit(int u)
calculates the value of a computer unit of order n (i.e KB,GB, etc */
static const char *const FILE_COMMAND_PATH
static std::string to_string(mode_t permissions)
converts permission to string */
void ensure_path(mode_t permissions) const
creates the parent path */
void rename(const File &other) const
rename or moves the file */
void remove() const
recursively delete files and directories */
static std::string pretty_permissions(mode_t permissions)
pretty prints permissions */
OksSystem::File temporary(const std::string &prefix) const
static const char *const HUMAN_SIZE_STR[]
strings for pretty printing file sizes */
std::vector< OksSystem::File > file_list_t
const char * c_full_name() const
static std::string working_directory()
current directory of process */
void make_dir(mode_t permissions) const
creates a directory */
static int depth(const std::string &path)
calculates depth of a path */
static std::string pretty_size(size_t size, bool cut_small)
pretty prints a file size */
uid_t owner_id() const
owner id of file */
void make_fifo(mode_t permissions) const
creates a FIFO (named pipe) */
bool equals(const File &other) const
compare two files */
OksSystem::File child(const std::string &name) const
named child of the current directory */
void set_name(const std::string &name)
sets the name of the file */
static const char *const FILE_FLAG_STR
column headers for display of permissions */
gid_t group() const
group of file */
const std::string & full_name() const
full name for file */
static std::string expand_home(const std::string path)
resolve home directory */
std::ostream * output(bool append=false) const
returns an output stream to the file*/
mode_t get_mode() const
get mode associated with file (permission + type) */
std::string m_full_name
full name (path) of the file */
static std::string extension(const std::string &url)
extracts the extension of a path or an url */
bool is_fifo() const
is the file a named pipe */
bool is_regular() const
is the file a directory */
std::string pretty_permissions() const
pretty permissions for the file */
const std::string & home() const
home directory name
bool operator!=(const Host &a, const Host &b)
inequality operator
bool operator==(const Host &a, const Host &b)
equality operator