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 stat Mode. */
248 Mode mode() const { return mode_e; }
249 /** Return error returned from last stat/lstat call. */
250 int error() const { return error_i; }
252 /** Set a new Pathname. */
253 void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
254 /** Set a new Mode . */
255 void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
258 bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); }
259 /** LSTAT \a path. */
260 bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
261 /** Restat \a path using current mode. */
262 bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
264 /** STAT current path. */
265 bool stat() { setMode( STAT ); return operator()(); }
266 /** LSTAT current path. */
267 bool lstat() { setMode( LSTAT ); return operator()(); }
268 /** Restat current path using current mode. */
273 /** Return whether valid stat info exists.
274 * That's usg. whether the file exist and you had permission to
277 bool isExist() const { return !error_i; }
279 /** \name Query StatMode attibutes.
280 * Combines \ref zypp::PathInfo::isExist and \ref zypp::filesystem::StatMode query.
283 FileType fileType() const;
285 bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
286 bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
287 bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
288 bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
289 bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
290 bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
291 bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
294 bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
295 bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
296 bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
298 bool isR() const { return isRUsr(); }
299 bool isW() const { return isWUsr(); }
300 bool isX() const { return isXUsr(); }
302 bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
303 bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
304 bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
306 bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
307 bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
308 bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
310 bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
311 bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
312 bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
314 bool isPerm ( mode_t m ) const { return isExist() && (m == perm()); }
315 bool hasPerm( mode_t m ) const { return isExist() && (m == (m & perm())); }
317 mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
318 mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
319 mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
320 mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
322 mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
325 /** Return st_mode() as filesystem::StatMode. */
326 StatMode asStatMode() const { return st_mode(); }
328 nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; }
330 /** \name Owner and group */
332 uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; }
333 gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; }
336 /** \name Permission according to current uid/gid. */
338 /** Returns current users permission (<tt>[0-7]</tt>)*/
339 mode_t userMay() const;
341 bool userMayR() const { return( userMay() & 04 ); }
342 bool userMayW() const { return( userMay() & 02 ); }
343 bool userMayX() const { return( userMay() & 01 ); }
345 bool userMayRW() const { return( (userMay() & 06) == 06 ); }
346 bool userMayRX() const { return( (userMay() & 05) == 05 ); }
347 bool userMayWX() const { return( (userMay() & 03) == 03 ); }
349 bool userMayRWX() const { return( userMay() == 07 ); }
352 /** \name Device and inode info. */
354 ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; }
355 dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; }
356 dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; }
358 unsigned int major() const;
359 unsigned int minor() const;
362 /** \name Size info. */
364 off_t size() const { return isExist() ? statbuf_C.st_size : 0; }
365 unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
366 unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; }
369 /** \name Time stamps. */
371 time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */
372 time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */
373 time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
378 struct stat statbuf_C;
382 ///////////////////////////////////////////////////////////////////
384 /** \relates PathInfo Stream output. */
385 extern std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
387 ///////////////////////////////////////////////////////////////////
389 ///////////////////////////////////////////////////////////////////
390 /** \name Directory related functions. */
393 * Like '::mkdir'. Attempt to create a new directory named path. mode
394 * specifies the permissions to use. It is modified by the process's
395 * umask in the usual way.
397 * @return 0 on success, errno on failure
399 int mkdir( const Pathname & path, unsigned mode = 0755 );
402 * Like 'mkdir -p'. No error if directory exists. Make parent directories
403 * as needed. mode specifies the permissions to use, if directories have to
404 * be created. It is modified by the process's umask in the usual way.
406 * @return 0 on success, errno on failure
408 int assert_dir( const Pathname & path, unsigned mode = 0755 );
411 * Like '::rmdir'. Delete a directory, which must be empty.
413 * @return 0 on success, errno on failure
415 int rmdir( const Pathname & path );
418 * Like 'rm -r DIR'. Delete a directory, recursively removing its contents.
420 * @return 0 on success, ENOTDIR if path is not a directory, otherwise the
421 * commands return value.
423 int recursive_rmdir( const Pathname & path );
426 * Like 'rm -r DIR/ *'. Delete directory contents, but keep the directory itself.
428 * @return 0 on success, ENOTDIR if path is not a directory, otherwise the
429 * commands return value.
431 int clean_dir( const Pathname & path );
434 * Like 'cp -a srcpath destpath'. Copy directory tree. srcpath/destpath must be
435 * directories. 'basename srcpath' must not exist in destpath.
437 * @return 0 on success, ENOTDIR if srcpath/destpath is not a directory, EEXIST if
438 * 'basename srcpath' exists in destpath, otherwise the commands return value.
440 int copy_dir( const Pathname & srcpath, const Pathname & destpath );
443 * Like 'cp -a srcpath/. destpath'. Copy the content of srcpath recursively
444 * into destpath. Both \p srcpath and \p destpath has to exists.
446 * @return 0 on success, ENOTDIR if srcpath/destpath is not a directory,
447 * EEXIST if srcpath and destpath are equal, otherwise the commands
450 int copy_dir_content( const Pathname & srcpath, const Pathname & destpath);
453 * Return content of directory via retlist. If dots is false
454 * entries starting with '.' are not reported. "." and ".."
455 * are never reported.
457 * Returns just the directory entries as string.
459 * @return 0 on success, errno on failure.
461 * \todo provide some readdirIterator.
464 int readdir( std::list<std::string> & retlist,
465 const Pathname & path, bool dots = true );
468 * Return content of directory via retlist. If dots is false
469 * entries starting with '.' are not reported. "." and ".."
470 * are never reported.
472 * Returns the directory entries prefixed with \a path.
474 * @return 0 on success, errno on failure.
476 * \todo provide some readdirIterator.
479 int readdir( std::list<Pathname> & retlist,
480 const Pathname & path, bool dots = true );
482 /** Listentry returned by readdir. */
486 DirEntry( const std::string & name_r = std::string(), FileType type_r = FT_NOT_AVAIL )
492 /** Returned by readdir. */
493 typedef std::list<DirEntry> DirContent;
496 * Return content of directory via retlist. If dots is false
497 * entries starting with '.' are not reported. "." and ".."
498 * are never reported.
500 * The type of individual directory entries is determined accoding to
501 * statmode (i.e. via stat or lstat).
503 * @return 0 on success, errno on failure.
505 int readdir( DirContent & retlist, const Pathname & path,
506 bool dots = true, PathInfo::Mode statmode = PathInfo::STAT );
510 * Check if the specified directory is empty.
511 * \param path The path of the directory to check.
512 * \return 0 if directory is empty, -1 if not, errno > 0 on failure.
514 int is_empty_dir(const Pathname & path);
518 ///////////////////////////////////////////////////////////////////
519 /** \name File related functions. */
522 * Like '::unlink'. Delete a file (symbolic link, socket, fifo or device).
524 * @return 0 on success, errno on failure
526 int unlink( const Pathname & path );
529 * Like '::rename'. Renames a file, moving it between directories if required.
531 * @return 0 on success, errno on failure
533 int rename( const Pathname & oldpath, const Pathname & newpath );
536 * Like 'cp file dest'. Copy file to destination file.
538 * @return 0 on success, EINVAL if file is not a file, EISDIR if
539 * destiantion is a directory, otherwise the commands return value.
541 int copy( const Pathname & file, const Pathname & dest );
544 * Like '::symlink'. Creates a symbolic link named newpath which contains
545 * the string oldpath. If newpath exists it will not be overwritten.
547 * @return 0 on success, errno on failure.
549 int symlink( const Pathname & oldpath, const Pathname & newpath );
552 * Like '::link'. Creates a hard link named newpath to an existing file
553 * oldpath. If newpath exists it will not be overwritten.
555 * @return 0 on success, errno on failure.
557 int hardlink( const Pathname & oldpath, const Pathname & newpath );
560 * Like 'cp file dest'. Copy file to dest dir.
562 * @return 0 on success, EINVAL if file is not a file, ENOTDIR if dest
563 * is no directory, otherwise the commands return value.
565 int copy_file2dir( const Pathname & file, const Pathname & dest );
568 ///////////////////////////////////////////////////////////////////
569 /** \name Digest computaion.
570 * \todo check cooperation with zypp::Digest
574 * Compute a files md5sum.
576 * @return the files md5sum on success, otherwise an empty string..
578 std::string md5sum( const Pathname & file );
581 * Compute a files sha1sum.
583 * @return the files sha1sum on success, otherwise an empty string..
585 std::string sha1sum( const Pathname & file );
589 * Compute a files checksum
591 * @return the files checksum on success, otherwise an empty string..
593 std::string checksum( const Pathname & file, const std::string &algorithm );
596 * check files checksum
598 * @return true if the checksum matchs
600 bool is_checksum( const Pathname & file, const CheckSum &checksum );
602 ///////////////////////////////////////////////////////////////////
603 /** \name Changing permissions. */
606 * Like '::chmod'. The mode of the file given by path is changed.
608 * @return 0 on success, errno on failure
610 int chmod( const Pathname & path, mode_t mode );
613 ///////////////////////////////////////////////////////////////////
617 * Test whether a file is compressed (gzip/bzip2).
619 * @return ZT_GZ, ZT_BZ2 if file is compressed, otherwise ZT_NONE.
621 enum ZIP_TYPE { ZT_NONE, ZT_GZ, ZT_BZ2 };
623 ZIP_TYPE zipType( const Pathname & file );
626 * Erase whatever happens to be located at path (file or directory).
628 * @return 0 on success.
630 * \todo check cooperation with zypp::TmpFile and zypp::TmpDir
632 int erase( const Pathname & path );
635 * Report free disk space on a mounted file system.
637 * path is the path name of any file within the mounted filesystem.
639 * @return Free disk space or -1 on error.
641 ByteCount df( const Pathname & path );
644 /////////////////////////////////////////////////////////////////
645 } // namespace filesystem
646 ///////////////////////////////////////////////////////////////////
648 /** Dragged into namespace zypp. */
649 using filesystem::PathInfo;
651 /////////////////////////////////////////////////////////////////
653 ///////////////////////////////////////////////////////////////////
654 #endif // ZYPP_PATHINFO_H