summaryrefslogtreecommitdiff
path: root/noncore/settings/aqpkg/ipkg.cpp
blob: 6d0edad164662eb2181c4444916ed8be56987d59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/***************************************************************************
                          ipkg.cpp  -  description
                             -------------------
    begin                : Sat Aug 31 2002
    copyright            : (C) 2002 by Andy Qua
    email                : andy.qua@blueyonder.co.uk
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <fstream>
using namespace std;

#include <stdio.h>
#include <unistd.h>

#ifdef QWS
#include <qpe/qpeapplication.h>
#else
#include <qapplication.h>
#endif
#include <qdir.h>
#include <qtextstream.h>

#include "utils.h"
#include "ipkg.h"
#include "global.h"

Ipkg :: Ipkg()
{
}

Ipkg :: ~Ipkg()
{
}

// Option is what we are going to do - install, upgrade, download
// package is the package name to install - either a fully qualified path and ipk
//   file (if stored locally) or just the name of the package (for a network package)
// packageName is the package name - (for a network package this will be the same as
//    package parameter)
// dest is the destination alias (from ipk.conf)
// destDir is the dir that the destination alias points to (used to link to root)
// flags is the ipkg options flags
// dir is the directory to run ipkg in (defaults to "")
bool Ipkg :: runIpkg( )
{
    bool ret = false;

    QDir::setCurrent( "/tmp" );
    QString cmd = "";

    if ( runtimeDir != "" )
    {
        cmd += "cd ";
        cmd += runtimeDir;
        cmd += " ; ";
    }
    cmd += "ipkg";

    if ( option != "update" && option != "download" && option != "upgrade" )
    {
        cmd += " -dest "+ destination;
        cmd += " -force-defaults";

        if ( flags & FORCE_DEPENDS )
            cmd += " -force-depends";
        if ( flags & FORCE_REINSTALL  )
            cmd += " -force-reinstall";
        if ( flags & FORCE_REMOVE )
            cmd += " -force-removal-of-essential-packages";
        if ( flags & FORCE_OVERWRITE )
            cmd += " -force-overwrite";

        // Handle make links
        // Rules - If make links is switched on, create links to root
        // if destDir is NOT /
        if ( flags & MAKE_LINKS )
        {
            // If destDir == / turn off make links as package is being insalled
            // to root already.
            if ( destDir == "/" )
                flags ^= MAKE_LINKS;
        }

    }
    
#ifdef X86
    cmd += " -f ";
    cmd += IPKG_CONF;
#endif

    cmd += " " + option;
    if ( option != "upgrade" )
        cmd += " " + package;
    cmd += " 2>&1";

    qApp->processEvents();

    // If we are removing packages and make links option is selected
    // create the links
    if ( option == "remove" )
    {
        createLinks = false;
        if ( flags & MAKE_LINKS )
        {
            emit outputText( QString( "Removing symbolic links...\n" ) );
            linkPackage( Utils::getPackageNameFromIpkFilename( package ), destination, destDir );
            emit outputText( QString( " " ) );
        }
    }
    
    emit outputText( cmd );

    // Execute command
    dependantPackages = new QList<QString>;
    dependantPackages->setAutoDelete( true );

    ret = executeIpkgCommand( cmd, option );

    if ( option == "install" )
    {
        // If we are not removing packages and make links option is selected
        // create the links
        createLinks = true;
        if ( flags & MAKE_LINKS )
        {
            emit outputText( " " );
            emit outputText( QString( "Creating symbolic links for " )+ package );
            
            linkPackage( Utils::getPackageNameFromIpkFilename( package ), destination, destDir );

            // link dependant packages that were installed with this release
            QString *pkg;
            for ( pkg = dependantPackages->first(); pkg != 0; pkg = dependantPackages->next() )
            {
                if ( *pkg == package )
                    continue;
                emit outputText( " " );
                emit outputText( QString( "Creating symbolic links for " )+ (*pkg) );
                linkPackage( Utils::getPackageNameFromIpkFilename( *pkg ), destination, destDir );
            }
        }
    }

    delete dependantPackages;
    
    emit outputText( QString( "Finished - status=" ) + (ret ? "success" : "failure") );
    return ret;
}


int Ipkg :: executeIpkgCommand( QString &cmd, const QString option )
{
    FILE *fp = NULL;
    char line[130];
    QString lineStr, lineStrOld;
    int ret = false;

    fp = popen( (const char *) cmd, "r");
    if ( fp == NULL )
    {
        cout << "Couldn't execute " << cmd << "! err = " << fp << endl;
        QString text;
        text.sprintf( "Couldn't execute %s! See stdout for error code", (const char *)cmd );
        emit outputText( text );
    }
    else
    {
        while ( fgets( line, sizeof line, fp) != NULL )
        {
            lineStr = line;
            lineStr=lineStr.left( lineStr.length()-1 );

            if ( lineStr != lineStrOld )
            {
                //See if we're finished
                if ( option == "install" )
                {
                    // Need to keep track of any dependant packages that get installed
                    // so that we can create links to them as necessary
                    if ( lineStr.startsWith( "Installing " ) )
                    {
                        cout << "LineStr = " << lineStr << endl;
                        int start = lineStr.find( " " ) + 1;
                        int end = lineStr.find( " ", start );
                        QString *package = new QString( lineStr.mid( start, end-start ) );
                        dependantPackages->append( package );
                        cout << "installing dependant package <" << *package << ">" << endl;
                    }
                }

                if ( option == "update" )
                {
                    if (lineStr.contains("Updated list"))
                        ret = true;
                }
                else if ( option == "download" )
                {
                    if (lineStr.contains("Downloaded"))
                        ret = true;
                }
                else
                {
                    if (lineStr.contains("Done"))
                        ret = true;
                }

                emit outputText( lineStr );
            }
            lineStrOld = lineStr;
            qApp->processEvents();
        }
        pclose(fp);
    }

    return ret;
}


void Ipkg :: linkPackage( const QString &packFileName, const QString &dest, const QString &destDir )
{
    if ( dest == "root" || dest == "/" )
        return;
        
    qApp->processEvents();
    QStringList *fileList = getList( packFileName, destDir );
    qApp->processEvents();
    processFileList( fileList, destDir );
    delete fileList;
}

QStringList* Ipkg :: getList( const QString &packageFilename, const QString &destDir )
{
    QString packageFileDir = destDir+"/usr/lib/ipkg/info/"+packageFilename+".list";
    QFile f( packageFileDir );

    cout << "Try to open " << packageFileDir.latin1() << endl;
    if ( !f.open(IO_ReadOnly) )
    {
        // Couldn't open from dest, try from /
//        cout << "Could not open:" << packageFileDir << endl;
        f.close();
        
        packageFileDir = "/usr/lib/ipkg/info/"+packageFilename+".list";
        f.setName( packageFileDir );
//        cout << "Try to open " << packageFileDir.latin1() << endl;
        if ( ! f.open(IO_ReadOnly) )
        {
            cout << "Could not open:" << packageFileDir << endl;
            emit outputText( QString( "Could not open :" ) + packageFileDir );
            return (QStringList*)0;
        }
    }
    QStringList *fileList = new QStringList();
    QTextStream t( &f );
    while ( !t.eof() )
        *fileList += t.readLine();

    f.close();
    return fileList;
}

void Ipkg :: processFileList( const QStringList *fileList, const QString &destDir )
{
    if ( !fileList || fileList->isEmpty() )
        return;

    QString baseDir = ROOT;

    if ( createLinks == true )
    {
        for ( uint i=0; i < fileList->count(); i++ )
        {
            processLinkDir( (*fileList)[i], baseDir, destDir );
            qApp->processEvents();
        }
    }
    else
    {
        for ( int i = fileList->count()-1; i >= 0 ; i-- )
        {
            processLinkDir( (*fileList)[i], baseDir, destDir );
            qApp->processEvents();
        }
    }
}

void Ipkg :: processLinkDir( const QString &file, const QString &destDir, const QString &baseDir )
{

    QString sourceFile = baseDir + file;
    
    QString linkFile = destDir;
    if ( file.startsWith( "/" ) && destDir.right( 1 ) == "/" )
    {
        linkFile += file.mid( 1 );
    }   
    else
    {
        linkFile += file;
    }
    QString text;
    if ( createLinks )
    {
        // If this file is a directory (ends with a /) and it doesn't exist,
        // we need to create it
        if ( file.right(1) == "/" )
        {
            QFileInfo f( linkFile );
            if ( !f.exists() )
            {
                emit outputText( QString( "Creating directory " ) + linkFile );
                QDir d;
                d.mkdir( linkFile, true );
            }
            else
                emit outputText( QString( "Directory " ) + linkFile + " exists" );
            
        }
        else
        {
            int rc = symlink( sourceFile, linkFile );
            text = (rc == 0 ? "Linked " : "Failed to link ");
            text += sourceFile + " to " + linkFile;
            emit outputText( text );
        }
    }
    else
    {
        QFileInfo f( linkFile );
        if ( f.exists() )
        {
            if ( f.isFile() )
            {
                QFile f( linkFile );
                bool rc = f.remove();

                text = (rc ? "Removed " : "Failed to remove ");
                text += linkFile;
                emit outputText( text );
            }
            else if ( f.isDir() )
            {
                QDir d;
                bool rc = d.rmdir( linkFile, true );
                text = (rc ? "Removed " : "Failed to remove ");
                text += linkFile;
                emit outputText( text );
            }
        }
    }
    
}