summaryrefslogtreecommitdiff
path: root/core/launcher/packageslave.cpp
blob: abbc610ea8324388e19cb3c1f363ebc8e410cd73 (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
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include "packageslave.h"

/* OPIE */
#include <opie2/odebug.h>
#include <qtopia/qprocess.h>
#ifdef Q_WS_QWS
#include <qtopia/qcopenvelope_qws.h>
#endif
using namespace Opie::Core;

/* QT */
#ifdef Q_WS_QWS
#include <qcopchannel_qws.h>
#endif
#include <qtextstream.h>

/* STD */
#include <stdlib.h>
#include <sys/stat.h> // mkdir()
#if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
#include <unistd.h>
#include <sys/vfs.h>
#include <mntent.h>
#elif defined(Q_OS_MACX)
#include <unistd.h>
#endif


PackageHandler::PackageHandler( QObject *parent, char* name )
    : QObject( parent, name ), packageChannel( 0 ), currentProcess( 0 ), mNoSpaceLeft( FALSE )
{
    // setup qcop channel
#ifndef QT_NO_COP
    packageChannel = new QCopChannel( "QPE/Package", this );
    connect( packageChannel, SIGNAL( received(const QCString&,const QByteArray&) ),
         this, SLOT( qcopMessage(const QCString&,const QByteArray&) ) );
#endif
}

void PackageHandler::qcopMessage( const QCString &msg, const QByteArray &data )
{
    QDataStream stream( data, IO_ReadOnly );

    if ( msg == "installPackage(QString)" ) {
        QString file;
        stream >> file;
        installPackage( file );
    } else if ( msg == "installPackage(QString,QString)" ) {
        QString file, dest;
        stream >> file >> dest;
        installPackage( file, dest );
    } else if ( msg == "removePackage(QString)" ) {
    QString file;
        stream >> file;
        removePackage( file );
    } else if ( msg == "addPackageFiles(QString,QString)" ) {
    QString location, listfile;
        stream >> location >> listfile;
        addPackageFiles( location, listfile);
    } else if ( msg == "addPackages(QString)" ) {
    QString location;
        stream >> location;
        addPackages( location );
    } else if ( msg == "cleanupPackageFiles(QString)" ) {
    QString listfile;
        stream >> listfile;
    cleanupPackageFiles( listfile );
    } else if ( msg == "cleanupPackages(QString)" ) {
    QString location;
        stream >> location;
        cleanupPackages( location );
    } else if ( msg == "prepareInstall(QString,QString)" ) {
    QString size, path;
    stream >> size;
    stream >> path;
    prepareInstall( size, path );
    }
}

void PackageHandler::installPackage( const QString &package, const QString &dest )
{
    if ( mNoSpaceLeft ) {
    mNoSpaceLeft = FALSE;
    // Don't emit that for now, I still couldn't test it (Wener)
    //sendReply( "installFailed(QString)", package );
    //return;
    }

    QStringList cmd;
    cmd << "ipkg";
    if ( !dest.isEmpty() ) {
	cmd << "-d" << dest;
    }
    cmd << "install" << package;
    currentProcess = new QProcess( cmd ); // No tr
    connect( currentProcess, SIGNAL( processExited() ), SLOT( iProcessExited() ) );
    connect( currentProcess, SIGNAL( readyReadStdout() ), SLOT( readyReadStdout() ) );
    connect( currentProcess, SIGNAL( readyReadStderr() ), SLOT( readyReadStderr() ) );
    currentPackage = package;

    currentProcessError="";
    sendReply( "installStarted(QString)", package );
    currentProcess->start();
}

void PackageHandler::removePackage( const QString &package )
{
    currentProcess = new QProcess( QStringList() << "ipkg" << "remove" << package ); // No tr
    connect( currentProcess, SIGNAL( processExited() ), SLOT( rmProcessExited() ) );
    connect( currentProcess, SIGNAL( readyReadStdout() ), SLOT( readyReadStdout() ) );
    connect( currentProcess, SIGNAL( readyReadStderr() ), SLOT( readyReadStderr() ) );
    currentPackage = package;

    currentProcessError="";
    sendReply( "removeStarted(QString)", package );
    currentProcess->start();
}

void PackageHandler::sendReply( const QCString& msg, const QString& arg )
{
#ifndef QT_NO_COP
    QCopEnvelope e( "QPE/Desktop", msg );
    e << arg;
#endif
}

void PackageHandler::addPackageFiles( const QString &location,
                      const QString &listfile )
{
    QFile f(listfile);
#ifndef Q_OS_WIN32
    //make a copy so we can remove the symlinks later
    mkdir( ("/usr/lib/ipkg/info/"+location).ascii(), 0777 );
    system(("cp " + f.name() + " /usr/lib/ipkg/info/"+location).ascii());
#else
    QDir d;
    //#### revise
    odebug << "Copy file at " << __FILE__ << ": " << __LINE__ << "" << oendl;
    d.mkdir(("/usr/lib/ipkg/info/" + location).ascii());
    system(("copy " + f.name() + " /usr/lib/ipkg/info/"+location).ascii());
#endif


    if ( f.open(IO_ReadOnly) ) {
    QTextStream ts(&f);

    QString s;
    while ( !ts.eof() ) {        // until end of file...
        s = ts.readLine();       // line of text excluding '\n'
        // for s, do link/mkdir.
        if ( s.right(1) == "/" ) {
        odebug << "do mkdir for " << s.ascii() << "" << oendl;
#ifndef Q_OS_WIN32
        mkdir( s.ascii(), 0777 );
        //possible optimization: symlink directories
        //that don't exist already. -- Risky.
#else
        d.mkdir( s.ascii());
#endif

        } else {
#ifndef Q_OS_WIN32
        odebug << "do symlink for " << s.ascii() << "" << oendl;
        symlink( (location + s).ascii(), s.ascii() );
#else
        odebug << "Copy file instead of a symlink for WIN32" << oendl;
        if (!CopyFile((TCHAR*)qt_winTchar((location + s), TRUE), (TCHAR*)qt_winTchar(s, TRUE), FALSE))
          owarn << "Unable to create symlinkfor " << (location + s).ascii() << oendl;
#endif
        }
    }
    f.close();
    }
}

void PackageHandler::addPackages( const QString &location )
{
    // get list of *.list in location/usr/lib/ipkg/info/*.list
    QDir dir(location + "/usr/lib/ipkg/info", "*.list",  // No tr
         QDir::Name, QDir::Files);
    if ( !dir.exists() )
    return;

    QStringList packages = dir.entryList();
    for ( QStringList::Iterator it = packages.begin();
      it != packages.end(); ++it ) {
    addPackageFiles( location, *it );
    }
}


void PackageHandler::cleanupPackageFiles( const QString &listfile  )
{
    QFile f(listfile);

    if ( f.open(IO_ReadOnly) ) {
    QTextStream ts(&f);

    QString s;
    while ( !ts.eof() ) {        // until end of file...
        s = ts.readLine();       // line of text excluding '\n'
        // for s, do link/mkdir.
        if ( s.right(1) == "/" ) {
        //should rmdir if empty, after all files have been removed
        } else {
#ifndef Q_OS_WIN32
        odebug << "remove symlink for " << s.ascii() << "" << oendl;
        //check if it is a symlink first (don't remove /etc/passwd...)
        char buf[10]; //we don't care about the contents
        if ( ::readlink( s.ascii(),buf, 10 >= 0 ) )
             ::unlink( s.ascii() );
#else
        // ### revise
        owarn << "Unable to remove symlink " << __FILE__ << ":" << __LINE__ << "" << oendl;
#endif
        }
    }
    f.close();

        //remove the list file
    ::unlink( listfile.ascii() );

    }
}

void PackageHandler::cleanupPackages( const QString &location )
{
    // get list of *.list in location/usr/lib/ipkg/info/*.list
    QDir dir( "/usr/lib/ipkg/info/"+location, "*.list",  // No tr
          QDir::Name, QDir::Files);
    if ( !dir.exists() )
    return;

    QStringList packages = dir.entryList();
    for ( QStringList::Iterator it = packages.begin();
      it != packages.end(); ++it ) {
    cleanupPackageFiles( *it );
    }

    //remove the backup directory
    //###
}

void PackageHandler::prepareInstall( const QString& size, const QString& path )
{
    // Check whether there will be enough space to install the next package.
    bool ok;
    unsigned int s = size.toUInt( &ok );

    if ( !ok )
    return;

    // Shamelessly stolen from the sysinfo application (Werner)
#if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
    struct statfs fs;
    if ( statfs( path.latin1(), &fs ) == 0 )
    if ( s > fs.f_bsize * fs.f_bavail ) {
        //odebug << "############### Not enough space left ###############" << oendl;
        mNoSpaceLeft = TRUE;
    }
#endif
}

void PackageHandler::iProcessExited()
{
    if ( currentProcess->normalExit() && currentProcess->exitStatus() == 0 )
    sendReply( "installDone(QString)", currentPackage );
    else {
#ifndef QT_NO_COP
    QCopEnvelope e( "QPE/Desktop", "installFailed(QString,int,QString)" );
    e << currentPackage << currentProcess->exitStatus()
        << currentProcessError;
#endif
    }

    delete currentProcess;
    currentProcess = 0;

#ifndef QT_NO_COP
    QCopEnvelope e("QPE/System", "linkChanged(QString)");
    QString lf = QString::null;
    e << lf;
#endif
    unlink( currentPackage );
}

void PackageHandler::rmProcessExited()
{
    if ( currentProcess->normalExit() && currentProcess->exitStatus() == 0 )
    sendReply( "removeDone(QString)", currentPackage );
    else
    sendReply( "removeFailed(QString)", currentPackage );

#ifndef QT_NO_COP
    QCopEnvelope e("QPE/System", "linkChanged(QString)");
    QString lf = QString::null;
    e << lf;
#endif
}

void PackageHandler::readyReadStdout()
{
    while ( currentProcess->canReadLineStdout() ) {
    QString line = currentProcess->readLineStdout();
    currentProcessError.append("OUT:"+line);
    if ( line.contains( "Unpacking" ) ) // No tr
        sendReply( "installStep(QString)", "one" ); // No tr
    else if ( line.contains( "Configuring" ) ) // No tr
        sendReply( "installStep(QString)", "two" ); // No tr
    }
}

void PackageHandler::readyReadStderr()
{
    while ( currentProcess->canReadLineStderr() ) {
    QString line = currentProcess->readLineStderr();
    currentProcessError.append("ERR:"+line);
    }
}

void PackageHandler::redoPackages()
{
    //get list of filesystems

    //call cleanupPackages for the ones that have disappeared

    //call addPackageFiles for the new ones
}