summaryrefslogtreecommitdiff
path: root/src/jobs/widget_install/task_unzip.cpp
blob: c5f63d3af687f22fa9677c92041eb9ffe736780e (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
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
/*
 * @file    task_unzip.cpp
 * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
 * @version 1.0
 * @brief   Implementation file for installer task unzip
 */
#include <widget_install/task_unzip.h>
#include <widget_install/widget_install_errors.h>
#include <widget_install/widget_install_context.h>
#include <widget_install/job_widget_install.h>
#include <dpl/log/log.h>
#include <dpl/copy.h>
#include <dpl/file_output.h>
#include <dpl/abstract_waitable_input_adapter.h>
#include <dpl/wrt-dao-ro/global_config.h>
#include <task_commons.h>
#include <sys/stat.h>

using namespace WrtDB;

namespace {
struct PathAndFilePair
{
    std::string path;
    std::string file;

    PathAndFilePair(const std::string &p,
            const std::string &f) :
        path(p),
        file(f)
    {
    }
};

PathAndFilePair SplitFileAndPath(const std::string &filePath)
{
    std::string::size_type position = filePath.rfind('/');

    // Is this only a file without a path ?
    if (position == std::string::npos) {
        return PathAndFilePair(std::string(), filePath);
    }

    // This is full file-path pair
    return PathAndFilePair(filePath.substr(0,
                                           position),
                           filePath.substr(position + 1));
}
}

namespace Jobs {
namespace WidgetInstall {
TaskUnzip::TaskUnzip(InstallerContext &installerContext) :
    DPL::TaskDecl<TaskUnzip>(this),
    m_installerContext(installerContext)
{
    // Install steps
    AddStep(&TaskUnzip::StepUnzipPrepare);
    AddStep(&TaskUnzip::StepUnzipProgress);
    AddStep(&TaskUnzip::StepUnzipFinished);
}

void TaskUnzip::ExtractFile(DPL::ZipInput::File *input,
        const std::string &destFileName)
{
    Try
    {
        DPL::AbstractWaitableInputAdapter inputAdapter(input);
        DPL::FileOutput output(destFileName);

        DPL::Copy(&inputAdapter, &output);
    }
    Catch(DPL::FileOutput::Exception::OpenFailed)
    {
        ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
    }
    Catch(DPL::CopyFailed)
    {
        ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
    }
}

void TaskUnzip::StepUnzipPrepare()
{
    LogInfo("Prepare to unzip...");

    Try
    {
        m_zip.Reset(new DPL::ZipInput(m_installerContext.locations->getWidgetSource()));
        LogInfo("Widget package comment: " << m_zip->GetGlobalComment());

        // Widget package must not be empty
        if (m_zip->empty()) {
            ThrowMsg(Exceptions::ZipEmpty, m_installerContext.locations->getWidgetSource());
        }

        // Set iterator to first file
        m_zipIterator = m_zip->begin();
    }
    Catch(DPL::ZipInput::Exception::OpenFailed)
    {
        ReThrowMsg(Exceptions::OpenZipFailed, m_installerContext.locations->getWidgetSource());
    }
}

void TaskUnzip::StepUnzipProgress()
{
    // Show file info
    LogInfo("Unzipping: '" << m_zipIterator->name <<
            "', Comment: '" << m_zipIterator->comment <<
            "', Compressed size: " << m_zipIterator->compressedSize <<
            ", Uncompressed size: " << m_zipIterator->uncompressedSize);

    // Normalize file paths
    // FIXME: Implement checking for invalid characters

    // Extract file or path
    std::string fileName = m_zipIterator->name;

    if (fileName[fileName.size() - 1] == '/') {
        // This is path
        std::string newPath = m_installerContext.locations->getTemporaryPackageDir() + "/" +
            fileName.substr(0, fileName.size() - 1);
        LogPedantic("Path to extract: " << newPath);

        // Create path in case of it is empty
        createTempPath(newPath);
    } else {
        // This is regular file
        std::string fileExtractPath =
            m_installerContext.locations->getTemporaryPackageDir() + "/" + fileName;

        LogPedantic("File to extract: " << fileExtractPath);

        // Split into pat & file pair
        PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);

        LogPedantic("Path and file: " <<
                    pathAndFile.path <<
                    " : " << pathAndFile.file);

        // First, ensure that path exists
        createTempPath(pathAndFile.path);

        Try
        {
            // Open file
            DPL::ScopedPtr<DPL::ZipInput::File> file(
                m_zip->OpenFile(fileName));

            // Extract single file
            ExtractFile(file.Get(), fileExtractPath);
        }
        Catch(DPL::ZipInput::Exception::OpenFileFailed)
        {
            ThrowMsg(Exceptions::ExtractFileFailed, fileName);
        }
    }

    // Check whether there are more files to extract
    if (++m_zipIterator == m_zip->end()) {
        LogInfo("Unzip progress finished successfuly");
    } else {
        SwitchToStep(&TaskUnzip::StepUnzipProgress);
    }

    m_installerContext.job->UpdateProgress(
        InstallerContext::INSTALL_UNZIP_FILES,
        "Unzip widget files to temporary directory");
}

void TaskUnzip::StepUnzipFinished()
{
    // Unzip finished, close internal structures
    m_zip.Reset();

    // Done
    LogInfo("Unzip finished");
}

} //namespace WidgetInstall
} //namespace Jobs