allow proxy information to be url based instead of protocol based only,
[platform/upstream/libzypp.git] / zypp / PathInfo.h
index a13d6f8..e5ddf2d 100644 (file)
@@ -28,6 +28,8 @@ extern "C"
 #include <map>
 
 #include "zypp/Pathname.h"
+#include "zypp/CheckSum.h"
+#include "zypp/ByteCount.h"
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
@@ -242,6 +244,8 @@ namespace zypp
       const Pathname &    path()     const { return path_t; }
       /** Return current Pathname as String. */
       const std::string & asString() const { return path_t.asString(); }
+      /** Return current Pathname as C-string. */
+      const char * c_str()           const { return path_t.asString().c_str(); }
       /** Return current stat Mode. */
       Mode                mode()     const { return mode_e; }
       /** Return error returned from last stat/lstat call. */
@@ -438,6 +442,16 @@ namespace zypp
     int copy_dir( const Pathname & srcpath, const Pathname & destpath );
 
     /**
+     * Like 'cp -a srcpath/. destpath'. Copy the content of srcpath recursively
+     * into destpath. Both \p srcpath and \p destpath has to exists.
+     *
+     * @return 0 on success, ENOTDIR if srcpath/destpath is not a directory,
+     * EEXIST if srcpath and destpath are equal, otherwise the commands
+     * return value.
+     */
+    int copy_dir_content( const Pathname & srcpath, const Pathname & destpath);
+
+    /**
      * Return content of directory via retlist. If dots is false
      * entries starting with '.' are not reported. "." and ".."
      * are never reported.
@@ -475,6 +489,8 @@ namespace zypp
       : name( name_r )
       , type( type_r )
       {}
+
+      bool operator==( const DirEntry &rhs ) const;
     };
 
     /** Returned by readdir. */
@@ -492,12 +508,38 @@ namespace zypp
      **/
     int readdir( DirContent & retlist, const Pathname & path,
                  bool dots = true, PathInfo::Mode statmode = PathInfo::STAT );
+
+
+    /**
+     * Check if the specified directory is empty.
+     * \param path The path of the directory to check.
+     * \return 0 if directory is empty, -1 if not, errno > 0 on failure.
+     */
+    int is_empty_dir(const Pathname & path);
+
     //@}
 
     ///////////////////////////////////////////////////////////////////
     /** \name File related functions. */
     //@{
     /**
+     * Create an empty file if it does not yet exist. Make parent directories
+     * as needed. mode specifies the permissions to use. It is modified by the
+     * process's umask in the usual way.
+     *
+     * @return 0 on success, errno on failure
+     **/
+    int assert_file( const Pathname & path, unsigned mode = 0644 );
+
+    /**
+     * Change file's modification and access times.
+     *
+     * \return 0 on success, errno on failure
+     * \see man utime
+     */
+    int touch (const Pathname & path);
+
+    /**
      * Like '::unlink'. Delete a file (symbolic link, socket, fifo or device).
      *
      * @return 0 on success, errno on failure
@@ -511,6 +553,34 @@ namespace zypp
      **/
     int rename( const Pathname & oldpath, const Pathname & newpath );
 
+    /** Exchanges two files or directories.
+     *
+     * Most common use is when building a new config file (or dir)
+     * in a tempfile. After the job is done, configfile and tempfile
+     * are exchanged. This includes moving away the configfile in case
+     * the tempfile does not exist. Parent directories are created as
+     * needed.
+     *
+     * \note Paths are exchanged using \c ::rename, so take care both paths
+     * are located on the same filesystem.
+     *
+     * \code
+     * Pathname configfile( "/etc/myconfig" );
+     * TmpFile  newconfig( TmpFile::makeSibling( configfile ) );
+     * // now write the new config:
+     * std::ofstream o( newconfig.path().c_str() );
+     * o << "mew values << endl;
+     * o.close();
+     * // If everything is fine, exchange the files:
+     * exchange( newconfig.path(), configfile );
+     * // Now the old configfile is still available at newconfig.path()
+     * // until newconfig goes out of scope.
+     * \endcode
+     *
+     * @return 0 on success, errno on failure
+     */
+    int exchange( const Pathname & lpath, const Pathname & rpath );
+
     /**
      * Like 'cp file dest'. Copy file to destination file.
      *
@@ -536,6 +606,42 @@ namespace zypp
     int hardlink( const Pathname & oldpath, const Pathname & newpath );
 
     /**
+     * Create \a newpath as hardlink or copy of \a oldpath.
+     *
+     * @return 0 on success, errno on failure.
+     */
+    int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath );
+
+    /**
+     * Like '::readlink'. Return the contents of the symbolic link
+     * \a symlink_r via \a target_r.
+     *
+     * @return 0 on success, errno on failure.
+     */
+    int readlink( const Pathname & symlink_r, Pathname & target_r );
+    /** \overload Return an empty Pathname on error. */
+    inline Pathname readlink( const Pathname & symlink_r )
+    {
+      Pathname target;
+      readlink( symlink_r, target );
+      return target;
+    }
+
+    /**
+     * Recursively follows the symlink pointed to by \a path_r and returns
+     * the Pathname to the real file or directory pointed to by the link.
+     *
+     * There is a recursion limit of 256 iterations to protect against a cyclic
+     * link.
+     *
+     * @return Pathname of the file or directory pointed to by the given link
+     *   if it is a valid link. If \a path_r is not a link, an exact copy of
+     *   it is returned. If \a path_r is a broken or a cyclic link, an empty
+     *   Pathname is returned and the event logged.
+     */
+    Pathname expandlink( const Pathname & path_r );
+
+    /**
      * Like 'cp file dest'. Copy file to dest dir.
      *
      * @return 0 on success, EINVAL if file is not a file, ENOTDIR if dest
@@ -564,6 +670,20 @@ namespace zypp
     std::string sha1sum( const Pathname & file );
     //@}
 
+    /**
+     * Compute a files checksum
+     *
+     * @return the files checksum on success, otherwise an empty string..
+     **/
+    std::string checksum( const Pathname & file, const std::string &algorithm );
+
+    /**
+     * check files checksum
+     *
+     * @return true if the checksum matchs
+     **/
+    bool is_checksum( const Pathname & file, const CheckSum &checksum );
+
     ///////////////////////////////////////////////////////////////////
     /** \name Changing permissions. */
     //@{
@@ -573,6 +693,20 @@ namespace zypp
      * @return 0 on success, errno on failure
      **/
     int chmod( const Pathname & path, mode_t mode );
+
+    /**
+     * Add the \c mode bits to the file given by path.
+     *
+     * @return 0 on success, errno on failure
+     */
+    int addmod( const Pathname & path, mode_t mode );
+
+    /**
+     * Remove the \c mode bits from the file given by path.
+     *
+     * @return 0 on success, errno on failure
+     */
+    int delmod( const Pathname & path, mode_t mode );
     //@}
 
     ///////////////////////////////////////////////////////////////////
@@ -595,6 +729,31 @@ namespace zypp
      * \todo check cooperation with zypp::TmpFile and zypp::TmpDir
      **/
     int erase( const Pathname & path );
+
+    /**
+     * Report free disk space on a mounted file system.
+     *
+     * path is the path name of any file within the mounted filesystem.
+     *
+     * @return Free disk space or -1 on error.
+     **/
+    ByteCount df( const Pathname & path );
+
+    /**
+     * Get the current umask (file mode creation mask)
+     *
+     * @return The current umask
+     **/
+    mode_t getUmask();
+
+     /**
+     * Modify \c mode_r according to the current umask
+     * <tt>( mode_r & ~getUmask() )</tt>.
+     * \see \ref getUmask.
+     * @return The resulting permissions.
+     **/
+    inline mode_t applyUmaskTo( mode_t mode_r )
+    { return mode_r & ~getUmask(); }
     //@}
 
     /////////////////////////////////////////////////////////////////