42#define READ_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[1])
43#define WRITE_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[2])
44#define EXECUTE_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[3])
45#define NOTHING_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[0])
46#define SETUID_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[4])
67 std::string::size_type colon = url.find(
COLON_CHAR);
68 if (colon==std::string::npos)
return std::string();
69 return url.substr(0,colon);
78 std::string::size_type dot = url.rfind(
DOT_CHAR);
79 if (dot==std::string::npos)
return std::string();
80 return url.substr(dot+1);
89 std::string::size_type slash = url.rfind(
SLASH_CHAR);
90 if (slash==std::string::npos)
return url;
91 return url.substr(slash+1);
102 std::string::size_type colon = url.find(
COLON_CHAR);
103 if (colon==std::string::npos)
return url;
104 return url.substr(colon+1);
119 std::string rest = path.substr(1);
120 while(! rest.empty()) {
121 std::string::size_type slash = rest.find(
SLASH_CHAR);
122 if (slash==std::string::npos)
return count;
124 rest = rest.substr(slash+1);
128 if (protocol(path) == FILE_PROTOCOL) {
129 return depth(uri(path));
140 std::string::size_type nl = text.find(
'\n');
141 if (nl>0)
return text.substr(0,nl);
152 std::string prot = protocol(url);
154 const std::string path = uri(url);
161 char *wd_buffer = getcwd(0,MAXPATHLEN);
163 std::string directory = std::string(wd_buffer);
167 std::string message(
"getting the current working directory");
168 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"getcwd", message.c_str() );
173 const int status = ::chdir(path);
175 std::string message =
"on directory " + dir.
full_name();
176 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"chdir", message.c_str() );
188 std::string::size_type slash = path.find(
'/');
189 const std::string user_name = path.substr(1,slash-1);
190 const std::string rest_path = path.substr(slash);
192 if (! user_name.empty()) {
195 return user.
home() + rest_path;
218 return std::string(buffer);
228 std::ostringstream flag_str;
229 if (flags & O_WRONLY) {
230 flag_str << HUMAN_OPEN_STR[1];
231 }
else if (flags & O_RDWR) {
232 flag_str << HUMAN_OPEN_STR[0] <<
"-" << HUMAN_OPEN_STR[1];
234 flag_str << HUMAN_OPEN_STR[0];
236 if (flags & O_NONBLOCK) {
237 flag_str <<
"/" << HUMAN_OPEN_STR[2];
239 if (flags & O_APPEND) {
240 flag_str <<
"/" << HUMAN_OPEN_STR[3];
242 if (flags & O_CREAT) {
243 flag_str <<
"/" << HUMAN_OPEN_STR[4];
245 if (flags & O_TRUNC) {
246 flag_str <<
"/" << HUMAN_OPEN_STR[5];
248 if (flags & O_EXCL) {
249 flag_str <<
"/" << HUMAN_OPEN_STR[6];
251 flag_str <<
"(" << flags <<
")";
252 return flag_str.str();
259 return KILOBYTE * unit(u-1);
272 if (0==size)
return "0";
273 std::ostringstream size_str;
274 bool writen_something =
false;
275 for(
int i=3;i>=0;i--) {
276 const unsigned int size_unit = unit(i);
277 if (size > size_unit && ! (cut_small && writen_something)) {
278 const int amount = size / size_unit;
279 size = size % size_unit;
280 size_str << amount <<
SPACE_CHAR << HUMAN_SIZE_STR[i];
281 writen_something =
true;
284 return size_str.str();
306 std::string name = std::string(c_name);
324OksSystem::File::operator std::string()
const throw() {
328OksSystem::File::operator
const char*()
const throw() {
329 return m_full_name.c_str();
336OksSystem::File::operator bool()
const throw() {
348 return (m_full_name == other.m_full_name);
356OksSystem::File::operator size_t()
const {
382 const char c = name[0];
383 std::string long_path;
389 long_path = expand_home(name);
392 long_path = working_directory() +
"/" + name;
395 char buffer[PATH_MAX];
396 const char* result = realpath(long_path.c_str(),buffer);
398 m_full_name = long_path;
400 m_full_name = buffer;
413 return m_full_name.c_str();
420 return short_name(m_full_name);
428 std::string::size_type size = m_full_name.size();
429 std::string::size_type slash = m_full_name.rfind(
SLASH_CHAR,size-1);
430 if (slash==std::string::npos || slash==0)
return (
"/");
431 return m_full_name.substr(0,slash);
437 return extension(m_full_name);
441 return depth(m_full_name);
447 return File(parent_name());
451 std::string child_name = m_full_name +
"/" + name;
456 char *tmp_name = tempnam(m_full_name.c_str(),prefix.c_str());
458 std::string message =
"while creating a valid filename in directory " + m_full_name;
459 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"tempnam", message.c_str() );
473 struct stat file_status;
474 const int result = stat(m_full_name.c_str(),&file_status);
475 if (0==result)
return true;
486 struct stat file_status;
487 const int result = stat(m_full_name.c_str(),&file_status);
489 return file_status.st_mode;
491 std::string message =
"on file/directory " + m_full_name;
492 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"stat", message.c_str() );
501 mode_t mode = get_mode();
502 return (mode & 07777);
510 return pretty_permissions(permissions());
519 std::ostringstream stream;
520 stream.setf(std::ios::oct,std::ios::basefield);
521 stream << permissions;
531 struct stat file_status;
532 const int result = stat(m_full_name.c_str(),&file_status);
533 if (0==result)
return file_status.st_size;
534 std::string message =
"on file/directory " + m_full_name;
535 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"stat", message.c_str() );
543 struct stat file_status;
544 const int result = stat(m_full_name.c_str(),&file_status);
545 if (0==result)
return file_status.st_uid;
546 std::string message =
"on file/directory " + m_full_name;
547 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"stat", message.c_str() );
551 return User(owner_id());
560 struct stat file_status;
561 const int result = stat(m_full_name.c_str(),&file_status);
562 if (0==result)
return file_status.st_gid;
563 std::string message =
"on file/directory " + m_full_name;
564 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"stat", message.c_str() );
574 const mode_t mode = get_mode();
575 return (mode & S_IFREG);
585 const mode_t mode = get_mode();
586 return (mode & S_IFDIR);
595 const mode_t mode = get_mode();
596 return (mode & S_IFIFO);
603 params.push_back(
"-b");
604 params.push_back(full_name());
605 return first_line(file_command.
pipe_in(params));
616 DIR* directory_ptr = opendir(m_full_name.c_str());
617 if (! directory_ptr) {
618 std::string message =
"on directory " + m_full_name;
619 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"opendir", message.c_str() );
622 struct dirent* directory_entry = readdir(directory_ptr);
623 if (directory_entry) {
624 const std::string name = directory_entry->d_name;
625 if ((name!=
".") && (name!=
"..")) {
626 std::string path = m_full_name +
"/" + name;
628 file_vector.push_back(file);
634 int status = closedir(directory_ptr);
636 std::string message =
"on directory " + m_full_name;
637 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"closedir", message.c_str() );
647 const int result = ::unlink(m_full_name.c_str());
648 if (0==result)
return;
649 throw OksSystem::RemoveFileIssue(
ERS_HERE, errno, m_full_name.c_str() );
653 const int result = ::rmdir(m_full_name.c_str());
654 if (0==result)
return;
655 std::string message =
"on directory " + m_full_name;
656 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"rmdir", message.c_str() );
666 if (is_directory()) {
668 for (file_list_t::const_iterator p=childs.begin();p!=childs.end();p++) {
687 const char *source = c_full_name() ;
689 const int result = ::rename(source,dest);
690 if (0==result)
return;
691 throw OksSystem::RenameFileIssue(
ERS_HERE, errno, source, dest );
700 if(perm != permissions()) {
701 const int result = ::chmod(m_full_name.c_str(),perm);
705 std::string message =
"on file/directory " + m_full_name;
706 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"chmod", message.c_str());
720 const int result = ::mkdir(m_full_name.c_str(),perm);
721 int mkdir_error = errno;
727 catch (OksSystem::OksSystemCallIssue &e){
728 std::string message =
"on directory " + m_full_name;
729 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"mkdir -> chmod", message.c_str() );
734 if (is_directory()) {
738 catch (OksSystem::OksSystemCallIssue &e){
744 std::string message =
"on directory " + m_full_name;
745 throw OksSystem::OksSystemCallIssue(
ERS_HERE, mkdir_error,
"mkdir", message.c_str());
762 const File father = parent();
764 mode_t father_permission = perm | S_IRWXU;
777 const File father = parent();
789 if(perm != permissions()) {
797 const int status = ::mkfifo(m_full_name.c_str(),perm);
802 std::string message =
"while creating FIFO " + m_full_name;
803 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"mkfifo", message.c_str() );
817 std::ifstream *stream =
new std::ifstream(m_full_name.c_str());
818 stream->exceptions(std::ios::failbit | std::ios::badbit);
820 }
catch (std::ios_base::failure &ex) {
821 throw OksSystem::OpenFileIssue(
ERS_HERE, errno, m_full_name.c_str(), ex );
834 std::ios::openmode mode = std::ios::out;
836 mode |= std::ios::app;
838 std::ofstream *stream =
new std::ofstream(m_full_name.c_str(),mode);
839 stream->exceptions(std::ios::failbit | std::ios::badbit);
841 }
catch (std::ios_base::failure &ex) {
842 throw OksSystem::OpenFileIssue(
ERS_HERE, errno, m_full_name.c_str(), ex );
862 return ! a.equals(b);
#define ERS_PRECONDITION(expression)
#define NOTHING_CHAR_CODE
#define EXECUTE_CHAR_CODE
std::ostream & operator<<(std::ostream &stream, const OksSystem::File &file)
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 */
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 */
OksSystem::File temporary(const std::string &prefix) const
static const char *const HUMAN_SIZE_STR[]
strings for pretty printing file sizes */
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 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 */
bool is_fifo() const
is the file a named pipe */
std::vector< OksSystem::File > file_list_t
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