summaryrefslogtreecommitdiffstats
path: root/rbutil/installlog.cpp
blob: 4be4a9735278c6373ee90af7c73d98ef210c416b (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * Module: rbutil
 * File: installlog.cpp
 *
 * Copyright (C) 2006 Christi Alice Scarborough
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "installlog.h"
#include "rbutil.h"

InstallLog::InstallLog(wxString logname, bool CreateLog)
{
    wxString buf;
    dirtyflag = true;

    if (! CreateLog && ! wxFileExists(logname) ) return;

    logfile = new wxFileConfig(wxEmptyString, wxEmptyString, logname);


    if (!logfile)
    {
        buf.Printf(_("Failed to create install log file: %s"), logname.c_str());
        wxLogWarning(buf);
        return;
    }

    logfile->SetPath(wxT("/InstallLog"));
    if (logfile->Exists(wxT("Version")) &&
        logfile->Read(wxT("Version"), 0l) != LOGFILE_VERSION )
    {
        buf.Printf(_("Logfile version mismatch: %s"), logname.c_str() );
        wxLogWarning(buf);
        delete logfile;
        return;
    }

    logfile->Write(wxT("Version"), LOGFILE_VERSION);
    dirtyflag = false;
}

InstallLog::~InstallLog()
{
    if (dirtyflag) return;

    delete logfile;
}

unsigned int InstallLog::WriteFile(wxString filepath, bool isDir)
{
    wxString key, buf;
    long installcount = 0;

    if (dirtyflag) return true;

    filepath.Replace(PATH_SEP, wxT("/") );

    if (filepath.GetChar(0) == '/')
        filepath = filepath.Right(filepath.Len() - 1);

    logfile->SetPath(wxT("/FilePaths"));
    installcount = logfile->Read(filepath, 0l);

    if (isDir)
    {
        filepath.Append(wxT("/" DIRECTORY_KLUDGE) ); // Needed for empty dirs
    }

    logfile->Write(filepath, ++installcount);

    return false;
}

unsigned int InstallLog::WriteFile(wxArrayString filepaths)
{
    unsigned long i;
    unsigned int finalrc = false;
    wxString thisone;

    if (dirtyflag) return true;

    for (i = 0; i < filepaths.GetCount(); i++);
    {
        if ( WriteFile(filepaths[i]) )
        {
            finalrc++;
        }
    }

    return finalrc;
}

wxArrayString* InstallLog::GetInstalledFiles()
{
    wxString curdir = wxT("");

    if (dirtyflag) return NULL;
    workingAS.Clear();

    EnumerateCurDir(wxT(""));

    wxArrayString* out = new wxArrayString(workingAS);
    return out;
}

void InstallLog::EnumerateCurDir(wxString curdir)
{
    bool contflag;
    wxString curname, buf, buf2, pathcache;
    long dummy;

    buf.Printf(wxT("/FilePaths/%s"), curdir.c_str());
    pathcache = logfile->GetPath();
    logfile->SetPath(buf);

    contflag = logfile->GetFirstGroup(curname, dummy);
    while (contflag)
    {
        buf.Printf(wxT("%s/%s"), curdir.c_str(), curname.c_str() );
        buf2 = buf; buf2.Replace(wxT("/"), PATH_SEP);
        workingAS.Add(buf2);
        EnumerateCurDir(buf);
        contflag = logfile->GetNextGroup(curname, dummy);
    }

    contflag = logfile->GetFirstEntry(curname, dummy);
    while (contflag)
    {
        if (curname != wxT(DIRECTORY_KLUDGE) )
        {
            buf.Printf(wxT("%s/%s"), curdir.c_str(), curname.c_str() );
            buf2 = buf; buf2.Replace(wxT("/"), PATH_SEP);
            workingAS.Add(buf2);
        }
        contflag = logfile->GetNextEntry(curname, dummy);
    }

    logfile->SetPath(pathcache);
}