Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

main.cpp

Go to the documentation of this file.
00001 
00002 /* OPIE */
00003 #include <opie2/odebug.h>
00004 
00005 /* QT */
00006 #include <qapplication.h>
00007 #include <qfile.h>
00008 #include <qfileinfo.h>
00009 #include <qdir.h>
00010 #include <qtextstream.h>
00011 #include <qstringlist.h>
00012 
00013 /* STD */
00014 #include <stdlib.h>
00015 #include <unistd.h> //symlink()
00016 #include <sys/stat.h> // mkdir()
00017 
00018 #include <sys/vfs.h>
00019 #include <mntent.h>
00020 #include <errno.h>
00021 
00022 static const char *listDir = "/usr/lib/ipkg/externinfo/";
00023 
00024 static void createSymlinks( const QString &location, const QString &package )
00025 {
00026 
00027     QFile inFile( location + "/usr/lib/ipkg/info/" + package + ".list" );
00028     mkdir( "/usr/lib/ipkg", 0777 );
00029     mkdir( listDir, 0777 );
00030 
00031     QFile outFile( listDir + package + ".list");
00032 
00033 //    odebug << "createSymlinks " << inFile.name().ascii() << " -> " << outFile.name().ascii() << "" << oendl;
00034 
00035 
00036 
00037     if ( inFile.open(IO_ReadOnly) && outFile.open(IO_WriteOnly)) {
00038     QTextStream in(&inFile);
00039     QTextStream out(&outFile);
00040 
00041     QString s;
00042     while ( !in.eof() ) {        // until end of file...
00043         s = in.readLine();       // line of text excluding '\n'
00044 //      odebug << "Read: " << s.ascii() << "" << oendl;
00045         if (s.find(location,0,true) >= 0){
00046 //          odebug << "Found!" << oendl;
00047             s = s.replace(location,"");
00048         }
00049 //      odebug << "Read after: " << s.ascii() << "" << oendl;
00050 
00051         // for s, do link/mkdir.
00052         if ( s.right(1) == "/" ) {
00053 //      odebug << "do mkdir for " << s.ascii() << "" << oendl;
00054         mkdir( s.ascii(), 0777 );
00055         //possible optimization: symlink directories
00056         //that don't exist already. -- Risky.
00057         } else {
00058 //      odebug << "do symlink for " << s.ascii() << "" << oendl;
00059         QFileInfo ffi( s );
00060         //Don't try to symlink if a regular file exists already
00061         if ( !ffi.exists() || ffi.isSymLink() ) {
00062             if (symlink( (location+s).ascii(), s.ascii() ) != 0){
00063             if (errno == ENOENT){
00064 //          perror("Symlink Failed! ");
00065             QString e=s.ascii();
00066             e = e.replace(ffi.fileName(),"");
00067 //          odebug << "DirName : " << e.ascii() << "" << oendl;
00068             system ( QString("mkdir -p ")+e.ascii() );
00069                 if (symlink( (location+s).ascii(), s.ascii() ) != 0)
00070                 odebug << "Big problem creating symlink and directory" << oendl;
00071             }
00072             }
00073 //          odebug << "Created << s.ascii() << oendl;
00074             out << s << "\n";
00075         } else {
00076             odebug << "" << s.ascii() << "  exists already, not symlinked" << oendl;
00077         }
00078         }
00079     }
00080     inFile.close();
00081     outFile.close();
00082     }
00083 }
00084 
00085 
00086 
00087 static void removeSymlinks( const QString &package )
00088 {
00089     QFile inFile( listDir + package + ".list" );
00090 
00091     if ( inFile.open(IO_ReadOnly) ) {
00092     QTextStream in(&inFile);
00093 
00094     QString s;
00095     while ( !in.eof() ) {        // until end of file...
00096         s = in.readLine();       // line of text excluding '\n'
00097 //      odebug << "remove symlink " << s.ascii() << "" << oendl;
00098         QFileInfo ffi( s );
00099         //Confirm that it's still a symlink.
00100         if ( ffi.isSymLink() ){
00101         unlink( s.ascii() );
00102 //          odebug << "Removed " << s.ascii() << oendl; }
00103 //      else
00104 //      odebug << "Not removed " << s.ascii() << "" << oendl;
00105         }
00106     }
00107     inFile.close();
00108     inFile.remove();
00109     }
00110 }
00111 
00112 
00113 
00114 /*
00115   Slightly hacky: we can't use StorageInfo, since we don't have a
00116   QApplication. We look for filesystems that have the directory
00117   /usr/lib/ipkg/info, and assume that they are removable media
00118   with packages installed. This is safe even if eg. /usr is on a
00119   separate filesystem, since then we would be testing for
00120   /usr/usr/lib/ipkg/info, which should not exist. (And if it
00121   does they deserve to have it treated as removable.)
00122  */
00123 
00124 static void updateSymlinks()
00125 {
00126     QDir lists( listDir );
00127     QStringList knownPackages = lists.entryList( "*.list" ); // No tr
00128 
00129     struct mntent *me;
00130     FILE *mntfp = setmntent( "/etc/mtab", "r" );
00131 
00132     if ( mntfp ) {
00133     while ( (me = getmntent( mntfp )) != 0 ) {
00134         QString root = me->mnt_dir;
00135         if ( root == "/" )
00136         continue;
00137 
00138         QString info = root + "/usr/lib/ipkg/info";
00139         QDir infoDir( info );
00140 //      odebug << "looking at " << info.ascii() << "" << oendl;
00141         if ( infoDir.isReadable() ) {
00142         const QFileInfoList *packages = infoDir.entryInfoList( "*.list" ); // No tr
00143         QFileInfoListIterator it( *packages );
00144         QFileInfo *fi;
00145         while (( fi = *it )) {
00146             ++it;
00147             if ( knownPackages.contains( fi->fileName() ) ) {
00148 //          odebug << "found " << fi->fileName() << " and we've seen it before" << oendl;
00149             knownPackages.remove( fi->fileName() );
00150             } else {
00151             //it's a new one
00152             createSymlinks( root, fi->baseName() );
00153             }
00154 
00155         }
00156 
00157         }
00158     }
00159     endmntent( mntfp );
00160     }
00161 
00162     for ( QStringList::Iterator it = knownPackages.begin();
00163       it != knownPackages.end(); ++it ) {
00164     // strip ".info" off the end.
00165     removeSymlinks( (*it).left((*it).length()-5) );
00166     }
00167 }
00168 
00169 
00170 
00171 int main( int argc, char *argv[] )
00172 {
00173     QApplication a( argc, argv, QApplication::Tty );
00174 
00175     QString command = argc > 1 ? argv[1] : "update"; // No tr
00176 
00177     if ( command == "update" ) // No tr
00178     updateSymlinks();
00179     else if ( command == "create" && argc > 3 ) // No tr
00180     createSymlinks( argv[2], argv[3] );
00181     else if ( command == "remove"  && argc > 2 ) // No tr
00182     removeSymlinks( argv[2] );
00183     else
00184     owarn << "Argument error" << oendl;
00185 }

Generated on Sat Nov 5 16:15:25 2005 for OPIE by  doxygen 1.4.2