+/*
+The software in this package is distributed under the GNU General
+Public License version 2 (with a special exception described below).
+
+A copy of GNU General Public License (GPL) is included in this distribution,
+in the file COPYING.GPL.
+
+As a special exception, if other files instantiate templates or use macros
+or inline functions from this file, or you compile this file and link it
+with other works to produce a work based on this file, this file
+does not by itself cause the resulting work to be covered
+by the GNU General Public License.
+
+However the source code for this file must still be made available
+in accordance with section (3) of the GNU General Public License.
+
+This exception does not invalidate any other reasons why a work based
+on this file might be covered by the GNU General Public License.
+*/
/***************************************************************************
* Copyright (C) 2004-2008 by Intra2net AG *
- * info@intra2net.com *
* *
***************************************************************************/
#ifndef __FILEFUNC_HXX
#define __FILEFUNC_HXX
-#include "userfunc.hxx"
+#include "userfunc.hpp"
+#include <set>
-namespace i2n
-{
+// make sure we have proper large file support
+#include <features.h>
+#ifndef _GNU_SOURCE
+ #define _GNU_SOURCE
+#endif
+namespace I2n
+{
/**
* @brief helper class representing a file state.
*
*/
class Stat
{
- public:
- Stat();
- Stat(const std::string& path, bool follow_links= true);
- ~Stat();
-
- void recheck();
-
- bool isValid() const { return m_valid; }
-
- bool exists() const { return m_valid; }
- std::string path() const { return m_path; }
-
- dev_t device() const { return m_device; }
- ino_t inode() const { return m_inode; }
- mode_t mode() const { return m_mode; }
- nlink_t nlink() const { return m_num_links; }
- uid_t uid() const { return m_uid; }
- gid_t gid() const { return m_gid; }
- dev_t rdev() const { return m_device_type; }
- off_t size() const { return m_size; }
- time_t atime() const { return m_atime; }
- time_t mtime() const { return m_mtime; }
- time_t ctime() const { return m_ctime; }
-
- nlink_t numHardLinks() const { return m_num_links; }
- dev_t deviceType() const { return m_device_type; }
- time_t lastModifiedTime() const { return m_mtime; }
- time_t createdTime() const { return m_ctime; }
-
- /*
- ** unix style like type queries:
- */
-
- bool isLnk() const { return m_is_link; }
- bool isReg() const { return m_is_regular; }
- bool isDir() const { return m_is_directory; }
- bool isChr() const { return m_is_character_device; }
- bool isBlk() const { return m_is_block_device; }
- bool isFifo() const { return m_is_fifo; }
- bool isSock() const { return m_is_socket; }
-
- /*
- ** readable style type queries:
- */
-
- bool isLink () const { return m_is_link; }
- bool isRegular() const { return m_is_regular; }
- bool isRegularFile() const { return m_is_regular; }
- bool isDirectory() const { return m_is_directory; }
- bool isCharacterDevice() const {return m_is_character_device; }
- bool isBlockDevice() const { return m_is_block_device; }
- bool isFIFO() const { return m_is_fifo; }
- bool isSocket() const { return m_is_socket; }
-
- bool isDevice() const { return m_is_character_device or m_is_block_device; }
-
-
- /*
- ** "high level" queries
- */
-
- bool isSameAs(const Stat& rhs);
- bool isSameDeviceAs(const Stat& rhs);
-
- /*
- ** convenience methods
- */
-
- operator bool() const { return m_valid; }
-
- protected:
-
- void stat(const std::string& path, bool follow_links= true);
- void clear();
-
- protected:
- std::string m_path;
- bool m_follow_links;
-
- bool m_valid;
-
- dev_t m_device;
- ino_t m_inode;
- mode_t m_mode;
- nlink_t m_num_links;
- uid_t m_uid;
- gid_t m_gid;
- dev_t m_device_type;
- off_t m_size;
- time_t m_atime;
- time_t m_mtime;
- time_t m_ctime;
-
- bool m_is_link : 1;
- bool m_is_regular : 1;
- bool m_is_directory : 1;
- bool m_is_character_device : 1;
- bool m_is_block_device : 1;
- bool m_is_fifo : 1;
- bool m_is_socket : 1;
+public:
+ Stat();
+ Stat(const std::string& path, bool follow_links= true);
+ ~Stat();
+
+ void recheck();
+
+ bool is_valid() const { return Valid; }
+
+ bool exists() const { return Valid; }
+ std::string path() const { return Path; }
+
+ dev_t device() const { return Device; }
+ ino_t inode() const { return Inode; }
+ mode_t mode() const { return Mode; }
+ nlink_t nlink() const { return NumLinks; }
+ uid_t uid() const { return Uid; }
+ gid_t gid() const { return Gid; }
+ dev_t rdev() const { return DeviceType; }
+ off_t size() const { return Size; }
+ time_t atime() const { return Atime; }
+ time_t mtime() const { return Mtime; }
+ time_t ctime() const { return Ctime; }
+
+ // bytes used on disk, calculated from allocated blocks
+ long long bytes_on_disk() const { return BytesOnDisk; }
+
+ nlink_t num_hard_links() const { return NumLinks; }
+ dev_t device_type() const { return DeviceType; }
+ time_t last_modified_time() const { return Mtime; }
+ time_t created_time() const { return Ctime; }
+
+ /*
+ ** unix style like type queries:
+ */
+
+ bool is_lnk() const { return IsLink; }
+ bool is_reg() const { return IsRegular; }
+ bool is_dir() const { return IsDirectory; }
+ bool is_chr() const { return IsCharacterDevice; }
+ bool is_blk() const { return IsBlockDevice; }
+ bool is_fifo() const { return IsFifo; }
+ bool is_sock() const { return IsSocket; }
+
+ /*
+ ** readable style type queries:
+ */
+
+ bool is_link () const { return IsLink; }
+ bool is_regular() const { return IsRegular; }
+ bool is_regular_file() const { return IsRegular; }
+ bool is_directory() const { return IsDirectory; }
+ bool is_character_device() const {return IsCharacterDevice; }
+ bool is_block_device() const { return IsBlockDevice; }
+ bool is_socket() const { return IsSocket; }
+
+ bool is_device() const { return IsCharacterDevice or IsBlockDevice; }
+
+
+ /*
+ ** "high level" queries
+ */
+
+ bool is_same_as(const Stat& rhs);
+ bool is_same_device_as(const Stat& rhs);
+
+ /*
+ ** convenience methods
+ */
+
+ operator bool() const { return Valid; }
+
+protected:
+
+ void stat(const std::string& path, bool follow_links= true);
+ void clear();
+
+protected:
+ std::string Path;
+ bool FollowLinks;
+
+ bool Valid;
+
+ dev_t Device;
+ ino_t Inode;
+ mode_t Mode;
+ nlink_t NumLinks;
+ uid_t Uid;
+ gid_t Gid;
+ dev_t DeviceType;
+ off_t Size;
+ long long BytesOnDisk;
+ time_t Atime;
+ time_t Mtime;
+ time_t Ctime;
+
+ bool IsLink : 1;
+ bool IsRegular : 1;
+ bool IsDirectory : 1;
+ bool IsCharacterDevice : 1;
+ bool IsBlockDevice : 1;
+ bool IsFifo : 1;
+ bool IsSocket : 1;
}; // eo class Stat
/*
bool path_exists(const std::string& path);
bool file_exists(const std::string& path);
long file_size (const std::string &name);
+bool file_content_differs(const std::string &old_filename, const std::string &new_filename);
-time_t fileMTime(const std::string& path);
+time_t file_mtime(const std::string& path);
-bool getDir(const std::string& path, std::vector< std::string >& result, bool include_dot_names= false );
-std::vector< std::string > getDir(const std::string& path, bool include_dot_names= false );
+bool get_dir(const std::string& path, std::vector< std::string >& result, bool include_dot_names= false );
+std::vector< std::string > get_dir(const std::string& path, bool include_dot_names= false );
+int get_dir_count(const std::string& path, bool include_dot_names= false );
bool unlink(const std::string& path);
bool symlink(const std::string& target, const std::string& link_name, bool force= false);
-std::string readLink(const std::string& path);
-
+std::string read_link(const std::string& path);
-std::string readFile(const std::string& path);
+std::string read_file(const std::string& path);
-bool writeFile(const std::string& path, const std::string& data);
+bool write_file(const std::string& path, const std::string& data, bool trunc=true);
+bool copy_file(const std::string& src, const std::string& dest);
+bool copy_stream(std::istream& is, std::ostream& os);
std::string basename(const std::string& path);
std::string dirname(const std::string& path);
-std::string normalizePath(const std::string& path);
+std::string normalize_path(const std::string& path);
+
+bool dirsync(const std::string& path);
bool chmod(const std::string& path, int mode);
-bool chown(const std::string& path, const i2n::User& user, const i2n::Group& group= i2n::Group());
+bool chown(const std::string& path, const I2n::User& user, const I2n::Group& group= I2n::Group());
+
+bool recursive_delete(const std::string &path,
+ bool keep_parent_dir=false,
+ std::string *error=NULL);
+
+std::string mkdtemp(const std::string &path_template, std::string *error=NULL);
+// mkstemp: see tmpfstream
+
+bool mkdir(const std::string &path, const mode_t &mode=0700, std::string *error=NULL);
+bool rmdir(const std::string &path, std::string *error=NULL);
+
+std::string getcwd();
+bool chdir(const std::string &path, std::string *error=NULL);
-bool recursive_delete(const std::string &path, std::string *error=NULL);
+mode_t umask(mode_t mask);
+
+long long get_free_diskspace(const std::string& path);
+long long du(const std::string &path, std::string *error=NULL);
+
+/*
+** more specialized tool function(s)
+*/
+bool remove_unlisted_files(const std::string &directory,
+ const std::set<std::string> &keep_files,
+ const std::string &prefix="");
}