summaryrefslogtreecommitdiff
path: root/noncore/settings/packagemanager/oipkg.cpp
blob: 38cd82401b6ab30482a1288c42b1094c8d862cda (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/*
                             This file is part of the Opie Project

                             Copyright (C)2004, 2005 Dan Williams <drw@handhelds.org>
              =.
            .=l.
           .>+-=
 _;:,     .>    :=|.         This program is free software; you can
.> <`_,   >  .   <=          redistribute it and/or  modify it under
:`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; either version 2 of the License,
     ._= =}       :          or (at your option) any later version.
    .%`+i>       _;_.
    .i_,=:_.      -<s.       This program is distributed in the hope that
     +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
    : ..    .:,     . . .    without even the implied warranty of
    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
..}^=.=       =       ;      Library General Public License for more
++=   -.     .`     .:       details.
:     =  ...= . :.=-
 -.   .:....=;==+<;          You should have received a copy of the GNU
  -_. . .   )=.  =           Library General Public License along with
    --        :-=`           this library; see the file COPYING.LIB.
                             If not, write to the Free Software Foundation,
                             Inc., 59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.
*/

#include "oipkg.h"

#include <qdir.h>
#include <qfile.h>
#include <qtextstream.h>

#include <stdlib.h>
#include <unistd.h>

extern "C" {
#include <libipkg.h>
};
args_t m_ipkgArgs; // libipkg configuration arguments

const QString IPKG_CONF        = "/etc/ipkg.conf";      // Fully-qualified name of Ipkg primary configuration file
const QString IPKG_CONF_DIR    = "/etc/ipkg";           // Directory of secondary Ipkg configuration files
const QString IPKG_PKG_PATH    = "/usr/lib/ipkg/lists"; // Directory containing server package lists
const QString IPKG_STATUS_PATH = "usr/lib/ipkg/status"; // Destination status file location
const QString IPKG_INFO_PATH   = "usr/lib/ipkg/info";   // Package file lists location

OIpkg *oipkg;

// Ipkg callback functions

int fsignalIpkgMessage( ipkg_conf_t *conf, message_level_t level, char *msg )
{
    // Display message only if it is below the message level threshold
    if ( conf && ( conf->verbosity < level ) )
        return 0;
    else
        oipkg->ipkgMessage( msg );

    return 0;
}

char *fIpkgResponse( char */*question*/ )
{
    return 0l;
}

int fIpkgStatus( char */*name*/, int /*status*/, char *desc, void */*userdata*/ )
{
    oipkg->ipkgStatus( desc );
    return 0;
}

int fIpkgFiles( char */*name*/, char *desc, char */*version*/, pkg_state_status_t /*status*/,
                void */*userdata*/ )
{
    oipkg->ipkgList( desc );
    return 0;
}

OIpkg::OIpkg( Config *config, QObject *parent, const char *name )
    : QObject( parent, name )
    , m_config( config )
    , m_confInfo( NULL )
    , m_ipkgExecOptions( 0 )
    , m_ipkgExecVerbosity( 1 )
{
    // Keep pointer to self for the Ipkg callback functions
    oipkg = this;

    // Initialize libipkg
    ipkg_init( &fsignalIpkgMessage, &fIpkgResponse, &m_ipkgArgs );

    // Default ipkg run-time arguments
    m_ipkgArgs.noaction = false;
    m_ipkgArgs.force_defaults = true;
}

OIpkg::~OIpkg()
{
    // Upon destruction, ensure that items in config list are deleted with list
    if ( m_confInfo )
        m_confInfo->setAutoDelete( true );

    // Free up libipkg resources
    ipkg_deinit( &m_ipkgArgs );
}

OConfItemList *OIpkg::configItems()
{
    // Retrieve all configuration items
    return filterConfItems();
}

OConfItemList *OIpkg::servers()
{
    // Retrieve only servers
    return filterConfItems( OConfItem::Source );
}

OConfItemList *OIpkg::destinations()
{
    // Retrieve only destinations
    return filterConfItems( OConfItem::Destination );
}

OConfItemList *OIpkg::options()
{
    // Retrieve only destinations
    return filterConfItems( OConfItem::Option );
}

void OIpkg::setConfigItems( OConfItemList *configList )
{
    if ( m_confInfo )
        delete m_confInfo;

    m_confInfo = configList;

    // Write out new /etc/ipkg.conf
    QFile confFile( IPKG_CONF );
    if ( confFile.open( IO_WriteOnly ) )
    {
        QTextStream confStream( &confFile );
        confStream << "# Generated by Opie Package Manager\n\n";

        OConfItemListIterator it( *m_confInfo );
        for ( ; it.current(); ++it )
        {
            OConfItem *item = it.current();

            // Only write out valid conf items
            if ( item->type() != OConfItem::NotDefined )
            {
                QString confLine;
                QString name = item->name();
                if ( !item->active() )
                    confLine = "#";

                switch ( item->type() )
                {
                    case OConfItem::Source :
                    {
                        if ( item->features().contains( "Compressed" ) )
                            confLine.append( "src/gz" );
                        else
                            confLine.append( "src" );
                    }
                    break;
                    case OConfItem::Destination : confLine.append( "dest" ); break;
                    case OConfItem::Option : confLine.append( "option" ); break;
                    case OConfItem::Arch : confLine.append( "arch" ); break;
                    case OConfItem::Other :
                    {
                        // For options w/type = Other, the mapping is as follows:
                        //    name = typeStr (e.g. "lists_dir")
                        //    value = value
                        //    features = name (from configuration file)
                        confLine.append( item->name() );
                        name = item->features();
                    }
                    break;
                    default : break;
                };

                confStream << confLine << " " << name << " " << item->value() << "\n";
            }
        }

        confFile.close();
    }
    else
    {
        // Problem writing to /etc/ipkg.conf, exit before removing other conf files
        return;
    }

    // Delete /etc/ipkg/*.conf files (/etc/ipkg.conf should now have all settings
    QStringList confFiles;
    QDir confDir( IPKG_CONF_DIR );
    if ( confDir.exists() )
    {
        confDir.setNameFilter( "*.conf" );
        confDir.setFilter( QDir::Files );
        confFiles = confDir.entryList( "*.conf", QDir::Files );

        QStringList::Iterator lastFile = confFiles.end();
        for ( QStringList::Iterator it = confFiles.begin(); it != lastFile; ++it )
        {
            // Create absolute file path if necessary
            QString absFile = (*it);
            if ( !absFile.startsWith( "/" ) )
                absFile.prepend( QString( IPKG_CONF_DIR ) + "/" );

            // Delete file
            QFile::remove( absFile );
        }
    }

    // Reinitialize libipkg to pick up new configuration
    ipkg_deinit( &m_ipkgArgs );
    ipkg_init( &fsignalIpkgMessage, &fIpkgResponse, &m_ipkgArgs );
    m_ipkgArgs.noaction = false;
    m_ipkgArgs.force_defaults = true;
}

void OIpkg::saveSettings()
{
    // Save Ipkg execution options to application configuration file
    if ( m_config )
    {
        m_config->setGroup( "Ipkg" );
        m_config->writeEntry( "ExecOptions", m_ipkgExecOptions );
        m_config->writeEntry( "Verbosity", m_ipkgExecVerbosity );
    }
}

OPackageList *OIpkg::availablePackages( const QString &server )
{
    // Load Ipkg configuration info if not already cached
    if ( !m_confInfo )
        loadConfiguration();

    // Build new server list (caller is responsible for deleting)
    OPackageList *pl = new OPackageList;

    // Get directory where server lists are located
    QString listsDir;
    OConfItem *confItem = findConfItem( OConfItem::Other, "lists_dir" );
    if ( confItem )
        listsDir = confItem->value();
    else
        listsDir = IPKG_PKG_PATH;

    // Open package list file
    QFile f( listsDir + "/" + server );
    if ( !f.open( IO_ReadOnly ) )
        return NULL;
    QTextStream t( &f );

    // Process all information in package list file
    OPackage *package = NULL;
    QString line = t.readLine();
    while ( !t.eof() )
    {
        // Determine key/value pair
        int pos = line.find( ':', 0 );
        QString key;
        if ( pos > -1 )
            key = line.mid( 0, pos );
        else
            key = QString::null;
        QString value = line.mid( pos+2, line.length()-pos );

        // Allocate new package and insert into list
        if ( package == NULL  && !key.isEmpty() )
        {
            package = new OPackage( value );
            package->setSource( server );
            pl->append( package );
        }

        // Update package data
        if ( key == "Package" )
                package->setName( value );
        else if ( key == "Version" )
            package->setVersion( value );
        else if ( key == "Section" )
            package->setCategory( value );
            //DataManager::setAvailableCategories( value );
        else if ( key.isEmpty() && value.isEmpty() )
            package = NULL;

        // Skip past all description lines
        if ( key == "Description" )
        {
            line = t.readLine();
            while ( !line.isEmpty() && line.find( ':', 0 ) == -1 && !t.eof() )
                line = t.readLine();
        }
        else
            line = t.readLine();
    }

    f.close();

    return pl;
}

OPackageList *OIpkg::installedPackages( const QString &destName, const QString &destPath )
{
    // Load Ipkg configuration info if not already cached
    if ( !m_confInfo )
        loadConfiguration();

    // Build new server list (caller is responsible for deleting)
    OPackageList *pl = new OPackageList;

    // Open status file
    QString path = destPath;
    if ( path.right( 1 ) != "/" )
        path.append( "/" );
    path.append( IPKG_STATUS_PATH );

    QFile f( path );
    if ( !f.open( IO_ReadOnly ) )
        return NULL;
    QTextStream t( &f );

    // Process all information in status file
    bool newPackage = false;
    QString line = t.readLine();
    QString name;
    QString version;
    QString status;

    while ( !t.eof() )
    {
        // Determine key/value pair
        int pos = line.find( ':', 0 );
        QString key;
        if ( pos > -1 )
            key = line.mid( 0, pos );
        else
            key = QString::null;
        QString value = line.mid( pos+2, line.length()-pos );

        // Allocate new package and insert into list
        if ( newPackage  && !key.isEmpty() )
        {
            // Add to list only if it has a valid name and is installed
            if ( !name.isNull() && status.contains( " installed" ) )
            {
                pl->append( new OPackage( name, QString::null, version, QString::null, destName ) );
                name = QString::null;
                version = QString::null;
                status = QString::null;

                newPackage = false;
            }
        }

        // Update package data
        if ( key == "Package" )
            name = value;
        else if ( key == "Version" )
            version = value;
        else if ( key == "Status" )
            status = value;
        else if ( key.isEmpty() && value.isEmpty() )
            newPackage = true;

        // Skip past all description lines
        if ( key == "Description" )
        {
            line = t.readLine();
            while ( !line.isEmpty() && line.find( ':', 0 ) == -1 && !t.eof() )
                line = t.readLine();
        }
        else
            line = t.readLine();
    }

    f.close();

    // Make sure to add to list last entry
    if ( !name.isNull() && status.contains( " installed" ) )
        pl->append( new OPackage( name, QString::null, version, QString::null, destName ) );

    return pl;
}

OConfItem *OIpkg::findConfItem( OConfItem::Type type, const QString &name )
{
    // Find configuration item in list
    OConfItemListIterator configIt( *m_confInfo );
    OConfItem *config = 0l;
    for ( ; configIt.current(); ++configIt )
    {
        config = configIt.current();
        if ( config->type() == type && config->name() == name )
            break;
    }

    if ( config && config->type() == type && config->name() == name )
        return config;

    return 0l;
}

bool OIpkg::executeCommand( OPackage::Command command, const QStringList &parameters, const QString &destination,
                            const QObject *receiver, const char *slotOutput, bool rawOutput )
{
    if ( command == OPackage::NotDefined )
        return false;

    // Set ipkg run-time options/arguments
    m_ipkgArgs.force_depends = ( m_ipkgExecOptions & FORCE_DEPENDS );
    m_ipkgArgs.force_reinstall = ( m_ipkgExecOptions & FORCE_REINSTALL );
    // TODO m_ipkgArgs.force_remove = ( m_ipkgExecOptions & FORCE_REMOVE );
    m_ipkgArgs.force_overwrite = ( m_ipkgExecOptions & FORCE_OVERWRITE );
    m_ipkgArgs.force_removal_of_dependent_packages = ( m_ipkgExecOptions & FORCE_RECURSIVE );
    m_ipkgArgs.verbose_wget = ( m_ipkgExecOptions & FORCE_VERBOSE_WGET );
    m_ipkgArgs.verbosity = m_ipkgExecVerbosity;
    if ( m_ipkgArgs.dest )
        free( m_ipkgArgs.dest );
    if ( !destination.isNull() )
    {
        int len = destination.length() + 1;
        m_ipkgArgs.dest = (char *)malloc( len );
        strncpy( m_ipkgArgs.dest, destination, destination.length() );
        m_ipkgArgs.dest[ len - 1 ] = '\0';
    }
    else
        m_ipkgArgs.dest = 0l;

    // Connect output signal to widget

    if ( !rawOutput )
    {
        // TODO - connect to local slot and parse output before emitting signalIpkgMessage
    }

    switch( command )
    {
        case OPackage::Update : {
                connect( this, SIGNAL(signalIpkgMessage(const QString &)), receiver, slotOutput );
                ipkg_lists_update( &m_ipkgArgs );
                disconnect( this, SIGNAL(signalIpkgMessage(const QString &)), 0, 0 );
            };
            break;
        case OPackage::Upgrade : {
                connect( this, SIGNAL(signalIpkgMessage(const QString &)), receiver, slotOutput );
                ipkg_packages_upgrade( &m_ipkgArgs );

                // Re-link non-root destinations to make sure everything is in sync
                OConfItemList *destList = destinations();
                OConfItemListIterator it( *destList );
                for ( ; it.current(); ++it )
                {
                    OConfItem *dest = it.current();
                    if ( dest->name() != "root" )
                        linkPackageDir( dest->name() );
                }
                delete destList;
                disconnect( this, SIGNAL(signalIpkgMessage(const QString &)), 0, 0 );
            };
            break;
        case OPackage::Install : {
                connect( this, SIGNAL(signalIpkgMessage(const QString &)), receiver, slotOutput );
                for ( QStringList::ConstIterator it = parameters.begin(); it != parameters.end(); ++it )
                {
                    ipkg_packages_install( &m_ipkgArgs, (*it) );
                }
                if ( destination != "root" )
                    linkPackageDir( destination );
                disconnect( this, SIGNAL(signalIpkgMessage(const QString &)), 0, 0 );
            };
            break;
        case OPackage::Remove : {
                connect( this, SIGNAL(signalIpkgMessage(const QString &)), receiver, slotOutput );

                // Get list of destinations for unlinking of packages not installed to root
                OConfItemList *destList = destinations();

                for ( QStringList::ConstIterator it = parameters.begin(); it != parameters.end(); ++it )
                {
                    unlinkPackage( (*it), destList );
                    ipkg_packages_remove( &m_ipkgArgs, (*it), true );
                }

                delete destList;
                disconnect( this, SIGNAL(signalIpkgMessage(const QString &)), 0, 0 );
            };
            break;
        case OPackage::Download : {
                connect( this, SIGNAL(signalIpkgMessage(const QString &)), receiver, slotOutput );
                for ( QStringList::ConstIterator it = parameters.begin(); it != parameters.end(); ++it )
                {
                    ipkg_packages_download( &m_ipkgArgs, (*it) );
                }
                disconnect( this, SIGNAL(signalIpkgMessage(const QString &)), 0, 0 );
            };
            break;
        case OPackage::Info : {
                connect( this, SIGNAL(signalIpkgStatus(const QString &)), receiver, slotOutput );
                ipkg_packages_info( &m_ipkgArgs, (*parameters.begin()), &fIpkgStatus, 0l );
                disconnect( this, SIGNAL(signalIpkgStatus(const QString &)), 0, 0 );
            };
            break;
        case OPackage::Files : {
                connect( this, SIGNAL(signalIpkgList(const QString &)), receiver, slotOutput );
                ipkg_package_files( &m_ipkgArgs, (*parameters.begin()), &fIpkgFiles, 0l );
                disconnect( this, SIGNAL(signalIpkgList(const QString &)), 0, 0 );
            };
            break;
        default : break;
    };

    return true;
}

void OIpkg::ipkgMessage( char *msg )
{
    emit signalIpkgMessage( msg );
}

void OIpkg::ipkgStatus( char *status )
{
    emit signalIpkgStatus( status );
}

void OIpkg::ipkgList( char *filelist )
{
    emit signalIpkgList( filelist );
}

void OIpkg::loadConfiguration()
{
    if ( m_confInfo )
        delete m_confInfo;

    // Load configuration item list
    m_confInfo = new OConfItemList();

    QStringList confFiles;
    QDir confDir( IPKG_CONF_DIR );
    if ( confDir.exists() )
    {
        confDir.setNameFilter( "*.conf" );
        confDir.setFilter( QDir::Files );
        confFiles = confDir.entryList( "*.conf", QDir::Files );
    }
    confFiles << IPKG_CONF;

    QStringList::Iterator lastFile = confFiles.end();
    for ( QStringList::Iterator it = confFiles.begin(); it != lastFile; ++it )
    {
        // Create absolute file path if necessary
        QString absFile = (*it);
        if ( !absFile.startsWith( "/" ) )
            absFile.prepend( QString( IPKG_CONF_DIR ) + "/" );

        // Read in file
        QFile f( absFile );
        if ( f.open( IO_ReadOnly ) )
        {
            QTextStream s( &f );
            while ( !s.eof() )
            {

                QString line = s.readLine().simplifyWhiteSpace();

                // Parse line and save info to the conf options list
                if ( !line.isEmpty() )
                {
                    // Strip leading comment marker if exists
                    bool comment = false;
                    if ( line.startsWith( "#" ) )
                    {
                        line.remove( 0, 1 );
                        line = line.simplifyWhiteSpace();
                        comment = true;
                    }

                    bool recognizedOption = true;
                    int pos = line.find( ' ', 1 )  + 1;
                    int endpos = line.find( ' ', pos );

                    // Name
                    QString name = line.mid( pos, endpos - pos );

                    // Value
                    QString value = "";
                    if ( endpos > -1 )
                        value = line.right( line.length() - endpos - 1 );

                    // Active
                    bool active = !comment;

                    // Type
                    // For options w/type = Other, the mapping is as follows:
                    //    name = typeStr (e.g. "lists_dir")
                    //    value = value
                    //    features = name (from configuration file)

                    QString typeStr = line.left( pos - 1 );
                    OConfItem::Type type;
                    QString features;
                    if ( typeStr == "src" )
                        type = OConfItem::Source;
                    else if ( typeStr == "src/gz" )
                    {
                        type = OConfItem::Source;
                        features = "Compressed";
                    }
                    else if ( typeStr == "dest" )
                        type = OConfItem::Destination;
                    else if ( typeStr == "option" )
                        type = OConfItem::Option;
                    else if ( typeStr == "arch" )
                        type = OConfItem::Arch;
                    else if ( typeStr == "lists_dir" )
                    {
                        type = OConfItem::Other;
                        features = name;
                        name = typeStr;

                        // Default value when not defined
                        if ( value == QString::null || value == "" )
                            value = IPKG_PKG_PATH;
                    }
                    else
                        recognizedOption = false;

                    // Add to list
                    if ( recognizedOption )
                        m_confInfo->append( new OConfItem( type, name, value, features, active ) );
                }
            }

            f.close();
        }
    }

    // Load Ipkg execution options from application configuration file
    if ( m_config )
    {
        m_config->setGroup( "Ipkg" );
        m_ipkgExecOptions = m_config->readNumEntry( "ExecOptions", m_ipkgExecOptions );
        m_ipkgExecVerbosity = m_config->readNumEntry( "Verbosity", m_ipkgExecVerbosity );
    }
}

OConfItemList *OIpkg::filterConfItems( OConfItem::Type typefilter )
{
    // Load Ipkg configuration info if not already cached
    if ( !m_confInfo )
        loadConfiguration();

    // Build new server list (caller is responsible for deleting)
    OConfItemList *sl = new OConfItemList;

    // If typefilter is empty, retrieve all items
    bool retrieveAll = ( typefilter == OConfItem::NotDefined );

    // Parse configuration info for servers
    OConfItemListIterator it( *m_confInfo );
    for ( ; it.current(); ++it )
    {
        OConfItem *item = it.current();
        if ( retrieveAll || item->type() == typefilter )
        {
            sl->append( item );
        }
    }

    return sl;
}

const QString &OIpkg::rootPath()
{
    if ( m_rootPath.isEmpty() )
    {
        OConfItem *rootDest = findConfItem( OConfItem::Destination, "root" );
        rootDest ? m_rootPath = rootDest->value()
                 : m_rootPath = '/';
        if ( m_rootPath.right( 1 ) == '/' )
            m_rootPath.truncate( m_rootPath.length() - 1 );
    }
    return m_rootPath;
}

void OIpkg::linkPackageDir( const QString &dest )
{
    if ( !dest.isNull() )
    {
        OConfItem *destConfItem = findConfItem( OConfItem::Destination, dest );

        emit signalIpkgMessage( tr( "Linking packages installed in: %1" ).arg( dest ) );

        // Set package destination directory
        QString destDir = destConfItem->value();
        QString destInfoDir = destDir;
        if ( destInfoDir.right( 1 ) != '/' )
            destInfoDir.append( '/' );
        destInfoDir.append( IPKG_INFO_PATH );

        // Get list of installed packages in destination
        QDir packageDir( destInfoDir );
        QStringList packageFiles;
        if ( packageDir.exists() )
        {
            packageDir.setNameFilter( "*.list" );
            packageDir.setFilter( QDir::Files );
            packageFiles = packageDir.entryList( "*.list", QDir::Files );
        }

        // Link all files for every package installed in desination
        QStringList::Iterator lastFile = packageFiles.end();
        for ( QStringList::Iterator it = packageFiles.begin(); it != lastFile; ++it )
        {
            //emit signalIpkgMessage( QString( "Processing: %1/%2" ).arg( destInfoDir ).arg (*it) );
            QString packageFileName = destInfoDir;
            packageFileName.append( '/' );
            packageFileName.append( (*it) );
            QFile packageFile( packageFileName );
            if ( packageFile.open( IO_ReadOnly ) )
            {
                QTextStream t( &packageFile );
                QString linkFile;
                while ( !t.eof() )
                {
                    // Get the name of the file to link and build the sym link filename
                    linkFile = t.readLine();
                    QString linkDest( linkFile.right( linkFile.length() - destDir.length() ) );
                    linkDest.prepend( rootPath() );

                    // If file installed file is actually symbolic link, use actual file for linking
                    QFileInfo fileInfo( linkFile );
                    if ( fileInfo.isSymLink() && !fileInfo.readLink().isEmpty() )
                        linkFile = fileInfo.readLink();

                    // See if directory exists in 'root', if not, create
                    fileInfo.setFile( linkDest );
                    QString linkDestDirName = fileInfo.dirPath( true );
                    QDir linkDestDir( linkDestDirName );
                    if ( !linkDestDir.exists() )
                    {
                        linkDestDir.mkdir( linkDestDirName );
                    }
                    else
                    {
                        // Remove any previous link to make sure we will be pointing to the current version
                        if ( QFile::exists( linkDest ) )
                            QFile::remove( linkDest );
                    }

                    // Link the file
                    //emit signalIpkgMessage( QString( "Linking '%1' to '%2'" ).arg( linkFile ).arg( linkDest ) );
                    if ( symlink( linkFile, linkDest ) == -1 )
                        emit signalIpkgMessage( tr( "Error linkling '%1' to '%2'" )
                                                .arg( linkFile )
                                                .arg( linkDest ) );
                }
                packageFile.close();
            }
        }
    }
}

void OIpkg::unlinkPackage( const QString &package, OConfItemList *destList )
{
    if ( !package.isNull() )
    {
        // Find destination package is installed in
        if ( destList )
        {
            OConfItemListIterator it( *destList );
            for ( ; it.current(); ++it )
            {
                OConfItem *dest = it.current();
                QString destInfoFileName = QString( "%1/%2/%3.list" ).arg( dest->value() )
                                                                     .arg( IPKG_INFO_PATH )
                                                                     .arg( package );
                //emit signalIpkgMessage( QString( "Looking for '%1'" ).arg ( destInfoFileName ) );

                // If found and destination is not 'root', remove symbolic links
                if ( QFile::exists( destInfoFileName ) && dest->name() != "root" )
                {
                    QFile destInfoFile( destInfoFileName );
                    if ( destInfoFile.open( IO_ReadOnly ) )
                    {
                        QTextStream t( &destInfoFile );
                        QString linkFile;
                        while ( !t.eof() )
                        {
                            // Get the name of the file to link and build the sym link filename
                            linkFile = t.readLine();
                            QString linkDest( linkFile.right( linkFile.length() -
                                                              dest->value().length() ) );
                            linkDest.prepend( rootPath() );

                            //emit signalIpkgMessage( QString( "Deleting: '%1'" ).arg( linkDest ) );
                            QFile::remove( linkDest );
                        }
                        destInfoFile.close();
                    }

                    emit signalIpkgMessage( tr( "Links removed for: %1" ).arg( package ) );
                    return;
                }
            }
        }
    }
}