File: afcfile.cpp

package info (click to toggle)
kio-extras 4%3A25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 31,928 kB
  • sloc: cpp: 28,852; ansic: 3,084; perl: 1,048; xml: 116; sh: 92; python: 28; makefile: 9
file content (116 lines) | stat: -rw-r--r-- 2,943 bytes parent folder | download
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
/*
 * SPDX-FileCopyrightText: 2022 Kai Uwe Broulik <kde@broulik.de>
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "afcfile.h"

#include "afc_debug.h"

#include "afcutils.h"

using namespace KIO;

AfcFile::AfcFile(const AfcClient::Ptr &client, const QString &path)
    : m_client(client)
    , m_path(path)
{
}

AfcFile::AfcFile(AfcFile &&other) Q_DECL_NOEXCEPT : m_client(std::move(other.m_client)), m_path(other.m_path), m_handle(other.m_handle)
{
    other.m_handle.reset();
}

AfcFile::~AfcFile()
{
    if (m_handle) {
        close();
    }
}

AfcClient::Ptr AfcFile::client() const
{
    return m_client;
}

QString AfcFile::path() const
{
    return m_path;
}

WorkerResult AfcFile::open(QIODevice::OpenMode mode)
{
    Q_ASSERT(!m_handle);

    afc_file_mode_t fileMode = static_cast<afc_file_mode_t>(0);

    if (mode == QIODevice::ReadOnly) {
        fileMode = AFC_FOPEN_RDONLY;
    } else if (mode == QIODevice::WriteOnly) {
        fileMode = AFC_FOPEN_WRONLY;
    } else if (mode == QIODevice::ReadWrite) {
        fileMode = AFC_FOPEN_RW;
    } else if (mode == (QIODevice::ReadWrite | QIODevice::Truncate)) {
        fileMode = AFC_FOPEN_WR;
    } else if (mode == QIODevice::Append || mode == (QIODevice::Append | QIODevice::WriteOnly)) {
        fileMode = AFC_FOPEN_APPEND;
    } else if (mode == (QIODevice::Append | QIODevice::ReadWrite)) {
        fileMode = AFC_FOPEN_RDAPPEND;
    }

    if (!fileMode) {
        return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, QString::number(mode));
    }

    uint64_t handle = 0;
    const auto ret = afc_file_open(m_client->internalClient(), m_path.toLocal8Bit().constData(), fileMode, &handle);

    const WorkerResult result = AfcUtils::Result::from(ret);
    if (result.success()) {
        m_handle = handle;
    }
    return result;
}

WorkerResult AfcFile::seek(filesize_t offset)
{
    Q_ASSERT(m_handle);
    const auto ret = afc_file_seek(m_client->internalClient(), m_handle.value(), offset, SEEK_SET);
    return AfcUtils::Result::from(ret);
}

WorkerResult AfcFile::truncate(filesize_t length)
{
    Q_ASSERT(m_handle);
    const auto ret = afc_file_truncate(m_client->internalClient(), m_handle.value(), length);
    return AfcUtils::Result::from(ret);
}

WorkerResult AfcFile::write(const QByteArray &data, uint32_t &bytesWritten)
{
    Q_ASSERT(m_handle);
    const auto ret = afc_file_write(m_client->internalClient(), m_handle.value(), data.constData(), data.size(), &bytesWritten);
    return AfcUtils::Result::from(ret);
}

WorkerResult AfcFile::close()
{
    Q_ASSERT(m_handle);

    const auto ret = afc_file_close(m_client->internalClient(), m_handle.value());

    const WorkerResult result = AfcUtils::Result::from(ret);
    if (result.success()) {
        m_handle.reset();
    }
    return result;
}

AfcFileReader AfcFile::reader() const
{
    Q_ASSERT(m_handle);

    AfcFileReader reader(m_client, m_handle.value());
    return reader;
}