00001
00002
00003
00004
00005
00006
00007
00008
00013 #include <iostream>
00014 #include <fstream>
00015 #include <sstream>
00016
00017 #include "zypp/TmpPath.h"
00018 #include "zypp/base/Logger.h"
00019 #include "zypp/base/String.h"
00020 #include "zypp/media/MediaHandler.h"
00021 #include "zypp/media/MediaManager.h"
00022 #include "zypp/media/Mount.h"
00023 #include <limits.h>
00024 #include <stdlib.h>
00025 #include <errno.h>
00026
00027
00028 using namespace std;
00029
00030
00031 #define NONREMOTE_DIRECTORY_YAST 1
00032
00033 namespace zypp {
00034 namespace media {
00035
00036 Pathname MediaHandler::_attachPrefix("");
00037
00039
00040
00041
00043
00045
00046
00047
00048
00049
00050
00051
00052 MediaHandler::MediaHandler ( const Url & url_r,
00053 const Pathname & attach_point_r,
00054 const Pathname & urlpath_below_attachpoint_r,
00055 const bool does_download_r )
00056 : _mediaSource()
00057 , _attachPoint( new AttachPoint())
00058 , _AttachPointHint()
00059 , _relativeRoot( urlpath_below_attachpoint_r)
00060 , _does_download( does_download_r )
00061 , _attach_mtime(0)
00062 , _url( url_r )
00063 , _parentId(0)
00064 {
00065 Pathname real_attach_point( getRealPath(attach_point_r.asString()));
00066
00067 if ( !real_attach_point.empty() ) {
00069
00071
00072 PathInfo adir( real_attach_point );
00073
00074
00075
00076
00077
00078
00079
00080 if ( !adir.isDir()
00081 || (_url.getScheme() != "file"
00082 && _url.getScheme() != "dir"
00083 && !real_attach_point.absolute()) )
00084 {
00085 ERR << "Provided attach point is not a absolute directory: "
00086 << adir << endl;
00087 }
00088 else {
00089 attachPointHint( real_attach_point, false);
00090 setAttachPoint( real_attach_point, false);
00091 }
00092 }
00093 }
00094
00096
00097
00098
00099
00100
00101
00102
00103 MediaHandler::~MediaHandler()
00104 {
00105 try
00106 {
00107 removeAttachPoint();
00108 }
00109 catch(...) {}
00110 }
00111
00112 void
00113 MediaHandler::resetParentId()
00114 {
00115 _parentId = 0;
00116 }
00117
00118 std::string
00119 MediaHandler::getRealPath(const std::string &path)
00120 {
00121 std::string real;
00122 if( !path.empty())
00123 {
00124 #if __GNUC__ > 2
00125
00126 char *ptr = ::realpath(path.c_str(), NULL);
00127 if( ptr != NULL)
00128 {
00129 real = ptr;
00130 free( ptr);
00131 }
00132 else
00134 if( EINVAL == errno)
00135 {
00136 char buff[PATH_MAX + 2];
00137 memset(buff, '\0', sizeof(buff));
00138 if( ::realpath(path.c_str(), buff) != NULL)
00139 {
00140 real = buff;
00141 }
00142 }
00143 #else
00144 char buff[PATH_MAX + 2];
00145 memset(buff, '\0', sizeof(buff));
00146 if( ::realpath(path.c_str(), buff) != NULL)
00147 {
00148 real = buff;
00149 }
00150 #endif
00151 }
00152 return real;
00153 }
00154
00155 zypp::Pathname
00156 MediaHandler::getRealPath(const Pathname &path)
00157 {
00158 return zypp::Pathname(getRealPath(path.asString()));
00159 }
00160
00161
00163
00164
00165
00166
00167
00168
00169
00170 void
00171 MediaHandler::removeAttachPoint()
00172 {
00173 if ( _mediaSource ) {
00174 INT << "MediaHandler deleted with media attached." << endl;
00175 return;
00176 }
00177
00178 DBG << "MediaHandler - checking if to remove attach point" << endl;
00179 if ( _attachPoint.unique() &&
00180 _attachPoint->temp &&
00181 !_attachPoint->path.empty() &&
00182 PathInfo(_attachPoint->path).isDir())
00183 {
00184 Pathname path(_attachPoint->path);
00185
00186 setAttachPoint("", true);
00187
00188 int res = recursive_rmdir( path );
00189 if ( res == 0 ) {
00190 MIL << "Deleted default attach point " << path << endl;
00191 } else {
00192 ERR << "Failed to Delete default attach point " << path
00193 << " errno(" << res << ")" << endl;
00194 }
00195 }
00196 else
00197 {
00198 if( !_attachPoint->path.empty() && !_attachPoint->temp)
00199 DBG << "MediaHandler - attachpoint is not temporary" << endl;
00200 }
00201 }
00202
00203
00205
00206
00207
00208
00209
00210
00211
00212 Pathname
00213 MediaHandler::attachPoint() const
00214 {
00215 return _attachPoint->path;
00216 }
00217
00218
00220
00221
00222
00223
00224
00225
00226
00227 void
00228 MediaHandler::setAttachPoint(const Pathname &path, bool temporary)
00229 {
00230 _attachPoint.reset( new AttachPoint(path, temporary));
00231 }
00232
00233 Pathname
00234 MediaHandler::localRoot() const
00235 {
00236 if( _attachPoint->path.empty())
00237 return Pathname();
00238 else
00239 return _attachPoint->path + _relativeRoot;
00240 }
00241
00243
00244
00245
00246
00247
00248
00249
00250 void
00251 MediaHandler::setAttachPoint(const AttachPointRef &ref)
00252 {
00253 if( ref)
00254 AttachPointRef(ref).swap(_attachPoint);
00255 else
00256 _attachPoint.reset( new AttachPoint());
00257 }
00258
00260
00261
00262
00263
00264
00265
00266
00267 void
00268 MediaHandler::attachPointHint(const Pathname &path, bool temporary)
00269 {
00270 _AttachPointHint.path = path;
00271 _AttachPointHint.temp = temporary;
00272 }
00273
00275
00276
00277
00278
00279
00280
00281
00282 AttachPoint
00283 MediaHandler::attachPointHint() const
00284 {
00285 return _AttachPointHint;
00286 }
00287
00289
00290
00291
00292
00293
00294
00295
00296 AttachedMedia
00297 MediaHandler::findAttachedMedia(const MediaSourceRef &media) const
00298 {
00299 return MediaManager().findAttachedMedia(media);
00300 }
00301
00303
00304
00305
00306
00307
00308
00309
00310 bool
00311 MediaHandler::setAttachPrefix(const Pathname &attach_prefix)
00312 {
00313 if( attach_prefix.empty())
00314 {
00315 MIL << "Reseting to built-in attach point prefixes."
00316 << std::endl;
00317 MediaHandler::_attachPrefix = attach_prefix;
00318 return true;
00319 }
00320 else
00321 if( MediaHandler::checkAttachPoint(attach_prefix, false, true))
00322 {
00323 MIL << "Setting user defined attach point prefix: "
00324 << attach_prefix << std::endl;
00325 MediaHandler::_attachPrefix = attach_prefix;
00326 return true;
00327 }
00328 return false;
00329 }
00330
00332
00333
00334
00335
00336
00337
00338
00339 Pathname
00340 MediaHandler::createAttachPoint() const
00341 {
00343
00345 const char * defmounts[] = {
00346 "/var/adm/mount", filesystem::TmpPath::defaultLocation().c_str(), NULL
00347 };
00348
00349 Pathname apoint;
00350 Pathname aroot( MediaHandler::_attachPrefix);
00351
00352 if( !aroot.empty())
00353 {
00354 apoint = createAttachPoint(aroot);
00355 }
00356 for ( const char ** def = defmounts; *def && apoint.empty(); ++def ) {
00357 aroot = *def;
00358 if( aroot.empty())
00359 continue;
00360
00361 apoint = createAttachPoint(aroot);
00362 }
00363
00364 if ( aroot.empty() ) {
00365 ERR << "Create attach point: Can't find a writable directory to create an attach point" << std::endl;
00366 return aroot;
00367 }
00368
00369 if ( !apoint.empty() ) {
00370 MIL << "Created default attach point " << apoint << std::endl;
00371 }
00372 return apoint;
00373 }
00374
00375 Pathname
00376 MediaHandler::createAttachPoint(const Pathname &attach_root) const
00377 {
00378 Pathname apoint;
00379
00380 if( attach_root.empty() || !attach_root.absolute()) {
00381 ERR << "Create attach point: invalid attach root: '"
00382 << attach_root << "'" << std::endl;
00383 return apoint;
00384 }
00385
00386 PathInfo adir( attach_root);
00387 if( !adir.isDir() || (getuid() != 0 && !adir.userMayRWX())) {
00388 DBG << "Create attach point: attach root is not a writable directory: '"
00389 << attach_root << "'" << std::endl;
00390 return apoint;
00391 }
00392
00393 DBG << "Trying to create attach point in " << attach_root << std::endl;
00394
00395
00396
00397
00398 #warning Use class TmpDir from TmpPath.h
00399 Pathname abase( attach_root + "AP_" );
00400
00401
00402 for ( unsigned i = 1; i < 1000; ++i ) {
00403 adir( Pathname::extend( abase, str::hexstring( i ) ) );
00404 if ( ! adir.isExist() ) {
00405 int err = mkdir( adir.path() );
00406 if (err == 0 ) {
00407 apoint = getRealPath(adir.asString());
00408 if( apoint.empty())
00409 {
00410 ERR << "Unable to resolve a real path for "
00411 << adir.path() << std::endl;
00412 rmdir(adir.path());
00413 }
00414 break;
00415 }
00416 else
00417 if (err != EEXIST)
00418 break;
00419 }
00420 }
00421
00422 if ( apoint.empty()) {
00423 ERR << "Unable to create an attach point below of "
00424 << attach_root << std::endl;
00425 }
00426 return apoint;
00427 }
00428
00430
00431
00432
00433
00434
00435
00436
00437 bool
00438 MediaHandler::isUseableAttachPoint(const Pathname &path, bool mtab) const
00439 {
00440 MediaManager manager;
00441 return manager.isUseableAttachPoint(path, mtab);
00442 }
00443
00444
00446
00447
00448
00449
00450
00451
00452
00453 void
00454 MediaHandler::setMediaSource(const MediaSourceRef &ref)
00455 {
00456 _mediaSource.reset();
00457 if( ref && !ref->type.empty() && !ref->name.empty())
00458 _mediaSource = ref;
00459 }
00460
00462
00463
00464
00465
00466
00467
00468
00469 AttachedMedia
00470 MediaHandler::attachedMedia() const
00471 {
00472 if ( _mediaSource && _attachPoint)
00473 return AttachedMedia(_mediaSource, _attachPoint);
00474 else
00475 return AttachedMedia();
00476 }
00477
00479
00480
00481
00482
00483
00484
00485
00486 bool
00487 MediaHandler::isSharedMedia() const
00488 {
00489 return !_mediaSource.unique();
00490 }
00491
00493
00494
00495
00496
00497
00498
00499
00500 bool
00501 MediaHandler::checkAttached(bool matchMountFs) const
00502 {
00503 bool _isAttached = false;
00504
00505 AttachedMedia ref( attachedMedia());
00506 if( ref.mediaSource)
00507 {
00508 time_t old_mtime = _attach_mtime;
00509 _attach_mtime = MediaManager::getMountTableMTime();
00510 if( !(old_mtime <= 0 || _attach_mtime != old_mtime))
00511 {
00512
00513 _isAttached = true;
00514 }
00515 else
00516 {
00517 if( old_mtime > 0)
00518 DBG << "Mount table changed - rereading it" << std::endl;
00519 else
00520 DBG << "Forced check of the mount table" << std::endl;
00521
00522 MountEntries entries( MediaManager::getMountEntries());
00523 MountEntries::const_iterator e;
00524 for( e = entries.begin(); e != entries.end(); ++e)
00525 {
00526 bool is_device = false;
00527 std::string dev_path(Pathname(e->src).asString());
00528 PathInfo dev_info;
00529
00530 if( dev_path.compare(0, sizeof("/dev/")-1, "/dev/") == 0 &&
00531 dev_info(e->src) && dev_info.isBlk())
00532 {
00533 is_device = true;
00534 }
00535
00536 if( is_device && (ref.mediaSource->maj_nr &&
00537 ref.mediaSource->bdir.empty()))
00538 {
00539 std::string mtype(matchMountFs ? e->type : ref.mediaSource->type);
00540 MediaSource media(mtype, e->src, dev_info.major(), dev_info.minor());
00541
00542 if( ref.mediaSource->equals( media) &&
00543 ref.attachPoint->path == Pathname(e->dir))
00544 {
00545 DBG << "Found media device "
00546 << ref.mediaSource->asString()
00547 << " in the mount table as " << e->src << std::endl;
00548 _isAttached = true;
00549 break;
00550 }
00551
00552 }
00553 else
00554 if(!is_device && (!ref.mediaSource->maj_nr ||
00555 !ref.mediaSource->bdir.empty()))
00556 {
00557 std::string mtype(matchMountFs ? e->type : ref.mediaSource->type);
00558 if( ref.mediaSource->bdir.empty())
00559 {
00560 MediaSource media(mtype, e->src);
00561
00562 if( ref.mediaSource->equals( media) &&
00563 ref.attachPoint->path == Pathname(e->dir))
00564 {
00565 DBG << "Found media name "
00566 << ref.mediaSource->asString()
00567 << " in the mount table as " << e->src << std::endl;
00568 _isAttached = true;
00569 break;
00570 }
00571 }
00572 else
00573 {
00574 if(ref.mediaSource->bdir == e->src &&
00575 ref.attachPoint->path == Pathname(e->dir))
00576 {
00577 DBG << "Found bound media "
00578 << ref.mediaSource->asString()
00579 << " in the mount table as " << e->src << std::endl;
00580 _isAttached = true;
00581 break;
00582 }
00583 }
00584
00585 }
00586 }
00587
00588 if( !_isAttached)
00589 {
00590 if( entries.empty())
00591 {
00592 ERR << "Unable to find any entry in the /etc/mtab file" << std::endl;
00593 }
00594 else
00595 {
00596 MountEntries::const_iterator e;
00597 for( e = entries.begin(); e != entries.end(); ++e)
00598 {
00599 XXX << "mount entry: " << e->src << " on " << e->dir
00600 << " type " << e->type << "(" << e->opts << ")" << endl;
00601 }
00602 }
00603 if( old_mtime > 0)
00604 {
00605 ERR << "Attached media not in mount table any more - forcing reset!"
00606 << std::endl;
00607
00608 _mediaSource.reset();
00609 }
00610 else
00611 {
00612 WAR << "Attached media not in mount table ..." << std::endl;
00613 }
00614
00615
00616
00617 _attach_mtime = 0;
00618 }
00619 }
00620 }
00621 return _isAttached;
00622 }
00623
00625
00626
00627
00628
00629
00630
00631
00632 void MediaHandler::attach( bool next )
00633 {
00634 if ( isAttached() )
00635 return;
00636
00637
00638
00639 setMediaSource(MediaSourceRef());
00640
00641 AttachPoint ap( attachPointHint());
00642 setAttachPoint(ap.path, ap.temp);
00643
00644 try
00645 {
00646 attachTo( next );
00647 }
00648 catch(const MediaException &e)
00649 {
00650 removeAttachPoint();
00651 ZYPP_RETHROW(e);
00652 }
00653 MIL << "Attached: " << *this << endl;
00654 }
00655
00656
00658
00659
00660
00661
00662
00663 Pathname MediaHandler::localPath( const Pathname & pathname ) const
00664 {
00665 Pathname _localRoot( localRoot());
00666 if ( _localRoot.empty() )
00667 return _localRoot;
00668
00669
00670
00671
00672
00673 return _localRoot + pathname.absolutename();
00674 }
00675
00676
00677
00678
00679
00681
00682
00683
00684
00685
00686 void MediaHandler::disconnect()
00687 {
00688 if ( !isAttached() )
00689 return;
00690
00691 disconnectFrom();
00692 MIL << "Disconnected: " << *this << endl;
00693 }
00694
00696
00697
00698
00699
00700
00701
00702
00703 void MediaHandler::release( bool eject )
00704 {
00705 if ( !isAttached() ) {
00706 DBG << "Request to release media - not attached; eject " << eject << std::endl;
00707 if ( eject )
00708 forceEject();
00709 return;
00710 }
00711
00712 DBG << "Request to release attached media "
00713 << _mediaSource->asString()
00714 << ", use count=" << _mediaSource.use_count()
00715 << std::endl;
00716
00717 if( _mediaSource.unique())
00718 {
00719 DBG << "Releasing media " << _mediaSource->asString() << std::endl;
00720 try {
00721 releaseFrom( eject );
00722 }
00723 catch(const MediaNotEjectedException &e)
00724 {
00725
00726
00727
00728
00729 _mediaSource.reset(NULL);
00730 removeAttachPoint();
00731
00732 ZYPP_RETHROW(e);
00733 }
00734 _mediaSource.reset(NULL);
00735 removeAttachPoint();
00736 }
00737 else if( eject) {
00738
00739
00740
00741
00742
00743 MediaSourceRef media( new MediaSource(*_mediaSource));
00744 _mediaSource.reset(NULL);
00745
00746 MediaManager manager;
00747 manager.forceReleaseShared(media);
00748
00749 setMediaSource(media);
00750 DBG << "Releasing media (forced) " << _mediaSource->asString() << std::endl;
00751 try {
00752 releaseFrom( eject );
00753 }
00754 catch(const MediaNotEjectedException &e)
00755 {
00756
00757
00758
00759
00760 _mediaSource.reset(NULL);
00761 removeAttachPoint();
00762
00763 ZYPP_RETHROW(e);
00764 }
00765 _mediaSource.reset(NULL);
00766 removeAttachPoint();
00767 }
00768 else {
00769 DBG << "Releasing shared media reference only" << std::endl;
00770 _mediaSource.reset(NULL);
00771 setAttachPoint("", true);
00772 }
00773 MIL << "Released: " << *this << endl;
00774 }
00775
00776 bool MediaHandler::isAutoMountedMedia(const AttachedMedia &media)
00777 {
00778 (void)media;
00779 return false;
00780 }
00781
00782 void MediaHandler::forceRelaseAllMedia(bool matchMountFs, bool autoMountedOny)
00783 {
00784 forceRelaseAllMedia( attachedMedia().mediaSource, matchMountFs, autoMountedOny);
00785 }
00786
00787 void MediaHandler::forceRelaseAllMedia(const MediaSourceRef &ref,
00788 bool matchMountFs,
00789 bool autoMountedOny)
00790 {
00791 if( !ref)
00792 return;
00793
00794 MountEntries entries( MediaManager::getMountEntries());
00795 MountEntries::const_iterator e;
00796 for( e = entries.begin(); e != entries.end(); ++e)
00797 {
00798 bool is_device = false;
00799 std::string dev_path(Pathname(e->src).asString());
00800 PathInfo dev_info;
00801
00802 if( dev_path.compare(0, sizeof("/dev/")-1, "/dev/") == 0 &&
00803 dev_info(e->src) && dev_info.isBlk())
00804 {
00805 is_device = true;
00806 }
00807
00808 if( is_device && ref->maj_nr)
00809 {
00810 std::string mtype(matchMountFs ? e->type : ref->type);
00811 MediaSource media(mtype, e->src, dev_info.major(), dev_info.minor());
00812
00813 if( ref->equals( media) && e->type != "subfs")
00814 {
00815 if(autoMountedOny)
00816 {
00817 try {
00818 AttachedMedia am(MediaSourceRef(new MediaSource(media)),
00819 AttachPointRef(new AttachPoint(e->dir)));
00820 if( !isAutoMountedMedia(am))
00821 continue;
00822 }
00823 catch(...)
00824 {
00825 continue;
00826 }
00827 }
00828 DBG << "Forcing release of media device "
00829 << ref->asString()
00830 << " in the mount table as "
00831 << e->src << std::endl;
00832 try {
00833 Mount mount;
00834 mount.umount(e->dir);
00835 }
00836 catch (const Exception &e)
00837 {
00838 ZYPP_CAUGHT(e);
00839 }
00840 }
00841 }
00842 else
00843 if(!is_device && !ref->maj_nr)
00844 {
00845 std::string mtype(matchMountFs ? e->type : ref->type);
00846 MediaSource media(mtype, e->src);
00847 if( ref->equals( media))
00848 {
00849 if(autoMountedOny)
00850 {
00851 try {
00852 AttachedMedia am(MediaSourceRef(new MediaSource(media)),
00853 AttachPointRef(new AttachPoint(e->dir)));
00854 if( !isAutoMountedMedia(am))
00855 continue;
00856 }
00857 catch(...)
00858 {
00859 continue;
00860 }
00861 }
00862 DBG << "Forcing release of media name "
00863 << ref->asString()
00864 << " in the mount table as "
00865 << e->src << std::endl;
00866 try {
00867 Mount mount;
00868 mount.umount(e->dir);
00869 }
00870 catch (const Exception &e)
00871 {
00872 ZYPP_CAUGHT(e);
00873 }
00874 }
00875 }
00876 }
00877 }
00878
00879 bool
00880 MediaHandler::checkAttachPoint(const Pathname &apoint) const
00881 {
00882 return MediaHandler::checkAttachPoint( apoint, true, false);
00883 }
00884
00885
00886 bool
00887 MediaHandler::checkAttachPoint(const Pathname &apoint,
00888 bool emptydir,
00889 bool writeable)
00890 {
00891 if( apoint.empty() || !apoint.absolute())
00892 {
00893 ERR << "Attach point '" << apoint << "' is not absolute"
00894 << std::endl;
00895 return false;
00896 }
00897 if( apoint == "/")
00898 {
00899 ERR << "Attach point '" << apoint << "' is not allowed"
00900 << std::endl;
00901 return false;
00902 }
00903
00904 PathInfo ainfo(apoint);
00905 if( !ainfo.isDir())
00906 {
00907 ERR << "Attach point '" << apoint << "' is not a directory"
00908 << std::endl;
00909 return false;
00910 }
00911
00912 if( emptydir)
00913 {
00914 if( 0 != zypp::filesystem::is_empty_dir(apoint))
00915 {
00916 ERR << "Attach point '" << apoint << "' is not a empty directory"
00917 << std::endl;
00918 return false;
00919 }
00920 }
00921
00922 if( writeable)
00923 {
00924 Pathname apath(apoint + "XXXXXX");
00925 char *atemp = ::strdup( apath.asString().c_str());
00926 char *atest = NULL;
00927 if( !ainfo.userMayRWX() || atemp == NULL ||
00928 (atest=::mkdtemp(atemp)) == NULL)
00929 {
00930 if( atemp != NULL)
00931 ::free(atemp);
00932
00933 ERR << "Attach point '" << ainfo.path()
00934 << "' is not a writeable directory" << std::endl;
00935 return false;
00936 }
00937 else if( atest != NULL)
00938 ::rmdir(atest);
00939
00940 if( atemp != NULL)
00941 ::free(atemp);
00942 }
00943 return true;
00944 }
00945
00947
00948
00949
00950
00951
00952
00953 bool
00954 MediaHandler::dependsOnParent()
00955 {
00956 return _parentId != 0;
00957 }
00958
00959 bool
00960 MediaHandler::dependsOnParent(MediaAccessId parentId, bool exactIdMatch)
00961 {
00962 if( _parentId != 0)
00963 {
00964 if(parentId == _parentId)
00965 return true;
00966
00967 if( !exactIdMatch)
00968 {
00969 MediaManager mm;
00970 AttachedMedia am1 = mm.getAttachedMedia(_parentId);
00971 AttachedMedia am2 = mm.getAttachedMedia(parentId);
00972 if( am1.mediaSource && am2.mediaSource)
00973 {
00974 return am1.mediaSource->equals( *(am2.mediaSource));
00975 }
00976 }
00977 }
00978 return false;
00979 }
00980
00982
00983
00984
00985
00986
00987
00988
00989 void MediaHandler::provideFileCopy( Pathname srcFilename,
00990 Pathname targetFilename ) const
00991 {
00992 if ( !isAttached() ) {
00993 INT << "Media not_attached on provideFileCopy(" << srcFilename
00994 << "," << targetFilename << ")" << endl;
00995 ZYPP_THROW(MediaNotAttachedException(url()));
00996 }
00997
00998 getFileCopy( srcFilename, targetFilename );
00999 DBG << "provideFileCopy(" << srcFilename << "," << targetFilename << ")" << endl;
01000 }
01001
01002 void MediaHandler::provideFile( Pathname filename ) const
01003 {
01004 if ( !isAttached() ) {
01005 INT << "Error: Not attached on provideFile(" << filename << ")" << endl;
01006 ZYPP_THROW(MediaNotAttachedException(url()));
01007 }
01008
01009 getFile( filename );
01010 DBG << "provideFile(" << filename << ")" << endl;
01011 }
01012
01013
01015
01016
01017
01018
01019
01020
01021
01022 void MediaHandler::provideDir( Pathname dirname ) const
01023 {
01024 if ( !isAttached() ) {
01025 INT << "Error: Not attached on provideDir(" << dirname << ")" << endl;
01026 ZYPP_THROW(MediaNotAttachedException(url()));
01027 }
01028
01029 getDir( dirname, false );
01030 MIL << "provideDir(" << dirname << ")" << endl;
01031 }
01032
01034
01035
01036
01037
01038
01039
01040
01041 void MediaHandler::provideDirTree( Pathname dirname ) const
01042 {
01043 if ( !isAttached() ) {
01044 INT << "Error Not attached on provideDirTree(" << dirname << ")" << endl;
01045 ZYPP_THROW(MediaNotAttachedException(url()));
01046 }
01047
01048 getDir( dirname, true );
01049 MIL << "provideDirTree(" << dirname << ")" << endl;
01050 }
01051
01053
01054
01055
01056
01057
01058
01059
01060 void MediaHandler::releasePath( Pathname pathname ) const
01061 {
01062 if ( ! _does_download || _attachPoint->empty() )
01063 return;
01064
01065 PathInfo info( localPath( pathname ) );
01066
01067 if ( info.isFile() ) {
01068 unlink( info.path() );
01069 } else if ( info.isDir() ) {
01070 if ( info.path() != localRoot() ) {
01071 recursive_rmdir( info.path() );
01072 } else {
01073 clean_dir( info.path() );
01074 }
01075 }
01076 }
01077
01079
01080
01081
01082
01083
01084
01085
01086 void MediaHandler::dirInfo( std::list<std::string> & retlist,
01087 const Pathname & dirname, bool dots ) const
01088 {
01089 retlist.clear();
01090
01091 if ( !isAttached() ) {
01092 INT << "Error: Not attached on dirInfo(" << dirname << ")" << endl;
01093 ZYPP_THROW(MediaNotAttachedException(url()));
01094 }
01095
01096 getDirInfo( retlist, dirname, dots );
01097 MIL << "dirInfo(" << dirname << ")" << endl;
01098 }
01099
01101
01102
01103
01104
01105
01106
01107
01108 void MediaHandler::dirInfo( filesystem::DirContent & retlist,
01109 const Pathname & dirname, bool dots ) const
01110 {
01111 retlist.clear();
01112
01113 if ( !isAttached() ) {
01114 INT << "Error: Not attached on dirInfo(" << dirname << ")" << endl;
01115 ZYPP_THROW(MediaNotAttachedException(url()));
01116 }
01117
01118 getDirInfo( retlist, dirname, dots );
01119 MIL << "dirInfo(" << dirname << ")" << endl;
01120 }
01121
01123
01124
01125
01126
01127
01128
01129
01130 bool MediaHandler::doesFileExist( const Pathname & filename ) const
01131 {
01132
01133 if ( !isAttached() ) {
01134 INT << "Error Not attached on doesFileExist(" << filename << ")" << endl;
01135 ZYPP_THROW(MediaNotAttachedException(url()));
01136 }
01137 return getDoesFileExist( filename );
01138 MIL << "doesFileExist(" << filename << ")" << endl;
01139 }
01140
01142
01143
01144
01145
01146
01147 void MediaHandler::getDirectoryYast( std::list<std::string> & retlist,
01148 const Pathname & dirname, bool dots ) const
01149 {
01150 retlist.clear();
01151
01152 filesystem::DirContent content;
01153 getDirectoryYast( content, dirname, dots );
01154
01155
01156 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
01157 retlist.push_back( it->name );
01158 }
01159 }
01160
01162
01163
01164
01165
01166
01167 void MediaHandler::getDirectoryYast( filesystem::DirContent & retlist,
01168 const Pathname & dirname, bool dots ) const
01169 {
01170 retlist.clear();
01171
01172
01173 Pathname dirFile = dirname + "directory.yast";
01174 getFile( dirFile );
01175 DBG << "provideFile(" << dirFile << "): " << "OK" << endl;
01176
01177
01178 ifstream dir( localPath( dirFile ).asString().c_str() );
01179 if ( dir.fail() ) {
01180 ERR << "Unable to load '" << localPath( dirFile ) << "'" << endl;
01181 ZYPP_THROW(MediaSystemException(url(),
01182 "Unable to load '" + localPath( dirFile ).asString() + "'"));
01183 }
01184
01185 string line;
01186 while( getline( dir, line ) ) {
01187 if ( line.empty() ) continue;
01188 if ( line == "directory.yast" ) continue;
01189
01190
01191
01192 filesystem::FileType type = filesystem::FT_NOT_AVAIL;
01193 if ( *line.rbegin() == '/' ) {
01194 line.erase( line.end()-1 );
01195 type = filesystem::FT_DIR;
01196 }
01197
01198 if ( dots ) {
01199 if ( line == "." || line == ".." ) continue;
01200 } else {
01201 if ( *line.begin() == '.' ) continue;
01202 }
01203
01204 retlist.push_back( filesystem::DirEntry( line, type ) );
01205 }
01206 }
01207
01208
01209
01210
01211
01212
01213
01214 ostream & operator<<( ostream & str, const MediaHandler & obj )
01215 {
01216 str << obj.url() << ( obj.isAttached() ? "" : " not" )
01217 << " attached; localRoot \"" << obj.localRoot() << "\"";
01218 return str;
01219 }
01220
01222
01223
01224
01225
01226
01227
01228
01229
01230 void MediaHandler::getFile( const Pathname & filename ) const
01231 {
01232 PathInfo info( localPath( filename ) );
01233 if( info.isFile() ) {
01234 return;
01235 }
01236
01237 if (info.isExist())
01238 ZYPP_THROW(MediaNotAFileException(url(), localPath(filename)));
01239 else
01240 ZYPP_THROW(MediaFileNotFoundException(url(), filename));
01241 }
01242
01243
01244 void MediaHandler::getFileCopy ( const Pathname & srcFilename, const Pathname & targetFilename ) const
01245 {
01246 getFile(srcFilename);
01247
01248 if ( copy( localPath( srcFilename ), targetFilename ) != 0 ) {
01249 ZYPP_THROW(MediaWriteException(targetFilename));
01250 }
01251 }
01252
01253
01254
01256
01257
01258
01259
01260
01261
01262
01263
01264 void MediaHandler::getDir( const Pathname & dirname, bool recurse_r ) const
01265 {
01266 PathInfo info( localPath( dirname ) );
01267 if( info.isDir() ) {
01268 return;
01269 }
01270
01271 if (info.isExist())
01272 ZYPP_THROW(MediaNotADirException(url(), localPath(dirname)));
01273 else
01274 ZYPP_THROW(MediaFileNotFoundException(url(), dirname));
01275 }
01276
01278
01279
01280
01281
01282
01283
01284
01285
01286 void MediaHandler::getDirInfo( std::list<std::string> & retlist,
01287 const Pathname & dirname, bool dots ) const
01288 {
01289 PathInfo info( localPath( dirname ) );
01290 if( ! info.isDir() ) {
01291 ZYPP_THROW(MediaNotADirException(url(), localPath(dirname)));
01292 }
01293
01294 #if NONREMOTE_DIRECTORY_YAST
01295
01296 try {
01297 getDirectoryYast( retlist, dirname, dots );
01298 }
01299 catch (const MediaException & excpt_r)
01300 {
01301 #endif
01302
01303
01304 int res = readdir( retlist, info.path(), dots );
01305 if ( res )
01306 {
01307 MediaSystemException nexcpt(url(), "readdir failed");
01308 #if NONREMOTE_DIRECTORY_YAST
01309 nexcpt.remember(excpt_r);
01310 #endif
01311 ZYPP_THROW(nexcpt);
01312 }
01313
01314 #if NONREMOTE_DIRECTORY_YAST
01315 }
01316 #endif
01317
01318 return;
01319 }
01320
01322
01323
01324
01325
01326
01327
01328
01329
01330 void MediaHandler::getDirInfo( filesystem::DirContent & retlist,
01331 const Pathname & dirname, bool dots ) const
01332 {
01333 PathInfo info( localPath( dirname ) );
01334 if( ! info.isDir() ) {
01335 ZYPP_THROW(MediaNotADirException(url(), localPath(dirname)));
01336 }
01337
01338 #if NONREMOTE_DIRECTORY_YAST
01339
01340 try {
01341 getDirectoryYast( retlist, dirname, dots );
01342 }
01343 catch (const MediaException & excpt_r)
01344 {
01345 #endif
01346
01347
01348 int res = readdir( retlist, info.path(), dots );
01349 if ( res )
01350 {
01351 MediaSystemException nexcpt(url(), "readdir failed");
01352 #if NONREMOTE_DIRECTORY_YAST
01353 nexcpt.remember(excpt_r);
01354 #endif
01355 ZYPP_THROW(nexcpt);
01356 }
01357 #if NONREMOTE_DIRECTORY_YAST
01358 }
01359 #endif
01360 }
01361
01363
01364
01365
01366
01367
01368
01369
01370
01371 bool MediaHandler::getDoesFileExist( const Pathname & filename ) const
01372 {
01373 PathInfo info( localPath( filename ) );
01374 if( info.isDir() ) {
01375 ZYPP_THROW(MediaNotAFileException(url(), localPath(filename)));
01376 }
01377 return info.isExist();
01378 }
01379
01380 bool MediaHandler::hasMoreDevices()
01381 {
01382 return false;
01383 }
01384
01385 }
01386 }
01387