34 #define NONREMOTE_DIRECTORY_YAST 1
39 Pathname MediaHandler::_attachPrefix(
"");
55 MediaHandler::MediaHandler (
const Url & url_r,
56 const Pathname & attach_point_r,
57 const Pathname & urlpath_below_attachpoint_r,
58 const bool does_download_r )
62 , _relativeRoot( urlpath_below_attachpoint_r)
63 , _does_download( does_download_r )
68 Pathname real_attach_point(
getRealPath(attach_point_r.asString()));
70 if ( !real_attach_point.empty() ) {
75 PathInfo adir( real_attach_point );
86 && !real_attach_point.absolute()) )
88 ERR <<
"Provided attach point is not a absolute directory: "
129 char *ptr = ::realpath(path.c_str(), NULL);
139 char buff[PATH_MAX + 2];
140 memset(buff,
'\0',
sizeof(buff));
141 if( ::realpath(path.c_str(), buff) != NULL)
147 char buff[PATH_MAX + 2];
148 memset(buff,
'\0',
sizeof(buff));
149 if( ::realpath(path.c_str(), buff) != NULL)
161 return zypp::Pathname(
getRealPath(path.asString()));
177 INT <<
"MediaHandler deleted with media attached." << endl;
181 DBG <<
"MediaHandler - checking if to remove attach point" << endl;
193 MIL <<
"Deleted default attach point " << path << endl;
195 ERR <<
"Failed to Delete default attach point " << path
196 <<
" errno(" << res <<
")" << endl;
202 DBG <<
"MediaHandler - attachpoint is not temporary" << endl;
316 if( attach_prefix.empty())
318 MIL <<
"Reseting to built-in attach point prefixes."
320 MediaHandler::_attachPrefix = attach_prefix;
326 MIL <<
"Setting user defined attach point prefix: "
327 << attach_prefix << std::endl;
328 MediaHandler::_attachPrefix = attach_prefix;
349 if ( ! aroot.empty() )
353 if ( apoint.empty() )
356 if ( ! aroot.empty() )
360 if ( apoint.empty() )
363 if ( ! aroot.empty() )
367 if ( apoint.empty() )
370 except.addHistory(
_(
"Create attach point: Can't find a writable directory to create an attach point") );
374 MIL <<
"Created default attach point " << apoint << std::endl;
383 if( attach_root.empty() || !attach_root.absolute()) {
384 ERR <<
"Create attach point: invalid attach root: '"
385 << attach_root <<
"'" << std::endl;
389 PathInfo adir( attach_root );
390 if( !adir.isDir() || (geteuid() != 0 && !adir.userMayRWX())) {
391 DBG <<
"Create attach point: attach root is not a writable directory: '"
392 << attach_root <<
"'" << std::endl;
396 static bool cleanup_once(
true );
399 cleanup_once =
false;
400 DBG <<
"Look for orphaned attach points in " << adir << std::endl;
401 std::list<std::string> entries;
403 for (
const std::string & entry : entries )
407 PathInfo sdir( attach_root + entry );
409 && sdir.dev() == adir.dev()
412 DBG <<
"Remove orphaned attach point " << sdir << std::endl;
422 if ( ! apoint.empty() )
428 ERR <<
"Unable to resolve real path for attach point " << tmpdir << std::endl;
433 ERR <<
"Unable to create attach point below " << attach_root << std::endl;
466 if( ref && !ref->type.empty() && !ref->name.empty())
512 bool _isAttached =
false;
527 DBG <<
"Mount table changed - rereading it" << std::endl;
529 DBG <<
"Forced check of the mount table" << std::endl;
532 for_( e, entries.begin(), entries.end() )
537 bool is_device =
false;
540 dev_info(e->src) && dev_info.isBlk() )
548 std::string mtype(matchMountFs ? e->type : ref.
mediaSource->type);
549 MediaSource media(mtype, e->src, dev_info.devMajor(), dev_info.devMinor());
553 DBG <<
"Found media device "
555 <<
" in the mount table as " << e->src << std::endl;
569 if ( matchMountFs && e->type != ref.
mediaSource->type )
572 matchMountFs =
false;
573 else if ( ( e->type ==
"cifs" || e->type ==
"smb" ) && ( ref.
mediaSource->type ==
"cifs" || ref.
mediaSource->type ==
"smb" ) )
574 matchMountFs =
false;
593 DBG <<
"Found media name "
595 <<
" in the mount table as " << e->src << std::endl;
604 DBG <<
"Found bound media "
606 <<
" in the mount table as " << e->src << std::endl;
621 DBG <<
"Found bound media "
623 <<
" in the mount table as " << e->src << std::endl;
632 MIL <<
"Looking for " << ref << endl;
633 if( entries.empty() )
635 ERR <<
"Unable to find any entry in the /etc/mtab file" << std::endl;
639 dumpRange(
DBG <<
"MountEntries: ", entries.begin(), entries.end() ) << endl;
643 ERR <<
"Attached media not in mount table any more - forcing reset!"
650 WAR <<
"Attached media not in mount table ..." << std::endl;
691 MIL <<
"Attached: " << *
this << endl;
704 if ( _localRoot.empty() )
711 return _localRoot + pathname.absolutename();
730 MIL <<
"Disconnected: " << *
this << endl;
744 DBG <<
"Request to release media - not attached; eject '" << ejectDev <<
"'"
746 if ( !ejectDev.empty() )
751 DBG <<
"Request to release attached media "
776 else if( !ejectDev.empty() ) {
789 DBG <<
"Releasing media (forced) " <<
_mediaSource->asString() << std::endl;
808 DBG <<
"Releasing shared media reference only" << std::endl;
812 MIL <<
"Released: " << *
this << endl;
827 MountEntries::const_iterator e;
828 for( e = entries.begin(); e != entries.end(); ++e)
830 bool is_device =
false;
834 dev_info(e->src) && dev_info.isBlk())
839 if( is_device && ref->maj_nr)
841 std::string mtype(matchMountFs ? e->type : ref->type);
842 MediaSource media(mtype, e->src, dev_info.devMajor(), dev_info.devMinor());
844 if( ref->equals( media) && e->type !=
"subfs")
846 DBG <<
"Forcing release of media device "
848 <<
" in the mount table as "
849 << e->src << std::endl;
861 if(!is_device && !ref->maj_nr)
863 std::string mtype(matchMountFs ? e->type : ref->type);
865 if( ref->equals( media))
867 DBG <<
"Forcing release of media name "
869 <<
" in the mount table as "
870 << e->src << std::endl;
896 if( apoint.empty() || !apoint.absolute())
898 ERR <<
"Attach point '" << apoint <<
"' is not absolute"
904 ERR <<
"Attach point '" << apoint <<
"' is not allowed"
909 PathInfo ainfo(apoint);
912 ERR <<
"Attach point '" << apoint <<
"' is not a directory"
921 ERR <<
"Attach point '" << apoint <<
"' is not a empty directory"
929 Pathname apath(apoint +
"XXXXXX");
930 char *atemp = ::strdup( apath.asString().c_str());
932 if( !ainfo.userMayRWX() || atemp == NULL ||
933 (atest=::mkdtemp(atemp)) == NULL)
938 ERR <<
"Attach point '" << ainfo.path()
939 <<
"' is not a writeable directory" << std::endl;
942 else if( atest != NULL)
995 Pathname targetFilename ,
const ByteCount &expectedFileSize_r)
const
998 INT <<
"Media not_attached on provideFileCopy(" << srcFilename
999 <<
"," << targetFilename <<
")" << endl;
1003 getFileCopy( srcFilename, targetFilename, expectedFileSize_r );
1004 DBG <<
"provideFileCopy(" << srcFilename <<
"," << targetFilename <<
")" << endl;
1010 INT <<
"Error: Not attached on provideFile(" << filename <<
")" << endl;
1014 getFile( filename, expectedFileSize_r );
1015 DBG <<
"provideFile(" << filename <<
")" << endl;
1030 INT <<
"Error: Not attached on provideDir(" << dirname <<
")" << endl;
1034 getDir( dirname,
false );
1035 MIL <<
"provideDir(" << dirname <<
")" << endl;
1049 INT <<
"Error Not attached on provideDirTree(" << dirname <<
")" << endl;
1054 MIL <<
"provideDirTree(" << dirname <<
")" << endl;
1072 if ( info.isFile() ) {
1074 }
else if ( info.isDir() ) {
1092 const Pathname & dirname,
bool dots )
const
1097 INT <<
"Error: Not attached on dirInfo(" << dirname <<
")" << endl;
1102 MIL <<
"dirInfo(" << dirname <<
")" << endl;
1114 const Pathname & dirname,
bool dots )
const
1119 INT <<
"Error: Not attached on dirInfo(" << dirname <<
")" << endl;
1124 MIL <<
"dirInfo(" << dirname <<
")" << endl;
1139 INT <<
"Error Not attached on doesFileExist(" << filename <<
")" << endl;
1143 MIL <<
"doesFileExist(" << filename <<
")" << endl;
1153 const Pathname & dirname,
bool dots )
const
1161 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1162 retlist.push_back( it->name );
1173 const Pathname & dirname,
bool dots )
const
1178 Pathname dirFile = dirname +
"directory.yast";
1180 DBG <<
"provideFile(" << dirFile <<
"): " <<
"OK" << endl;
1185 ERR <<
"Unable to load '" <<
localPath( dirFile ) <<
"'" << endl;
1191 while(
getline( dir, line ) ) {
1192 if ( line.empty() )
continue;
1193 if ( line ==
"directory.yast" )
continue;
1198 if ( *line.rbegin() ==
'/' ) {
1199 line.erase( line.end()-1 );
1204 if ( line ==
"." || line ==
".." )
continue;
1206 if ( *line.begin() ==
'.' )
continue;
1222 <<
" attached; localRoot \"" << obj.
localRoot() <<
"\"";
1238 if( info.isFile() ) {
1251 getFile(srcFilename, expectedFileSize_r);
1253 if (
copy(
localPath( srcFilename ), targetFilename ) != 0 ) {
1272 if( info.isDir() ) {
1292 const Pathname & dirname,
bool dots )
const
1295 if( ! info.isDir() ) {
1299 #if NONREMOTE_DIRECTORY_YAST
1309 int res =
readdir( retlist, info.path(), dots );
1313 #if NONREMOTE_DIRECTORY_YAST
1319 #if NONREMOTE_DIRECTORY_YAST
1336 const Pathname & dirname,
bool dots )
const
1339 if( ! info.isDir() ) {
1343 #if NONREMOTE_DIRECTORY_YAST
1353 int res =
readdir( retlist, info.path(), dots );
1357 #if NONREMOTE_DIRECTORY_YAST
1362 #if NONREMOTE_DIRECTORY_YAST
1379 if( info.isDir() ) {
1382 return info.isExist();
1391 unsigned int & index)
const
1394 if (!devices.empty())
1398 DBG <<
"No devices for this medium" << endl;
Listentry returned by readdir.
std::string asString(const DefaultIntegral< Tp, TInitial > &obj)
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
static ZConfig & instance()
Singleton ctor.
Store and operate with byte count.
const std::string & asString() const
String representation.
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
static const Pathname & defaultLocation()
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
static const ValueType month
int is_empty_dir(const Pathname &path_r)
Check if the specified directory is empty.
void remember(const Exception &old_r)
Store an other Exception as history.
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
std::string getline(std::istream &str)
Read one line from stream.
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\n ", const std::string &sep="\n ", const std::string &sfx="\n", const std::string &extro="}")
Print range defined by iterators (multiline style).
Provide a new empty temporary directory and recursively delete it when no longer needed.
int unlink(const Pathname &path)
Like 'unlink'.
int recursive_rmdir(const Pathname &path)
Like 'rm -r DIR'.
std::list< DirEntry > DirContent
Returned by readdir.
Pathname download_mediaMountdir() const
Path where media are preferably mounted or downloaded.
std::ostream & copy(std::istream &from_r, std::ostream &to_r)
Copy istream to ostream.
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
int rmdir(const Pathname &path)
Like 'rmdir'.
void swap(RW_pointer &rhs)
Base class for Exception.
FileType
File type information.
static Date now()
Return the current time.
bool autoCleanup() const
Whether path is valid and deleted when the last reference drops.
std::string getScheme() const
Returns the scheme name of the URL.
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.