-rw-r--r-- | core/symlinker/main.cpp | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/core/symlinker/main.cpp b/core/symlinker/main.cpp new file mode 100644 index 0000000..73d5166 --- a/dev/null +++ b/core/symlinker/main.cpp | |||
@@ -0,0 +1,160 @@ | |||
1 | #include <qapplication.h> | ||
2 | #include <qfile.h> | ||
3 | #include <qfileinfo.h> | ||
4 | #include <qdir.h> | ||
5 | #include <qtextstream.h> | ||
6 | #include <qstringlist.h> | ||
7 | |||
8 | #include <stdlib.h> | ||
9 | #include <unistd.h> //symlink() | ||
10 | #include <sys/stat.h> // mkdir() | ||
11 | |||
12 | #include <sys/vfs.h> | ||
13 | #include <mntent.h> | ||
14 | |||
15 | static const char *listDir = "/usr/lib/ipkg/externinfo/"; | ||
16 | |||
17 | static void createSymlinks( const QString &location, const QString &package ) | ||
18 | { | ||
19 | QFile inFile( location + "/usr/lib/ipkg/info/" + package + ".list" ); | ||
20 | mkdir( "/usr/lib/ipkg", 0777 ); | ||
21 | mkdir( listDir, 0777 ); | ||
22 | |||
23 | QFile outFile( listDir + package + ".list"); | ||
24 | |||
25 | //qDebug( "createSymlinks %s -> %s", inFile.name().ascii(), outFile.name().ascii() ); | ||
26 | |||
27 | |||
28 | |||
29 | if ( inFile.open(IO_ReadOnly) && outFile.open(IO_WriteOnly)) { | ||
30 | QTextStream in(&inFile); | ||
31 | QTextStream out(&outFile); | ||
32 | |||
33 | QString s; | ||
34 | while ( !in.eof() ) { // until end of file... | ||
35 | s = in.readLine(); // line of text excluding '\n' | ||
36 | //qDebug( "Read: %s", s.ascii() ); | ||
37 | // for s, do link/mkdir. | ||
38 | if ( s.right(1) == "/" ) { | ||
39 | //qDebug("do mkdir for %s", s.ascii()); | ||
40 | mkdir( s.ascii(), 0777 ); | ||
41 | //possible optimization: symlink directories | ||
42 | //that don't exist already. -- Risky. | ||
43 | } else { | ||
44 | //qDebug("do symlink for %s", s.ascii()); | ||
45 | QFileInfo ffi( s ); | ||
46 | //Don't try to symlink if a regular file exists already | ||
47 | if ( !ffi.exists() || ffi.isSymLink() ) { | ||
48 | symlink( (location+s).ascii(), s.ascii() ); | ||
49 | // qDebug ( "Created %s" ,s.ascii() ); | ||
50 | out << s << "\n"; | ||
51 | } //else { | ||
52 | // qDebug( "%s exists already, not symlinked", s.ascii() ); | ||
53 | // } | ||
54 | } | ||
55 | } | ||
56 | inFile.close(); | ||
57 | outFile.close(); | ||
58 | } | ||
59 | } | ||
60 | |||
61 | |||
62 | |||
63 | static void removeSymlinks( const QString &package ) | ||
64 | { | ||
65 | QFile inFile( listDir + package + ".list" ); | ||
66 | |||
67 | if ( inFile.open(IO_ReadOnly) ) { | ||
68 | QTextStream in(&inFile); | ||
69 | |||
70 | QString s; | ||
71 | while ( !in.eof() ) { // until end of file... | ||
72 | s = in.readLine(); // line of text excluding '\n' | ||
73 | //qDebug("remove symlink %s", s.ascii()); | ||
74 | QFileInfo ffi( s ); | ||
75 | //Confirm that it's still a symlink. | ||
76 | if ( ffi.isSymLink() ) | ||
77 | unlink( s.ascii() ); | ||
78 | // qDebug ( "Removed %s", s.ascii() ); | ||
79 | // else | ||
80 | // qDebug( "Not removed %s", s.ascii() ); | ||
81 | } | ||
82 | inFile.close(); | ||
83 | inFile.remove(); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | |||
88 | |||
89 | /* | ||
90 | Slightly hacky: we can't use StorageInfo, since we don't have a | ||
91 | QApplication. We look for filesystems that have the directory | ||
92 | /usr/lib/ipkg/info, and assume that they are removable media | ||
93 | with packages installed. This is safe even if eg. /usr is on a | ||
94 | separate filesystem, since then we would be testing for | ||
95 | /usr/usr/lib/ipkg/info, which should not exist. (And if it | ||
96 | does they deserve to have it treated as removable.) | ||
97 | */ | ||
98 | |||
99 | static void updateSymlinks() | ||
100 | { | ||
101 | QDir lists( listDir ); | ||
102 | QStringList knownPackages = lists.entryList( "*.list" ); // No tr | ||
103 | |||
104 | struct mntent *me; | ||
105 | FILE *mntfp = setmntent( "/etc/mtab", "r" ); | ||
106 | |||
107 | if ( mntfp ) { | ||
108 | while ( (me = getmntent( mntfp )) != 0 ) { | ||
109 | QString root = me->mnt_dir; | ||
110 | if ( root == "/" ) | ||
111 | continue; | ||
112 | |||
113 | QString info = root + "/usr/lib/ipkg/info"; | ||
114 | QDir infoDir( info ); | ||
115 | //qDebug( "looking at %s", info.ascii() ); | ||
116 | if ( infoDir.isReadable() ) { | ||
117 | const QFileInfoList *packages = infoDir.entryInfoList( "*.list" ); // No tr | ||
118 | QFileInfoListIterator it( *packages ); | ||
119 | QFileInfo *fi; | ||
120 | while (( fi = *it )) { | ||
121 | ++it; | ||
122 | if ( knownPackages.contains( fi->fileName() ) ) { | ||
123 | //qDebug( "found %s and we've seen it before", fi->fileName().latin1() ); | ||
124 | knownPackages.remove( fi->fileName() ); | ||
125 | } else { | ||
126 | //it's a new one | ||
127 | createSymlinks( root, fi->baseName() ); | ||
128 | } | ||
129 | |||
130 | } | ||
131 | |||
132 | } | ||
133 | } | ||
134 | endmntent( mntfp ); | ||
135 | } | ||
136 | |||
137 | for ( QStringList::Iterator it = knownPackages.begin(); | ||
138 | it != knownPackages.end(); ++it ) { | ||
139 | // strip ".info" off the end. | ||
140 | removeSymlinks( (*it).left((*it).length()-5) ); | ||
141 | } | ||
142 | } | ||
143 | |||
144 | |||
145 | |||
146 | int main( int argc, char *argv[] ) | ||
147 | { | ||
148 | QApplication a( argc, argv, QApplication::Tty ); | ||
149 | |||
150 | QString command = argc > 1 ? argv[1] : "update"; // No tr | ||
151 | |||
152 | if ( command == "update" ) // No tr | ||
153 | updateSymlinks(); | ||
154 | else if ( command == "create" && argc > 3 ) // No tr | ||
155 | createSymlinks( argv[2], argv[3] ); | ||
156 | else if ( command == "remove" && argc > 2 ) // No tr | ||
157 | removeSymlinks( argv[2] ); | ||
158 | else | ||
159 | qWarning( "Argument error" ); | ||
160 | } | ||