sptk2 logo
SPTK Home Page
ArchiveFile.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#ifndef SPTK_ARCHIVEFILE_H
28#define SPTK_ARCHIVEFILE_H
29
30#include <sptk5/Buffer.h>
31#include <sptk5/DateTime.h>
32
33namespace sptk {
34
35constexpr int TAR_BLOCK_SIZE = 512;
36
37#pragma pack(push, 1)
38
42struct TarHeader {
43 std::array<char, 100> filename;
44 std::array<char, 8> mode;
45 std::array<char, 8> uid;
46 std::array<char, 8> gid;
47 std::array<char, 12> size;
48 std::array<char, 12> mtime;
49 std::array<char, 8> chksum;
50 char typeflag;
51 std::array<char, 100> linkname;
52 std::array<char, 6> magic;
53 std::array<char, 2> version;
54 std::array<char, 32> uname;
55 std::array<char, 32> gname;
56 std::array<char, 8> devmajor;
57 std::array<char, 8> devminor;
58 std::array<char, 155> prefix;
59 std::array<char, 12> padding;
60};
61
62#pragma pack(pop)
63
67class SP_EXPORT ArchiveFile
68 : public Buffer
69{
70public:
74 enum class Type : uint8_t
75 {
76 REGULAR_FILE = '0',
77 REGULAR_FILE2 = '\0',
78 HARD_LINK = '1',
79 SYM_LINK = '2',
80 CHARACTER = '3',
81 BLOCK = '4',
82 DIRECTORY = '5',
83 FIFO = '6',
84 CONTTYPE = '7'
85 };
86
87 struct Ownership {
88 int uid {0};
89 int gid {0};
90 String uname;
91 String gname;
92 };
93
99 explicit ArchiveFile(const fs::path& fileName, const fs::path& baseDirectory);
100
111 ArchiveFile(const fs::path& fileName, const Buffer& content, int mode, const DateTime& mtime,
112 ArchiveFile::Type type, const Ownership& ownership, const fs::path& linkName);
113
118 const char* header() const;
119
120 String fileName() const
121 {
122 return m_fileName;
123 }
124
125 unsigned mode() const
126 {
127 return m_mode;
128 }
129
130 const Ownership& ownership() const
131 {
132 return m_ownership;
133 }
134
135 unsigned size() const
136 {
137 return m_size;
138 }
139
140 DateTime mtime() const
141 {
142 return m_mtime;
143 }
144
145 Type type() const
146 {
147 return m_type;
148 }
149
150 String linkname() const
151 {
152 return m_linkname;
153 }
154
155 static fs::path relativePath(const fs::path& fileName, const fs::path& baseDirectory);
156
157private:
158 String m_fileName;
159 unsigned m_mode {777};
160 Ownership m_ownership {};
161 unsigned m_size {0};
162 DateTime m_mtime;
163 Type m_type {Type::REGULAR_FILE};
164 String m_linkname;
165
166 std::shared_ptr<TarHeader> m_header;
167
168 void makeHeader();
169};
170
171using SArchiveFile = std::shared_ptr<ArchiveFile>;
172
173} // namespace sptk
174
175#endif
File inside tar archive.
Definition: ArchiveFile.h:69
Type
File type for file inside tar archive.
Definition: ArchiveFile.h:75
const char * header() const
Actual tar file header, length is TAR_BLOCK_SIZE.
ArchiveFile(const fs::path &fileName, const fs::path &baseDirectory)
Constructor.
ArchiveFile(const fs::path &fileName, const Buffer &content, int mode, const DateTime &mtime, ArchiveFile::Type type, const Ownership &ownership, const fs::path &linkName)
Constructor.
Definition: Buffer.h:51
Definition: DateTime.h:86
Definition: String.h:49
Definition: ArchiveFile.h:87
Definition: ArchiveFile.h:42

Fri Oct 14 2022 09:58:31: SPTK 5.4.1