summaryrefslogtreecommitdiff
path: root/noncore/settings/aqpkg/datamgr.cpp
blob: b6e6e37a409fb404a325330d77c97707818901c4 (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
/***************************************************************************
                          datamgr.cpp  -  description
                             -------------------
    begin                : Thu Aug 29 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>
#include <iostream>
using namespace std;

#include <stdio.h>

#include "datamgr.h"
#include "global.h"


DataManager::DataManager()
{
	activeServer = "";
}

DataManager::~DataManager()
{
}

Server *DataManager :: getServer( const char *name )
{
	Server *s = 0;
	vector<Server>::iterator it = serverList.begin();
    while ( it != serverList.end() && s == 0 )
    {
		if ( it->getServerName() == name )
			s = &(*it);

		++it;
	}

	return s;
}

Destination *DataManager :: getDestination( const char *name )
{
	Destination *d = 0;
	vector<Destination>::iterator it = destList.begin();
    while ( it != destList.end() && d == 0 )
    {
		if ( it->getDestinationName() == name )
			d = &(*it);

		++it;
	}

	return d;
}

void DataManager :: loadServers()
{
    // First add our local server - not really a server but
    // the local config (which packages are installed)
    serverList.push_back( Server( LOCAL_SERVER, "" ) );
    serverList.push_back( Server( LOCAL_IPKGS, "" ) );

    // Read file from /etc/ipkg.conf
    QString ipkg_conf = IPKG_CONF;
    FILE *fp;
    fp = fopen( ipkg_conf, "r" );
    char line[130];
    QString lineStr;
    if ( fp == NULL )
    {
        cout << "Couldn't open " << ipkg_conf << "! err = " << fp << endl;
        return;
    }
    else
    {
        {
            cout << "Before ipkg.conf read" << endl;
            vector<Server>::iterator it;
            for ( it = serverList.begin() ; it != serverList.end() ; ++it )
                cout << "servername - " << it->getServerName() << endl;
        }
        
        while ( fgets( line, sizeof line, fp) != NULL )
        {
            lineStr = line;
            if ( lineStr.startsWith( "src" ) || lineStr.startsWith( "#src" ) || lineStr.startsWith( "# src" ) )
            {
                char alias[20];
                char url[100];


                // Looks a little wierd but read up to the r of src (throwing it away),
                // then read up to the next space and throw that away, the alias
                // is next.
                // Should Handle #src, # src, src, and combinations of
                sscanf( lineStr, "%*[^r]%*[^ ] %s %s", alias, url );
                cout << "Adding alias " << alias << " to list" << endl;
                cout << lineStr << endl;
                Server s( alias, url );
                serverList.push_back( s );

                if ( lineStr.startsWith( "src" ) )
               		setActiveServer( alias );
            }
            else if ( lineStr.startsWith( "dest" ) )
            {
                char alias[20];
                char path[50];
                sscanf( lineStr, "%*[^ ] %s %s", alias, path );
                Destination d( alias, path );
                destList.push_back( d );
            }
        }
        {
            cout << "After ipkg.conf read" << endl;
            vector<Server>::iterator it;
            for ( it = serverList.begin() ; it != serverList.end() ; ++it )
                cout << "servername - " << it->getServerName() << endl;
        }
        
    }
    fclose( fp );

    // Go through the server destination list and add root, cf and card if they
    // don't already exist
/* AQ - commented out as if you don't have a CF or SD card in then
 * this causes IPKG to try to create directories on non existant devices
 * (which of course fails), gives a nasty error message and can cause ipkg
 * to seg fault.
 *
    vector<Destination>::iterator dit;
    bool foundRoot = false;
    bool foundCF = false;
    bool foundCard = false;
    for ( dit = destList.begin() ; dit != destList.end() ; ++dit )
    {
        if ( dit->getDestinationPath() == "/" )
            foundRoot = true;
        if ( dit->getDestinationPath() == "/mnt/cf" )
            foundCF = true;
        if ( dit->getDestinationPath() == "/mnt/card" )
            foundCard = true;
    }

    // If running on a Zaurus (arm) then if we didn't find root, CF or card
    // destinations, add them as default
#ifdef QWS
#ifndef X86
    if ( !foundRoot )
        destList.push_back( Destination( "root", "/" ) );
    if ( !foundCF )
        destList.push_back( Destination( "cf", "/mnt/cf" ) );
    if ( !foundCF )
        destList.push_back( Destination( "card", "/mnt/card" ) );
#endif
#endif
*/    
    vector<Server>::iterator it;
    for ( it = serverList.begin() ; it != serverList.end() ; ++it )
        reloadServerData( it->getServerName() );
}

void DataManager :: reloadServerData( const char *serverName )
{
    Server *s = getServer( serverName );
    // Now we've read the config file in we need to read the servers
    // The local server is a special case. This holds the contents of the
    // status files the number of which depends on how many destinations
    // we've set up
    // The other servers files hold the contents of the server package list
    if ( s->getServerName() == LOCAL_SERVER )
        s->readStatusFile( destList );
    else if ( s->getServerName() == LOCAL_IPKGS )
    	s->readLocalIpks( getServer( LOCAL_SERVER ) );
    else
        s->readPackageFile( getServer( LOCAL_SERVER ) );

}

void DataManager :: writeOutIpkgConf()
{
    QString ipkg_conf = IPKG_CONF;
    ofstream out( ipkg_conf );

    out << "# Written by AQPkg" << endl;
    out << "# Must have one or more source entries of the form:" << endl;
    out << "#" << endl;
    out << "#   src <src-name> <source-url>" << endl;
    out << "#" << endl;
    out << "# and one or more destination entries of the form:" << endl;
    out << "#" << endl;
    out << "#   dest <dest-name> <target-path>" << endl;
    out << "#" << endl;
    out << "# where <src-name> and <dest-names> are identifiers that" << endl;
    out << "# should match [a-zA-Z0-9._-]+, <source-url> should be a" << endl;
    out << "# URL that points to a directory containing a Familiar" << endl;
    out << "# Packages file, and <target-path> should be a directory" << endl;
    out << "# that exists on the target system." << endl << endl;

    // Write out servers
    vector<Server>::iterator it = serverList.begin();
    while ( it != serverList.end() )
    {
        QString alias = it->getServerName();
        // Don't write out local as its a dummy
        if ( alias != LOCAL_SERVER && alias != LOCAL_IPKGS )
        {
            QString url = it->getServerUrl();;

            if ( !activeServer || alias != activeServer )
                out << "#";
            out << "src " << alias << " " << url << endl;
        }

        it++;
    }

    out << endl;

    // Write out destinations
    vector<Destination>::iterator it2 = destList.begin();
    while ( it2 != destList.end() )
    {
        out << "dest " << it2->getDestinationName() << " " << it2->getDestinationPath() << endl;
        it2++;
    }

    out.close();
}