Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / PathInfo.h
index 15c3344..0a189cb 100644 (file)
@@ -35,6 +35,8 @@ extern "C"
 namespace zypp
 { /////////////////////////////////////////////////////////////////
 
+  class StrMatcher;
+
   ///////////////////////////////////////////////////////////////////
   /** Types and functions for filesystem operations.
    * \todo move zypp::filesystem stuff into separate header
@@ -357,8 +359,8 @@ namespace zypp
       dev_t  dev()     const { return isExist() ? statbuf_C.st_dev  : 0; }
       dev_t  rdev()    const { return isExist() ? statbuf_C.st_rdev : 0; }
 
-      unsigned int major() const;
-      unsigned int minor() const;
+      unsigned int devMajor() const;
+      unsigned int devMinor() const;
       //@}
 
       /** \name Size info. */
@@ -452,6 +454,51 @@ namespace zypp
     int copy_dir_content( const Pathname & srcpath, const Pathname & destpath);
 
     /**
+     * Convenience returning <tt>StrMatcher( "[^.]*", Match::GLOB )</tt>
+     * \see \ref dirForEach
+     */
+    const StrMatcher & matchNoDots();
+
+    /**
+     * Invoke callback function \a fnc_r for each entry in directory \a dir_r.
+     *
+     * If \a fnc_r is a \c NULL function \c 0 is returned immediately without even
+     * testing or accessing \a dir_r.
+     *
+     * Otherwise \c ::readdir is used to read the name of every entry in \a dir_r,
+     * omitting  \c '.' and \c '..'. \a dir_r and the current entries name are passed
+     * as arguments to \a fnc_r. If \a fnc_r returns \c false processing is aborted.
+     *
+     * @return 0 on success, -1 if aborted by callback, errno > 0 on ::readdir failure.
+     */
+    int dirForEach( const Pathname & dir_r, function<bool(const Pathname &, const char *const)> fnc_r );
+
+    /**
+     * \overload taking a \ref StrMatcher to filter the entries for which \a fnc_r is invoked.
+     *
+     * For convenience a \ref StrMatcher \ref matchNoDots is provided in this namespace.</tt>
+     *
+     * \code
+     *   bool cbfnc( const Pathname & dir_r, const char *const str_r )
+     *   {
+     *     D BG <*< " - " << dir_r/str_r << endl;
+     *     return true;
+     *   }
+     *   // Print no-dot files in "/tmp" via callback
+     *   filesystem::dirForEach( "/tmp", filesystem::matchNoDots(), cbfnc );
+     *
+     *   // same via lambda
+     *   filesystem::dirForEach( "/tmp", filesystem::matchNoDots(),
+     *                           [](const Pathname & dir_r, const std::string & str_r)->bool
+     *                           {
+     *                             DBG << " - " << dir_r/str_r << endl;
+     *                             return true;
+     *                           });
+     * \endcode
+     */
+    int dirForEach( const Pathname & dir_r, const StrMatcher & matcher_r, function<bool(const Pathname &, const char *const)> fnc_r );
+
+    /**
      * Return content of directory via retlist. If dots is false
      * entries starting with '.' are not reported. "." and ".."
      * are never reported.
@@ -489,11 +536,18 @@ namespace zypp
       : name( name_r )
       , type( type_r )
       {}
+
+      bool operator==( const DirEntry &rhs ) const;
     };
 
+    inline std::ostream & operator<<( std::ostream & str, const DirEntry & obj )
+    { return str << '[' << obj.type << "] " << obj.name; }
+
     /** Returned by readdir. */
     typedef std::list<DirEntry> DirContent;
 
+    std::ostream & operator<<( std::ostream & str, const DirContent & obj );
+
     /**
      * Return content of directory via retlist. If dots is false
      * entries starting with '.' are not reported. "." and ".."
@@ -507,7 +561,6 @@ 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.
@@ -521,6 +574,15 @@ namespace zypp
     /** \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
@@ -542,6 +604,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.
      *
@@ -567,6 +657,13 @@ 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.
      *
@@ -584,7 +681,7 @@ namespace zypp
     /**
      * 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.
      *
@@ -592,7 +689,7 @@ namespace zypp
      *   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 );
 
     /**
@@ -647,6 +744,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 );
     //@}
 
     ///////////////////////////////////////////////////////////////////