32 #define NONREMOTE_DIRECTORY_YAST 1
37 Pathname MediaHandler::_attachPrefix(
"");
53 MediaHandler::MediaHandler (
const Url & url_r,
54 const Pathname & attach_point_r,
55 const Pathname & urlpath_below_attachpoint_r,
56 const bool does_download_r )
60 , _relativeRoot( urlpath_below_attachpoint_r)
61 , _does_download( does_download_r )
66 Pathname real_attach_point(
getRealPath(attach_point_r.asString()));
68 if ( !real_attach_point.empty() ) {
73 PathInfo adir( real_attach_point );
84 && !real_attach_point.absolute()) )
86 ERR <<
"Provided attach point is not a absolute directory: "
127 char *ptr = ::realpath(path.c_str(), NULL);
137 char buff[PATH_MAX + 2];
138 memset(buff,
'\0',
sizeof(buff));
139 if( ::realpath(path.c_str(), buff) != NULL)
145 char buff[PATH_MAX + 2];
146 memset(buff,
'\0',
sizeof(buff));
147 if( ::realpath(path.c_str(), buff) != NULL)
159 return zypp::Pathname(
getRealPath(path.asString()));
175 INT <<
"MediaHandler deleted with media attached." << endl;
179 DBG <<
"MediaHandler - checking if to remove attach point" << endl;
191 MIL <<
"Deleted default attach point " << path << endl;
193 ERR <<
"Failed to Delete default attach point " << path
194 <<
" errno(" << res <<
")" << endl;
200 DBG <<
"MediaHandler - attachpoint is not temporary" << endl;
314 if( attach_prefix.empty())
316 MIL <<
"Reseting to built-in attach point prefixes."
318 MediaHandler::_attachPrefix = attach_prefix;
324 MIL <<
"Setting user defined attach point prefix: "
325 << attach_prefix << std::endl;
326 MediaHandler::_attachPrefix = attach_prefix;
346 const char * defmounts[] = {
351 Pathname aroot( MediaHandler::_attachPrefix);
357 for (
const char ** def = defmounts; *def && apoint.empty(); ++def ) {
365 if ( aroot.empty() ) {
366 ERR <<
"Create attach point: Can't find a writable directory to create an attach point" << std::endl;
370 if ( !apoint.empty() ) {
371 MIL <<
"Created default attach point " << apoint << std::endl;
381 if( attach_root.empty() || !attach_root.absolute()) {
382 ERR <<
"Create attach point: invalid attach root: '"
383 << attach_root <<
"'" << std::endl;
387 PathInfo adir( attach_root );
388 if( !adir.isDir() || (geteuid() != 0 && !adir.userMayRWX())) {
389 DBG <<
"Create attach point: attach root is not a writable directory: '"
390 << attach_root <<
"'" << std::endl;
394 static bool cleanup_once(
true );
397 cleanup_once =
false;
398 DBG <<
"Look for orphaned attach points in " << adir << std::endl;
399 std::list<std::string> entries;
401 for (
const std::string & entry : entries )
405 PathInfo sdir( attach_root + entry );
407 && sdir.dev() == adir.dev()
410 DBG <<
"Remove orphaned attach point " << sdir << std::endl;
420 if ( ! apoint.empty() )
426 ERR <<
"Unable to resolve real path for attach point " << tmpdir << std::endl;
431 ERR <<
"Unable to create attach point below " << attach_root << std::endl;
464 if( ref && !ref->type.empty() && !ref->name.empty())
510 bool _isAttached =
false;
525 DBG <<
"Mount table changed - rereading it" << std::endl;
527 DBG <<
"Forced check of the mount table" << std::endl;
530 for_( e, entries.begin(), entries.end() )
535 bool is_device =
false;
538 dev_info(e->src) && dev_info.isBlk() )
546 std::string mtype(matchMountFs ? e->type : ref.
mediaSource->type);
547 MediaSource media(mtype, e->src, dev_info.major(), dev_info.minor());
551 DBG <<
"Found media device "
553 <<
" in the mount table as " << e->src << std::endl;
567 if ( matchMountFs && e->type != ref.
mediaSource->type )
570 matchMountFs =
false;
571 else if ( ( e->type ==
"cifs" || e->type ==
"smb" ) && ( ref.
mediaSource->type ==
"cifs" || ref.
mediaSource->type ==
"smb" ) )
572 matchMountFs =
false;
591 DBG <<
"Found media name "
593 <<
" in the mount table as " << e->src << std::endl;
602 DBG <<
"Found bound media "
604 <<
" in the mount table as " << e->src << std::endl;
615 MIL <<
"Looking for " << ref << endl;
616 if( entries.empty() )
618 ERR <<
"Unable to find any entry in the /etc/mtab file" << std::endl;
622 dumpRange(
DBG <<
"MountEntries: ", entries.begin(), entries.end() ) << endl;
626 ERR <<
"Attached media not in mount table any more - forcing reset!"
633 WAR <<
"Attached media not in mount table ..." << std::endl;
674 MIL <<
"Attached: " << *
this << endl;
687 if ( _localRoot.empty() )
694 return _localRoot + pathname.absolutename();
713 MIL <<
"Disconnected: " << *
this << endl;
727 DBG <<
"Request to release media - not attached; eject '" << ejectDev <<
"'"
729 if ( !ejectDev.empty() )
734 DBG <<
"Request to release attached media "
759 else if( !ejectDev.empty() ) {
772 DBG <<
"Releasing media (forced) " <<
_mediaSource->asString() << std::endl;
791 DBG <<
"Releasing shared media reference only" << std::endl;
795 MIL <<
"Released: " << *
this << endl;
810 MountEntries::const_iterator e;
811 for( e = entries.begin(); e != entries.end(); ++e)
813 bool is_device =
false;
817 dev_info(e->src) && dev_info.isBlk())
822 if( is_device && ref->maj_nr)
824 std::string mtype(matchMountFs ? e->type : ref->type);
825 MediaSource media(mtype, e->src, dev_info.major(), dev_info.minor());
827 if( ref->equals( media) && e->type !=
"subfs")
829 DBG <<
"Forcing release of media device "
831 <<
" in the mount table as "
832 << e->src << std::endl;
844 if(!is_device && !ref->maj_nr)
846 std::string mtype(matchMountFs ? e->type : ref->type);
848 if( ref->equals( media))
850 DBG <<
"Forcing release of media name "
852 <<
" in the mount table as "
853 << e->src << std::endl;
879 if( apoint.empty() || !apoint.absolute())
881 ERR <<
"Attach point '" << apoint <<
"' is not absolute"
887 ERR <<
"Attach point '" << apoint <<
"' is not allowed"
892 PathInfo ainfo(apoint);
895 ERR <<
"Attach point '" << apoint <<
"' is not a directory"
904 ERR <<
"Attach point '" << apoint <<
"' is not a empty directory"
912 Pathname apath(apoint +
"XXXXXX");
913 char *atemp = ::strdup( apath.asString().c_str());
915 if( !ainfo.userMayRWX() || atemp == NULL ||
916 (atest=::mkdtemp(atemp)) == NULL)
921 ERR <<
"Attach point '" << ainfo.path()
922 <<
"' is not a writeable directory" << std::endl;
925 else if( atest != NULL)
978 Pathname targetFilename )
const
981 INT <<
"Media not_attached on provideFileCopy(" << srcFilename
982 <<
"," << targetFilename <<
")" << endl;
987 DBG <<
"provideFileCopy(" << srcFilename <<
"," << targetFilename <<
")" << endl;
993 INT <<
"Error: Not attached on provideFile(" << filename <<
")" << endl;
998 DBG <<
"provideFile(" << filename <<
")" << endl;
1013 INT <<
"Error: Not attached on provideDir(" << dirname <<
")" << endl;
1017 getDir( dirname,
false );
1018 MIL <<
"provideDir(" << dirname <<
")" << endl;
1032 INT <<
"Error Not attached on provideDirTree(" << dirname <<
")" << endl;
1037 MIL <<
"provideDirTree(" << dirname <<
")" << endl;
1055 if ( info.isFile() ) {
1057 }
else if ( info.isDir() ) {
1075 const Pathname & dirname,
bool dots )
const
1080 INT <<
"Error: Not attached on dirInfo(" << dirname <<
")" << endl;
1085 MIL <<
"dirInfo(" << dirname <<
")" << endl;
1097 const Pathname & dirname,
bool dots )
const
1102 INT <<
"Error: Not attached on dirInfo(" << dirname <<
")" << endl;
1107 MIL <<
"dirInfo(" << dirname <<
")" << endl;
1122 INT <<
"Error Not attached on doesFileExist(" << filename <<
")" << endl;
1126 MIL <<
"doesFileExist(" << filename <<
")" << endl;
1136 const Pathname & dirname,
bool dots )
const
1144 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
1145 retlist.push_back( it->name );
1156 const Pathname & dirname,
bool dots )
const
1161 Pathname dirFile = dirname +
"directory.yast";
1163 DBG <<
"provideFile(" << dirFile <<
"): " <<
"OK" << endl;
1168 ERR <<
"Unable to load '" <<
localPath( dirFile ) <<
"'" << endl;
1174 while(
getline( dir, line ) ) {
1175 if ( line.empty() )
continue;
1176 if ( line ==
"directory.yast" )
continue;
1181 if ( *line.rbegin() ==
'/' ) {
1182 line.erase( line.end()-1 );
1187 if ( line ==
"." || line ==
".." )
continue;
1189 if ( *line.begin() ==
'.' )
continue;
1205 <<
" attached; localRoot \"" << obj.
localRoot() <<
"\"";
1221 if( info.isFile() ) {
1236 if (
copy(
localPath( srcFilename ), targetFilename ) != 0 ) {
1255 if( info.isDir() ) {
1275 const Pathname & dirname,
bool dots )
const
1278 if( ! info.isDir() ) {
1282 #if NONREMOTE_DIRECTORY_YAST
1292 int res =
readdir( retlist, info.path(), dots );
1296 #if NONREMOTE_DIRECTORY_YAST
1302 #if NONREMOTE_DIRECTORY_YAST
1319 const Pathname & dirname,
bool dots )
const
1322 if( ! info.isDir() ) {
1326 #if NONREMOTE_DIRECTORY_YAST
1336 int res =
readdir( retlist, info.path(), dots );
1340 #if NONREMOTE_DIRECTORY_YAST
1345 #if NONREMOTE_DIRECTORY_YAST
1362 if( info.isDir() ) {
1365 return info.isExist();
1374 unsigned int & index)
const
1377 if (!devices.empty())
1381 DBG <<
"No devices for this medium" << endl;
Listentry returned by readdir.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
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, _Iterator begin, _Iterator 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'.
const char * c_str() const
String representation.
int recursive_rmdir(const Pathname &path)
Like 'rm -r DIR'.
std::list< DirEntry > DirContent
Returned by readdir.
std::string asString(const Patch::SeverityFlag &obj)
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'.
Base class for Exception.
FileType
File type information.
static Date now()
Return the current time.
void swap(RW_pointer &rhs)
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.