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"
32 ///////////////////////////////////////////////////////////////////
34 { /////////////////////////////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////
37 /** Types and functions for filesystem operations.
38 * \todo move zypp::filesystem stuff into separate header
39 * \todo Add tmpfile and tmpdir handling.
40 * \todo think about using Exceptions in zypp::filesystem
41 * \todo provide a readdir iterator; at least provide an interface
42 * using an insert_iterator to be independent from std::container.
45 { /////////////////////////////////////////////////////////////////
47 ///////////////////////////////////////////////////////////////////
48 /** File type information.
49 * \todo Think about an \ref g_EnumerationClass
53 FT_NOT_AVAIL = 0x00, // no typeinfo available
54 FT_NOT_EXIST = 0x01, // file does not exist
63 ///////////////////////////////////////////////////////////////////
65 /** \relates FileType Stram output. */
66 extern std::ostream & operator<<( std::ostream & str, FileType obj );
68 ///////////////////////////////////////////////////////////////////
70 ///////////////////////////////////////////////////////////////////
72 // CLASS NAME : StatMode
74 * @short Wrapper class for mode_t values as derived from ::stat
78 friend std::ostream & operator<<( std::ostream & str, const StatMode & obj );
81 /** Ctor taking mode_t value from ::stat. */
82 StatMode( const mode_t & mode_r = 0 )
88 /** \name Query FileType. */
90 FileType fileType() const;
92 bool isFile() const { return S_ISREG( _mode ); }
93 bool isDir () const { return S_ISDIR( _mode ); }
94 bool isLink() const { return S_ISLNK( _mode ); }
95 bool isChr() const { return S_ISCHR( _mode ); }
96 bool isBlk() const { return S_ISBLK( _mode ); }
97 bool isFifo() const { return S_ISFIFO( _mode ); }
98 bool isSock() const { return S_ISSOCK( _mode ); }
101 /** \name Query user permissions. */
103 bool isRUsr() const { return (_mode & S_IRUSR); }
104 bool isWUsr() const { return (_mode & S_IWUSR); }
105 bool isXUsr() const { return (_mode & S_IXUSR); }
107 /** Short for isRUsr().*/
108 bool isR() const { return isRUsr(); }
109 /** Short for isWUsr().*/
110 bool isW() const { return isWUsr(); }
111 /** Short for isXUsr().*/
112 bool isX() const { return isXUsr(); }
115 /** \name Query group permissions. */
117 bool isRGrp() const { return (_mode & S_IRGRP); }
118 bool isWGrp() const { return (_mode & S_IWGRP); }
119 bool isXGrp() const { return (_mode & S_IXGRP); }
122 /** \name Query others permissions. */
124 bool isROth() const { return (_mode & S_IROTH); }
125 bool isWOth() const { return (_mode & S_IWOTH); }
126 bool isXOth() const { return (_mode & S_IXOTH); }
129 /** \name Query special permissions. */
132 bool isUid() const { return (_mode & S_ISUID); }
134 bool isGid() const { return (_mode & S_ISGID); }
136 bool isVtx() const { return (_mode & S_ISVTX); }
139 /** \name Query permission */
141 /** Test for equal permission bits. */
142 bool isPerm ( mode_t m ) const { return (m == perm()); }
143 /** Test for set permission bits. */
144 bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
147 /** \name Extract permission bits only. */
149 mode_t uperm() const { return (_mode & S_IRWXU); }
150 mode_t gperm() const { return (_mode & S_IRWXG); }
151 mode_t operm() const { return (_mode & S_IRWXO); }
152 mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); }
155 /** Return the mode_t value. */
156 mode_t st_mode() const { return _mode; }
161 ///////////////////////////////////////////////////////////////////
163 /** \relates StatMode Stream output. */
164 extern std::ostream & operator<<( std::ostream & str, const StatMode & obj );
166 ///////////////////////////////////////////////////////////////////
168 ///////////////////////////////////////////////////////////////////
170 // CLASS NAME : DevInoCache
171 /** Simple cache remembering device/inode to detect hardlinks.
174 * for ( all files ) {
175 * if ( trace.insert( file.device, file.inode ) ) {
176 * // 1st occurance of file
178 * // else: hardlink; already counted this device/inode
190 void clear() { _devino.clear(); }
192 /** Remember dev/ino.
193 * \Return <code>true</code> if it's inserted the first
194 * time, <code>false</code> if alredy present in cache
195 * (a hardlink to a previously remembered file).
197 bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
198 return _devino[dev_r].insert( ino_r ).second;
202 std::map<dev_t,std::set<ino_t> > _devino;
204 ///////////////////////////////////////////////////////////////////
206 ///////////////////////////////////////////////////////////////////
208 // CLASS NAME : PathInfo
209 /** Wrapper class for ::stat/::lstat.
211 * \note All attribute quieries test for isExist(), and return \c false or
212 * \c 0, if stat was not successful.
214 * \note For convenience PathInfo is available as zypp::PathInfo too.
218 friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
221 /** stat() or lstat() */
222 enum Mode { STAT, LSTAT };
225 /** \name Construct from Pathname.
226 * Default mode is \c STAT.
231 PathInfo( const Pathname & path, Mode initial = STAT );
233 PathInfo( const std::string & path, Mode initial = STAT );
235 PathInfo( const char * path, Mode initial = STAT );
241 /** Return current Pathname. */
242 const Pathname & path() const { return path_t; }
243 /** Return current Pathname as String. */
244 const std::string & asString() const { return path_t.asString(); }
245 /** Return current stat Mode. */
246 Mode mode() const { return mode_e; }
247 /** Return error returned from last stat/lstat call. */
248 int error() const { return error_i; }
250 /** Set a new Pathname. */
251 void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
252 /** Set a new Mode . */
253 void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
256 bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); }
257 /** LSTAT \a path. */
258 bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
259 /** Restat \a path using current mode. */
260 bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
262 /** STAT current path. */
263 bool stat() { setMode( STAT ); return operator()(); }
264 /** LSTAT current path. */
265 bool lstat() { setMode( LSTAT ); return operator()(); }
266 /** Restat current path using current mode. */
271 /** Return whether valid stat info exists.
272 * That's usg. whether the file exist and you had permission to
275 bool isExist() const { return !error_i; }
277 /** \name Query StatMode attibutes.
278 * Combines \ref zypp::PathInfo::isExist and \ref zypp::filesystem::StatMode query.
281 FileType fileType() const;
283 bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
284 bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
285 bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
286 bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
287 bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
288 bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
289 bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
292 bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
293 bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
294 bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
296 bool isR() const { return isRUsr(); }
297 bool isW() const { return isWUsr(); }
298 bool isX() const { return isXUsr(); }
300 bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
301 bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
302 bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
304 bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
305 bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
306 bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
308 bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
309 bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
310 bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
312 bool isPerm ( mode_t m ) const { return isExist() && (m == perm()); }
313 bool hasPerm( mode_t m ) const { return isExist() && (m == (m & perm())); }
315 mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
316 mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
317 mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
318 mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
320 mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
323 /** Return st_mode() as filesystem::StatMode. */
324 StatMode asStatMode() const { return st_mode(); }
326 nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; }
328 /** \name Owner and group */
330 uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; }
331 gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; }
334 /** \name Permission according to current uid/gid. */
336 /** Returns current users permission (<tt>[0-7]</tt>)*/
337 mode_t userMay() const;
339 bool userMayR() const { return( userMay() & 04 ); }
340 bool userMayW() const { return( userMay() & 02 ); }
341 bool userMayX() const { return( userMay() & 01 ); }
343 bool userMayRW() const { return( (userMay() & 06) == 06 ); }
344 bool userMayRX() const { return( (userMay() & 05) == 05 ); }
345 bool userMayWX() const { return( (userMay() & 03) == 03 ); }
347 bool userMayRWX() const { return( userMay() == 07 ); }
350 /** \name Device and inode info. */
352 ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; }
353 dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; }
354 dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; }
356 unsigned int major() const;
357 unsigned int minor() const;
360 /** \name Size info. */
362 off_t size() const { return isExist() ? statbuf_C.st_size : 0; }
363 unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
364 unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; }
367 /** \name Time stamps. */
369 time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */
370 time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */
371 time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
376 struct stat statbuf_C;
380 ///////////////////////////////////////////////////////////////////
382 /** \relates PathInfo Stream output. */
383 extern std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
385 ///////////////////////////////////////////////////////////////////
387 ///////////////////////////////////////////////////////////////////
388 /** \name Directory related functions. */
391 * Like '::mkdir'. Attempt to create a new directory named path. mode
392 * specifies the permissions to use. It is modified by the process's
393 * umask in the usual way.
395 * @return 0 on success, errno on failure
397 int mkdir( const Pathname & path, unsigned mode = 0755 );
400 * Like 'mkdir -p'. No error if directory exists. Make parent directories
401 * as needed. mode specifies the permissions to use, if directories have to
402 * be created. It is modified by the process's umask in the usual way.
404 * @return 0 on success, errno on failure
406 int assert_dir( const Pathname & path, unsigned mode = 0755 );
409 * Like '::rmdir'. Delete a directory, which must be empty.
411 * @return 0 on success, errno on failure
413 int rmdir( const Pathname & path );
416 * Like 'rm -r DIR'. Delete a directory, recursively removing its contents.
418 * @return 0 on success, ENOTDIR if path is not a directory, otherwise the
419 * commands return value.
421 int recursive_rmdir( const Pathname & path );
424 * Like 'rm -r DIR/ *'. Delete directory contents, but keep the directory itself.
426 * @return 0 on success, ENOTDIR if path is not a directory, otherwise the
427 * commands return value.
429 int clean_dir( const Pathname & path );
432 * Like 'cp -a srcpath destpath'. Copy directory tree. srcpath/destpath must be
433 * directories. 'basename srcpath' must not exist in destpath.
435 * @return 0 on success, ENOTDIR if srcpath/destpath is not a directory, EEXIST if
436 * 'basename srcpath' exists in destpath, otherwise the commands return value.
438 int copy_dir( const Pathname & srcpath, const Pathname & destpath );
441 * Like 'cp -a srcpath/. destpath'. Copy the content of srcpath recursively
442 * into destpath. Both \p srcpath and \p destpath has to exists.
444 * @return 0 on success, ENOTDIR if srcpath/destpath is not a directory,
445 * EEXIST if srcpath and destpath are equal, otherwise the commands
448 int copy_dir_content( const Pathname & srcpath, const Pathname & destpath);
451 * Return content of directory via retlist. If dots is false
452 * entries starting with '.' are not reported. "." and ".."
453 * are never reported.
455 * Returns just the directory entries as string.
457 * @return 0 on success, errno on failure.
459 * \todo provide some readdirIterator.
462 int readdir( std::list<std::string> & retlist,
463 const Pathname & path, bool dots = true );
466 * Return content of directory via retlist. If dots is false
467 * entries starting with '.' are not reported. "." and ".."
468 * are never reported.
470 * Returns the directory entries prefixed with \a path.
472 * @return 0 on success, errno on failure.
474 * \todo provide some readdirIterator.
477 int readdir( std::list<Pathname> & retlist,
478 const Pathname & path, bool dots = true );
480 /** Listentry returned by readdir. */
484 DirEntry( const std::string & name_r = std::string(), FileType type_r = FT_NOT_AVAIL )
490 /** Returned by readdir. */
491 typedef std::list<DirEntry> DirContent;
494 * Return content of directory via retlist. If dots is false
495 * entries starting with '.' are not reported. "." and ".."
496 * are never reported.
498 * The type of individual directory entries is determined accoding to
499 * statmode (i.e. via stat or lstat).
501 * @return 0 on success, errno on failure.
503 int readdir( DirContent & retlist, const Pathname & path,
504 bool dots = true, PathInfo::Mode statmode = PathInfo::STAT );
508 * Check if the specified directory is empty.
509 * \param path The path of the directory to check.
510 * \return 0 if directory is empty, -1 if not, errno > 0 on failure.
512 int is_empty_dir(const Pathname & path);
516 ///////////////////////////////////////////////////////////////////
517 /** \name File related functions. */
520 * Like '::unlink'. Delete a file (symbolic link, socket, fifo or device).
522 * @return 0 on success, errno on failure
524 int unlink( const Pathname & path );
527 * Like '::rename'. Renames a file, moving it between directories if required.
529 * @return 0 on success, errno on failure
531 int rename( const Pathname & oldpath, const Pathname & newpath );
534 * Like 'cp file dest'. Copy file to destination file.
536 * @return 0 on success, EINVAL if file is not a file, EISDIR if
537 * destiantion is a directory, otherwise the commands return value.
539 int copy( const Pathname & file, const Pathname & dest );
542 * Like '::symlink'. Creates a symbolic link named newpath which contains
543 * the string oldpath. If newpath exists it will not be overwritten.
545 * @return 0 on success, errno on failure.
547 int symlink( const Pathname & oldpath, const Pathname & newpath );
550 * Like '::link'. Creates a hard link named newpath to an existing file
551 * oldpath. If newpath exists it will not be overwritten.
553 * @return 0 on success, errno on failure.
555 int hardlink( const Pathname & oldpath, const Pathname & newpath );
558 * Like 'cp file dest'. Copy file to dest dir.
560 * @return 0 on success, EINVAL if file is not a file, ENOTDIR if dest
561 * is no directory, otherwise the commands return value.
563 int copy_file2dir( const Pathname & file, const Pathname & dest );
566 ///////////////////////////////////////////////////////////////////
567 /** \name Digest computaion.
568 * \todo check cooperation with zypp::Digest
572 * Compute a files md5sum.
574 * @return the files md5sum on success, otherwise an empty string..
576 std::string md5sum( const Pathname & file );
579 * Compute a files sha1sum.
581 * @return the files sha1sum on success, otherwise an empty string..
583 std::string sha1sum( const Pathname & file );
586 ///////////////////////////////////////////////////////////////////
587 /** \name Changing permissions. */
590 * Like '::chmod'. The mode of the file given by path is changed.
592 * @return 0 on success, errno on failure
594 int chmod( const Pathname & path, mode_t mode );
597 ///////////////////////////////////////////////////////////////////
601 * Test whether a file is compressed (gzip/bzip2).
603 * @return ZT_GZ, ZT_BZ2 if file is compressed, otherwise ZT_NONE.
605 enum ZIP_TYPE { ZT_NONE, ZT_GZ, ZT_BZ2 };
607 ZIP_TYPE zipType( const Pathname & file );
610 * Erase whatever happens to be located at path (file or directory).
612 * @return 0 on success.
614 * \todo check cooperation with zypp::TmpFile and zypp::TmpDir
616 int erase( const Pathname & path );
619 /////////////////////////////////////////////////////////////////
620 } // namespace filesystem
621 ///////////////////////////////////////////////////////////////////
623 /** Dragged into namespace zypp. */
624 using filesystem::PathInfo;
626 /////////////////////////////////////////////////////////////////
628 ///////////////////////////////////////////////////////////////////
629 #endif // ZYPP_PATHINFO_H