1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PathInfo.h
12 #ifndef ZYPP_PATHINFO_H
13 #define ZYPP_PATHINFO_H
17 #include <sys/types.h>
30 #include "zypp/Pathname.h"
31 #include "zypp/CheckSum.h"
32 #include "zypp/ByteCount.h"
34 ///////////////////////////////////////////////////////////////////
36 { /////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////
39 /** Types and functions for filesystem operations.
40 * \todo move zypp::filesystem stuff into separate header
41 * \todo Add tmpfile and tmpdir handling.
42 * \todo think about using Exceptions in zypp::filesystem
43 * \todo provide a readdir iterator; at least provide an interface
44 * using an insert_iterator to be independent from std::container.
47 { /////////////////////////////////////////////////////////////////
49 ///////////////////////////////////////////////////////////////////
50 /** File type information.
51 * \todo Think about an \ref g_EnumerationClass
55 FT_NOT_AVAIL = 0x00, // no typeinfo available
56 FT_NOT_EXIST = 0x01, // file does not exist
65 ///////////////////////////////////////////////////////////////////
67 /** \relates FileType Stram output. */
68 extern std::ostream & operator<<( std::ostream & str, FileType obj );
70 ///////////////////////////////////////////////////////////////////
72 ///////////////////////////////////////////////////////////////////
74 // CLASS NAME : StatMode
76 * @short Wrapper class for mode_t values as derived from ::stat
80 friend std::ostream & operator<<( std::ostream & str, const StatMode & obj );
83 /** Ctor taking mode_t value from ::stat. */
84 StatMode( const mode_t & mode_r = 0 )
90 /** \name Query FileType. */
92 FileType fileType() const;
94 bool isFile() const { return S_ISREG( _mode ); }
95 bool isDir () const { return S_ISDIR( _mode ); }
96 bool isLink() const { return S_ISLNK( _mode ); }
97 bool isChr() const { return S_ISCHR( _mode ); }
98 bool isBlk() const { return S_ISBLK( _mode ); }
99 bool isFifo() const { return S_ISFIFO( _mode ); }
100 bool isSock() const { return S_ISSOCK( _mode ); }
103 /** \name Query user permissions. */
105 bool isRUsr() const { return (_mode & S_IRUSR); }
106 bool isWUsr() const { return (_mode & S_IWUSR); }
107 bool isXUsr() const { return (_mode & S_IXUSR); }
109 /** Short for isRUsr().*/
110 bool isR() const { return isRUsr(); }
111 /** Short for isWUsr().*/
112 bool isW() const { return isWUsr(); }
113 /** Short for isXUsr().*/
114 bool isX() const { return isXUsr(); }
117 /** \name Query group permissions. */
119 bool isRGrp() const { return (_mode & S_IRGRP); }
120 bool isWGrp() const { return (_mode & S_IWGRP); }
121 bool isXGrp() const { return (_mode & S_IXGRP); }
124 /** \name Query others permissions. */
126 bool isROth() const { return (_mode & S_IROTH); }
127 bool isWOth() const { return (_mode & S_IWOTH); }
128 bool isXOth() const { return (_mode & S_IXOTH); }
131 /** \name Query special permissions. */
134 bool isUid() const { return (_mode & S_ISUID); }
136 bool isGid() const { return (_mode & S_ISGID); }
138 bool isVtx() const { return (_mode & S_ISVTX); }
141 /** \name Query permission */
143 /** Test for equal permission bits. */
144 bool isPerm ( mode_t m ) const { return (m == perm()); }
145 /** Test for set permission bits. */
146 bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
149 /** \name Extract permission bits only. */
151 mode_t uperm() const { return (_mode & S_IRWXU); }
152 mode_t gperm() const { return (_mode & S_IRWXG); }
153 mode_t operm() const { return (_mode & S_IRWXO); }
154 mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); }
157 /** Return the mode_t value. */
158 mode_t st_mode() const { return _mode; }
163 ///////////////////////////////////////////////////////////////////
165 /** \relates StatMode Stream output. */
166 extern std::ostream & operator<<( std::ostream & str, const StatMode & obj );
168 ///////////////////////////////////////////////////////////////////
170 ///////////////////////////////////////////////////////////////////
172 // CLASS NAME : DevInoCache
173 /** Simple cache remembering device/inode to detect hardlinks.
176 * for ( all files ) {
177 * if ( trace.insert( file.device, file.inode ) ) {
178 * // 1st occurance of file
180 * // else: hardlink; already counted this device/inode
192 void clear() { _devino.clear(); }
194 /** Remember dev/ino.
195 * \Return <code>true</code> if it's inserted the first
196 * time, <code>false</code> if alredy present in cache
197 * (a hardlink to a previously remembered file).
199 bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
200 return _devino[dev_r].insert( ino_r ).second;
204 std::map<dev_t,std::set<ino_t> > _devino;
206 ///////////////////////////////////////////////////////////////////
208 ///////////////////////////////////////////////////////////////////
210 // CLASS NAME : PathInfo
211 /** Wrapper class for ::stat/::lstat.
213 * \note All attribute quieries test for isExist(), and return \c false or
214 * \c 0, if stat was not successful.
216 * \note For convenience PathInfo is available as zypp::PathInfo too.
220 friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
223 /** stat() or lstat() */
224 enum Mode { STAT, LSTAT };
227 /** \name Construct from Pathname.
228 * Default mode is \c STAT.
233 PathInfo( const Pathname & path, Mode initial = STAT );
235 PathInfo( const std::string & path, Mode initial = STAT );
237 PathInfo( const char * path, Mode initial = STAT );
243 /** Return current Pathname. */
244 const Pathname & path() const { return path_t; }
245 /** Return current Pathname as String. */
246 const std::string & asString() const { return path_t.asString(); }
247 /** Return current Pathname as C-string. */
248 const char * c_str() const { return path_t.asString().c_str(); }
249 /** Return current stat Mode. */
250 Mode mode() const { return mode_e; }
251 /** Return error returned from last stat/lstat call. */
252 int error() const { return error_i; }
254 /** Set a new Pathname. */
255 void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
256 /** Set a new Mode . */
257 void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
260 bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); }
261 /** LSTAT \a path. */
262 bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
263 /** Restat \a path using current mode. */
264 bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
266 /** STAT current path. */
267 bool stat() { setMode( STAT ); return operator()(); }
268 /** LSTAT current path. */
269 bool lstat() { setMode( LSTAT ); return operator()(); }
270 /** Restat current path using current mode. */
275 /** Return whether valid stat info exists.
276 * That's usg. whether the file exist and you had permission to
279 bool isExist() const { return !error_i; }
281 /** \name Query StatMode attibutes.
282 * Combines \ref zypp::PathInfo::isExist and \ref zypp::filesystem::StatMode query.
285 FileType fileType() const;
287 bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
288 bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
289 bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
290 bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
291 bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
292 bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
293 bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
296 bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
297 bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
298 bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
300 bool isR() const { return isRUsr(); }
301 bool isW() const { return isWUsr(); }
302 bool isX() const { return isXUsr(); }
304 bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
305 bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
306 bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
308 bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
309 bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
310 bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
312 bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
313 bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
314 bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
316 bool isPerm ( mode_t m ) const { return isExist() && (m == perm()); }
317 bool hasPerm( mode_t m ) const { return isExist() && (m == (m & perm())); }
319 mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
320 mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
321 mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
322 mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
324 mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
327 /** Return st_mode() as filesystem::StatMode. */
328 StatMode asStatMode() const { return st_mode(); }
330 nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; }
332 /** \name Owner and group */
334 uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; }
335 gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; }
338 /** \name Permission according to current uid/gid. */
340 /** Returns current users permission (<tt>[0-7]</tt>)*/
341 mode_t userMay() const;
343 bool userMayR() const { return( userMay() & 04 ); }
344 bool userMayW() const { return( userMay() & 02 ); }
345 bool userMayX() const { return( userMay() & 01 ); }
347 bool userMayRW() const { return( (userMay() & 06) == 06 ); }
348 bool userMayRX() const { return( (userMay() & 05) == 05 ); }
349 bool userMayWX() const { return( (userMay() & 03) == 03 ); }
351 bool userMayRWX() const { return( userMay() == 07 ); }
354 /** \name Device and inode info. */
356 ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; }
357 dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; }
358 dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; }
360 unsigned int major() const;
361 unsigned int minor() const;
364 /** \name Size info. */
366 off_t size() const { return isExist() ? statbuf_C.st_size : 0; }
367 unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
368 unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; }
371 /** \name Time stamps. */
373 time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */
374 time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */
375 time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
380 struct stat statbuf_C;
384 ///////////////////////////////////////////////////////////////////
386 /** \relates PathInfo Stream output. */
387 extern std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
389 ///////////////////////////////////////////////////////////////////
391 ///////////////////////////////////////////////////////////////////
392 /** \name Directory related functions. */
395 * Like '::mkdir'. Attempt to create a new directory named path. mode
396 * specifies the permissions to use. It is modified by the process's
397 * umask in the usual way.
399 * @return 0 on success, errno on failure
401 int mkdir( const Pathname & path, unsigned mode = 0755 );
404 * Like 'mkdir -p'. No error if directory exists. Make parent directories
405 * as needed. mode specifies the permissions to use, if directories have to
406 * be created. It is modified by the process's umask in the usual way.
408 * @return 0 on success, errno on failure
410 int assert_dir( const Pathname & path, unsigned mode = 0755 );
413 * Like '::rmdir'. Delete a directory, which must be empty.
415 * @return 0 on success, errno on failure
417 int rmdir( const Pathname & path );
420 * Like 'rm -r DIR'. Delete a directory, recursively removing its contents.
422 * @return 0 on success, ENOTDIR if path is not a directory, otherwise the
423 * commands return value.
425 int recursive_rmdir( const Pathname & path );
428 * Like 'rm -r DIR/ *'. Delete directory contents, but keep the directory itself.
430 * @return 0 on success, ENOTDIR if path is not a directory, otherwise the
431 * commands return value.
433 int clean_dir( const Pathname & path );
436 * Like 'cp -a srcpath destpath'. Copy directory tree. srcpath/destpath must be
437 * directories. 'basename srcpath' must not exist in destpath.
439 * @return 0 on success, ENOTDIR if srcpath/destpath is not a directory, EEXIST if
440 * 'basename srcpath' exists in destpath, otherwise the commands return value.
442 int copy_dir( const Pathname & srcpath, const Pathname & destpath );
445 * Like 'cp -a srcpath/. destpath'. Copy the content of srcpath recursively
446 * into destpath. Both \p srcpath and \p destpath has to exists.
448 * @return 0 on success, ENOTDIR if srcpath/destpath is not a directory,
449 * EEXIST if srcpath and destpath are equal, otherwise the commands
452 int copy_dir_content( const Pathname & srcpath, const Pathname & destpath);
455 * Return content of directory via retlist. If dots is false
456 * entries starting with '.' are not reported. "." and ".."
457 * are never reported.
459 * Returns just the directory entries as string.
461 * @return 0 on success, errno on failure.
463 * \todo provide some readdirIterator.
466 int readdir( std::list<std::string> & retlist,
467 const Pathname & path, bool dots = true );
470 * Return content of directory via retlist. If dots is false
471 * entries starting with '.' are not reported. "." and ".."
472 * are never reported.
474 * Returns the directory entries prefixed with \a path.
476 * @return 0 on success, errno on failure.
478 * \todo provide some readdirIterator.
481 int readdir( std::list<Pathname> & retlist,
482 const Pathname & path, bool dots = true );
484 /** Listentry returned by readdir. */
488 DirEntry( const std::string & name_r = std::string(), FileType type_r = FT_NOT_AVAIL )
493 bool operator==( const DirEntry &rhs ) const;
496 /** Returned by readdir. */
497 typedef std::list<DirEntry> DirContent;
500 * Return content of directory via retlist. If dots is false
501 * entries starting with '.' are not reported. "." and ".."
502 * are never reported.
504 * The type of individual directory entries is determined accoding to
505 * statmode (i.e. via stat or lstat).
507 * @return 0 on success, errno on failure.
509 int readdir( DirContent & retlist, const Pathname & path,
510 bool dots = true, PathInfo::Mode statmode = PathInfo::STAT );
514 * Check if the specified directory is empty.
515 * \param path The path of the directory to check.
516 * \return 0 if directory is empty, -1 if not, errno > 0 on failure.
518 int is_empty_dir(const Pathname & path);
522 ///////////////////////////////////////////////////////////////////
523 /** \name File related functions. */
526 * Create an empty file if it does not yet exist. Make parent directories
527 * as needed. mode specifies the permissions to use. It is modified by the
528 * process's umask in the usual way.
530 * @return 0 on success, errno on failure
532 int assert_file( const Pathname & path, unsigned mode = 0644 );
535 * Change file's modification and access times.
537 * \return 0 on success, errno on failure
540 int touch (const Pathname & path);
543 * Like '::unlink'. Delete a file (symbolic link, socket, fifo or device).
545 * @return 0 on success, errno on failure
547 int unlink( const Pathname & path );
550 * Like '::rename'. Renames a file, moving it between directories if required.
552 * @return 0 on success, errno on failure
554 int rename( const Pathname & oldpath, const Pathname & newpath );
556 /** Exchanges two files or directories.
558 * Most common use is when building a new config file (or dir)
559 * in a tempfile. After the job is done, configfile and tempfile
560 * are exchanged. This includes moving away the configfile in case
561 * the tempfile does not exist. Parent directories are created as
564 * \note Paths are exchanged using \c ::rename, so take care both paths
565 * are located on the same filesystem.
568 * Pathname configfile( "/etc/myconfig" );
569 * TmpFile newconfig( TmpFile::makeSibling( configfile ) );
570 * // now write the new config:
571 * std::ofstream o( newconfig.path().c_str() );
572 * o << "mew values << endl;
574 * // If everything is fine, exchange the files:
575 * exchange( newconfig.path(), configfile );
576 * // Now the old configfile is still available at newconfig.path()
577 * // until newconfig goes out of scope.
580 * @return 0 on success, errno on failure
582 int exchange( const Pathname & lpath, const Pathname & rpath );
585 * Like 'cp file dest'. Copy file to destination file.
587 * @return 0 on success, EINVAL if file is not a file, EISDIR if
588 * destiantion is a directory, otherwise the commands return value.
590 int copy( const Pathname & file, const Pathname & dest );
593 * Like '::symlink'. Creates a symbolic link named newpath which contains
594 * the string oldpath. If newpath exists it will not be overwritten.
596 * @return 0 on success, errno on failure.
598 int symlink( const Pathname & oldpath, const Pathname & newpath );
601 * Like '::link'. Creates a hard link named newpath to an existing file
602 * oldpath. If newpath exists it will not be overwritten.
604 * @return 0 on success, errno on failure.
606 int hardlink( const Pathname & oldpath, const Pathname & newpath );
609 * Create \a newpath as hardlink or copy of \a oldpath.
611 * @return 0 on success, errno on failure.
613 int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath );
616 * Like '::readlink'. Return the contents of the symbolic link
617 * \a symlink_r via \a target_r.
619 * @return 0 on success, errno on failure.
621 int readlink( const Pathname & symlink_r, Pathname & target_r );
622 /** \overload Return an empty Pathname on error. */
623 inline Pathname readlink( const Pathname & symlink_r )
626 readlink( symlink_r, target );
631 * Recursively follows the symlink pointed to by \a path_r and returns
632 * the Pathname to the real file or directory pointed to by the link.
634 * There is a recursion limit of 256 iterations to protect against a cyclic
637 * @return Pathname of the file or directory pointed to by the given link
638 * if it is a valid link. If \a path_r is not a link, an exact copy of
639 * it is returned. If \a path_r is a broken or a cyclic link, an empty
640 * Pathname is returned and the event logged.
642 Pathname expandlink( const Pathname & path_r );
645 * Like 'cp file dest'. Copy file to dest dir.
647 * @return 0 on success, EINVAL if file is not a file, ENOTDIR if dest
648 * is no directory, otherwise the commands return value.
650 int copy_file2dir( const Pathname & file, const Pathname & dest );
653 ///////////////////////////////////////////////////////////////////
654 /** \name Digest computaion.
655 * \todo check cooperation with zypp::Digest
659 * Compute a files md5sum.
661 * @return the files md5sum on success, otherwise an empty string..
663 std::string md5sum( const Pathname & file );
666 * Compute a files sha1sum.
668 * @return the files sha1sum on success, otherwise an empty string..
670 std::string sha1sum( const Pathname & file );
674 * Compute a files checksum
676 * @return the files checksum on success, otherwise an empty string..
678 std::string checksum( const Pathname & file, const std::string &algorithm );
681 * check files checksum
683 * @return true if the checksum matchs
685 bool is_checksum( const Pathname & file, const CheckSum &checksum );
687 ///////////////////////////////////////////////////////////////////
688 /** \name Changing permissions. */
691 * Like '::chmod'. The mode of the file given by path is changed.
693 * @return 0 on success, errno on failure
695 int chmod( const Pathname & path, mode_t mode );
698 ///////////////////////////////////////////////////////////////////
702 * Test whether a file is compressed (gzip/bzip2).
704 * @return ZT_GZ, ZT_BZ2 if file is compressed, otherwise ZT_NONE.
706 enum ZIP_TYPE { ZT_NONE, ZT_GZ, ZT_BZ2 };
708 ZIP_TYPE zipType( const Pathname & file );
711 * Erase whatever happens to be located at path (file or directory).
713 * @return 0 on success.
715 * \todo check cooperation with zypp::TmpFile and zypp::TmpDir
717 int erase( const Pathname & path );
720 * Report free disk space on a mounted file system.
722 * path is the path name of any file within the mounted filesystem.
724 * @return Free disk space or -1 on error.
726 ByteCount df( const Pathname & path );
729 * Get the current umask (file mode creation mask)
731 * @return The current umask
736 * Modify \c mode_r according to the current umask
737 * <tt>( mode_r & ~getUmask() )</tt>.
738 * \see \ref getUmask.
739 * @return The resulting permissions.
741 inline mode_t applyUmaskTo( mode_t mode_r )
742 { return mode_r & ~getUmask(); }
745 /////////////////////////////////////////////////////////////////
746 } // namespace filesystem
747 ///////////////////////////////////////////////////////////////////
749 /** Dragged into namespace zypp. */
750 using filesystem::PathInfo;
752 /////////////////////////////////////////////////////////////////
754 ///////////////////////////////////////////////////////////////////
755 #endif // ZYPP_PATHINFO_H