41 "CREATE",
"TRUNCATE",
"EXCLUSIVE" };
46#define READ_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[1])
47#define WRITE_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[2])
48#define EXECUTE_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[3])
49#define NOTHING_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[0])
50#define SETUID_CHAR_CODE (OksSystem::File::FILE_FLAG_STR[4])
71 std::string::size_type colon = url.find(
COLON_CHAR);
72 if (colon == std::string::npos)
74 return url.substr(0, colon);
85 std::string::size_type dot = url.rfind(
DOT_CHAR);
86 if (dot == std::string::npos)
88 return url.substr(dot + 1);
99 std::string::size_type slash = url.rfind(
SLASH_CHAR);
100 if (slash == std::string::npos)
102 return url.substr(slash + 1);
115 std::string::size_type colon = url.find(
COLON_CHAR);
116 if (colon == std::string::npos)
118 return url.substr(colon + 1);
134 std::string rest = path.substr(1);
135 while (!rest.empty()) {
136 std::string::size_type slash = rest.find(
SLASH_CHAR);
137 if (slash == std::string::npos)
140 rest = rest.substr(slash + 1);
158 std::string::size_type nl = text.find(
'\n');
160 return text.substr(0, nl);
174 const std::string path =
uri(url);
183 char* wd_buffer = getcwd(0, MAXPATHLEN);
185 std::string
directory = std::string(wd_buffer);
189 std::string message(
"getting the current working directory");
190 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"getcwd", message.c_str());
197 const int status = ::chdir(path);
199 std::string message =
"on directory " + dir.
full_name();
200 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"chdir", message.c_str());
212 std::string::size_type slash = path.find(
'/');
213 const std::string user_name = path.substr(1, slash - 1);
214 const std::string rest_path = path.substr(slash);
216 if (!user_name.empty()) {
219 return user.
home() + rest_path;
243 return std::string(buffer);
255 std::ostringstream flag_str;
256 if (flags & O_WRONLY) {
258 }
else if (flags & O_RDWR) {
263 if (flags & O_NONBLOCK) {
266 if (flags & O_APPEND) {
269 if (flags & O_CREAT) {
272 if (flags & O_TRUNC) {
275 if (flags & O_EXCL) {
278 flag_str <<
"(" << flags <<
")";
279 return flag_str.str();
305 std::ostringstream size_str;
306 bool writen_something =
false;
307 for (
int i = 3; i >= 0; i--) {
308 const unsigned int size_unit =
unit(i);
309 if (
size > size_unit && !(cut_small && writen_something)) {
310 const int amount =
size / size_unit;
313 writen_something =
true;
316 return size_str.str();
339 std::string name = std::string(c_name);
356OksSystem::File::operator std::string()
const throw()
361OksSystem::File::operator
const char*()
const throw()
370OksSystem::File::operator bool()
const throw()
393OksSystem::File::operator size_t()
const
418 const char c = name[0];
419 std::string long_path;
431 char buffer[PATH_MAX];
432 const char* result = realpath(long_path.c_str(), buffer);
473 if (slash == std::string::npos || slash == 0)
510 char* tmp_name = tempnam(
m_full_name.c_str(), prefix.c_str());
512 std::string message =
"while creating a valid filename in directory " +
m_full_name;
513 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"tempnam", message.c_str());
528 struct stat file_status;
529 const int result = stat(
m_full_name.c_str(), &file_status);
544 struct stat file_status;
545 const int result = stat(
m_full_name.c_str(), &file_status);
547 return file_status.st_mode;
549 std::string message =
"on file/directory " +
m_full_name;
550 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"stat", message.c_str());
561 return (mode & 07777);
582 std::ostringstream stream;
583 stream.setf(std::ios::oct, std::ios::basefield);
595 struct stat file_status;
596 const int result = stat(
m_full_name.c_str(), &file_status);
598 return file_status.st_size;
599 std::string message =
"on file/directory " +
m_full_name;
600 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"stat", message.c_str());
610 struct stat file_status;
611 const int result = stat(
m_full_name.c_str(), &file_status);
613 return file_status.st_uid;
614 std::string message =
"on file/directory " +
m_full_name;
615 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"stat", message.c_str());
631 struct stat file_status;
632 const int result = stat(
m_full_name.c_str(), &file_status);
634 return file_status.st_gid;
635 std::string message =
"on file/directory " +
m_full_name;
636 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"stat", message.c_str());
648 return (mode & S_IFREG);
660 return (mode & S_IFDIR);
672 return (mode & S_IFIFO);
680 params.push_back(
"-b");
696 if (!directory_ptr) {
697 std::string message =
"on directory " +
m_full_name;
698 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"opendir", message.c_str());
701 struct dirent* directory_entry = readdir(directory_ptr);
702 if (directory_entry) {
703 const std::string name = directory_entry->d_name;
704 if ((name !=
".") && (name !=
"..")) {
707 file_vector.push_back(file);
713 int status = closedir(directory_ptr);
715 std::string message =
"on directory " +
m_full_name;
716 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"closedir", message.c_str());
740 std::string message =
"on directory " +
m_full_name;
741 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"rmdir", message.c_str());
754 for (file_list_t::const_iterator p = childs.begin(); p != childs.end(); p++) {
776 const int result =
::rename(source, dest);
779 throw OksSystem::RenameFileIssue(
ERS_HERE, errno, source, dest);
791 const int result = ::chmod(
m_full_name.c_str(), perm);
795 std::string message =
"on file/directory " +
m_full_name;
796 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"chmod", message.c_str());
812 const int result = ::mkdir(
m_full_name.c_str(), perm);
813 int mkdir_error = errno;
818 }
catch (OksSystem::OksSystemCallIssue& e) {
819 std::string message =
"on directory " +
m_full_name;
820 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"mkdir -> chmod", message.c_str());
828 }
catch (OksSystem::OksSystemCallIssue& e) {
834 std::string message =
"on directory " +
m_full_name;
835 throw OksSystem::OksSystemCallIssue(
ERS_HERE, mkdir_error,
"mkdir", message.c_str());
856 mode_t father_permission = perm | S_IRWXU;
892 const int status = ::mkfifo(
m_full_name.c_str(), perm);
897 std::string message =
"while creating FIFO " +
m_full_name;
898 throw OksSystem::OksSystemCallIssue(
ERS_HERE, errno,
"mkfifo", message.c_str());
911 std::ifstream* stream =
new std::ifstream(
m_full_name.c_str());
912 stream->exceptions(std::ios::failbit | std::ios::badbit);
914 }
catch (std::ios_base::failure& ex) {
930 std::ios::openmode mode = std::ios::out;
932 mode |= std::ios::app;
934 std::ofstream* stream =
new std::ofstream(
m_full_name.c_str(), mode);
935 stream->exceptions(std::ios::failbit | std::ios::badbit);
937 }
catch (std::ios_base::failure& ex) {
#define ERS_PRECONDITION(expression)
#define NOTHING_CHAR_CODE
#define EXECUTE_CHAR_CODE
bool operator==(const OksSystem::File &a, const OksSystem::File &b)
bool operator!=(const OksSystem::File &a, const OksSystem::File &b)
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
User()
constructor for current user