summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/base/mspackutil.cpp
blob: b794272199eccad813360e125adbe651c5665cbe (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 *   Copyright (C) 2013 Amaury Pouly
 *
 * 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 <QtCore>
#include "Logger.h"
#include "mspackutil.h"
#include "progressloggerinterface.h"

MsPackUtil::MsPackUtil(QObject* parent)
    :ArchiveUtil(parent)
{
    m_cabd = mspack_create_cab_decompressor(nullptr);
    m_cabinet = nullptr;
    if(!m_cabd)
        LOG_ERROR() << "CAB decompressor creation failed!";
}

MsPackUtil::~MsPackUtil()
{
    close();
    if(m_cabd)
        mspack_destroy_cab_decompressor(m_cabd);
}

bool MsPackUtil::open(QString& mspackfile)
{
    close();

    if(m_cabd == nullptr)
    {
        LOG_ERROR() << "No CAB decompressor available: cannot open file!";
        return false;
    }
    m_cabinet = m_cabd->search(m_cabd, QFile::encodeName(mspackfile).constData());
    return m_cabinet != nullptr;
}

bool MsPackUtil::close(void)
{
    if(m_cabd && m_cabinet)
        m_cabd->close(m_cabd, m_cabinet);
    m_cabinet = nullptr;
    return true;
}

bool MsPackUtil::extractArchive(const QString& dest, QString file)
{
    LOG_INFO() << "extractArchive" << dest << file;
    if(!m_cabinet)
    {
        LOG_ERROR() << "CAB file not open!";
        return false;
    }

    // construct the filename when extracting a single file from an archive.
    // if the given destination is a full path use it as output name,
    // otherwise use it as path to place the file as named in the archive.
    QString singleoutfile;
    if(!file.isEmpty() && QFileInfo(dest).isDir())
        singleoutfile = dest + "/" + file;
    else if(!file.isEmpty())
        singleoutfile = dest;
    struct mscabd_file *f = m_cabinet->files;
    if(f == nullptr)
    {
        LOG_WARNING() << "CAB doesn't contain file" << file;
        return true;
    }
    bool found = false;
    while(f)
    {
        QString name = QFile::decodeName(f->filename);
        name.replace("\\", "/");
        if(name.at(0) == '/')
            name.remove(0, 1);
        if(name == file || file.isEmpty())
        {
            QString path;
            if(!singleoutfile.isEmpty())
                path = singleoutfile;
            else
                path = dest + "/" + name;
            // make sure the output path exists
            if(!QDir().mkpath(QFileInfo(path).absolutePath()))
            {
                emit logItem(tr("Creating output path failed"), LOGERROR);
                LOG_ERROR() << "creating output path failed for:" << path;
                emit logProgress(1, 1);
                return false;
            }
            int ret = m_cabd->extract(m_cabd, f, QFile::encodeName(path).constData());
            if(ret != MSPACK_ERR_OK)
            {
                emit logItem(tr("Error during CAB operation"), LOGERROR);
                LOG_ERROR() << "mspack error: " << ret
                            << "(" << errorStringMsPack(ret) << ")";
                emit logProgress(1, 1);
                return false;
            }
            found = true;
        }
        f = f->next;
    }
    emit logProgress(1, 1);

    return found;
}

QStringList MsPackUtil::files(void)
{
    QStringList list;
    if(!m_cabinet)
    {
        LOG_WARNING() << "CAB file not open!";
        return list;
    }
    struct mscabd_file *file = m_cabinet->files;
    while(file)
    {
        QString name = QFile::decodeName(file->filename);
        name.replace("\\", "/");
        if(name.at(0) == '/')
            name.remove(0, 1);
        list.append(name);
        file = file->next;
    }

    return list;
}

QString MsPackUtil::errorStringMsPack(int error) const
{
    switch(error)
    {
        case MSPACK_ERR_OK: return "Ok";
        case MSPACK_ERR_ARGS: return "Bad arguments";
        case MSPACK_ERR_OPEN: return "Open error";
        case MSPACK_ERR_READ: return "Read error";
        case MSPACK_ERR_WRITE: return "Write error";
        case MSPACK_ERR_SEEK: return "Seek error";
        case MSPACK_ERR_NOMEMORY: return "Out of memory";
        case MSPACK_ERR_SIGNATURE: return "Bad signature";
        case MSPACK_ERR_DATAFORMAT: return "Bad data format";
        case MSPACK_ERR_CHECKSUM: return "Checksum error";
        case MSPACK_ERR_CRUNCH: return "Compression error";
        case MSPACK_ERR_DECRUNCH: return "Decompression error";
        default: return "Unknown";
    }
}