Skip to content

Commit fd157eb

Browse files
committed
WIP file_stat
1 parent 46cf711 commit fd157eb

25 files changed

+1026
-589
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ list(
351351
src/dwarfs/entry.cpp
352352
src/dwarfs/error.cpp
353353
src/dwarfs/file_scanner.cpp
354+
src/dwarfs/file_type.cpp
354355
src/dwarfs/filesystem_extractor.cpp
355356
src/dwarfs/filesystem_v2.cpp
356357
src/dwarfs/filesystem_writer.cpp
@@ -366,7 +367,7 @@ list(
366367
src/dwarfs/nilsimsa.cpp
367368
src/dwarfs/option_map.cpp
368369
src/dwarfs/options.cpp
369-
src/dwarfs/os_access_posix.cpp
370+
src/dwarfs/os_access_generic.cpp
370371
src/dwarfs/progress.cpp
371372
src/dwarfs/scanner.cpp
372373
src/dwarfs/similarity.cpp

include/dwarfs/entry.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@
3030
#include <string_view>
3131
#include <vector>
3232

33-
#include <sys/stat.h>
34-
3533
#include <folly/small_vector.h>
3634

3735
#include "dwarfs/entry_interface.h"
36+
#include "dwarfs/file_stat.h"
3837

3938
namespace dwarfs {
4039

@@ -68,16 +67,16 @@ class entry : public entry_interface {
6867
public:
6968
enum type_t { E_FILE, E_DIR, E_LINK, E_DEVICE, E_OTHER };
7069

71-
entry(const std::string& name, std::shared_ptr<entry> parent,
72-
const struct ::stat& st);
70+
entry(std::string const& name, std::shared_ptr<entry> parent,
71+
file_stat const& st);
7372

7473
bool has_parent() const;
7574
std::shared_ptr<entry> parent() const;
76-
void set_name(const std::string& name);
75+
void set_name(std::string const& name);
7776
std::string path() const override;
7877
std::string dpath() const override;
79-
const std::string& name() const override { return name_; }
80-
size_t size() const override { return stat_.st_size; }
78+
std::string const& name() const override { return name_; }
79+
size_t size() const override { return stat_.size; }
8180
virtual type_t type() const = 0;
8281
std::string type_string() const override;
8382
bool is_directory() const override;
@@ -88,11 +87,11 @@ class entry : public entry_interface {
8887
void update(global_entry_data& data) const;
8988
virtual void accept(entry_visitor& v, bool preorder = false) = 0;
9089
virtual void scan(os_access& os, progress& prog) = 0;
91-
const struct ::stat& status() const { return stat_; }
90+
file_stat const& status() const { return stat_; }
9291
void set_entry_index(uint32_t index) { entry_index_ = index; }
9392
std::optional<uint32_t> const& entry_index() const { return entry_index_; }
94-
uint64_t raw_inode_num() const { return stat_.st_ino; }
95-
uint64_t num_hard_links() const { return stat_.st_nlink; }
93+
uint64_t raw_inode_num() const { return stat_.ino; }
94+
uint64_t num_hard_links() const { return stat_.nlink; }
9695
virtual void set_inode_num(uint32_t ino) = 0;
9796
virtual std::optional<uint32_t> const& inode_num() const = 0;
9897

@@ -110,19 +109,19 @@ class entry : public entry_interface {
110109
uint64_t get_ctime() const override;
111110
void set_ctime(uint64_t ctime) override;
112111

113-
void override_size(size_t size) { stat_.st_size = size; }
112+
void override_size(size_t size) { stat_.size = size; }
114113

115114
private:
116115
std::string name_;
117116
std::weak_ptr<entry> parent_;
118-
struct ::stat stat_;
117+
file_stat stat_;
119118
std::optional<uint32_t> entry_index_;
120119
};
121120

122121
class file : public entry {
123122
public:
124123
file(const std::string& name, std::shared_ptr<entry> parent,
125-
const struct ::stat& st)
124+
file_stat const& st)
126125
: entry(name, std::move(parent), st) {}
127126

128127
type_t type() const override;

include/dwarfs/file_stat.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* vim:set ts=2 sw=2 sts=2 et: */
2+
/**
3+
* \author Marcus Holland-Moritz ([email protected])
4+
* \copyright Copyright (c) Marcus Holland-Moritz
5+
*
6+
* This file is part of dwarfs.
7+
*
8+
* dwarfs is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* dwarfs is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
#pragma once
23+
24+
#include <cstdint>
25+
#include <filesystem>
26+
#include <memory>
27+
#include <type_traits>
28+
29+
#include "dwarfs/file_type.h"
30+
31+
namespace dwarfs {
32+
33+
struct file_stat {
34+
using perms_type = std::underlying_type_t<std::filesystem::perms>;
35+
using mode_type = uint32_t;
36+
using dev_type = uint64_t;
37+
using ino_type = uint64_t;
38+
using nlink_type = uint64_t;
39+
using uid_type = uint32_t;
40+
using gid_type = uint32_t;
41+
using off_type = int64_t;
42+
using blksize_type = int64_t;
43+
using blkcnt_type = int64_t;
44+
using time_type = int64_t;
45+
46+
std::filesystem::file_status status() const {
47+
return file_mode_to_status(mode);
48+
};
49+
50+
posix_file_type::value type() const {
51+
return static_cast<posix_file_type::value>(mode & posix_file_type::mask);
52+
};
53+
54+
perms_type permissions() const { return mode & 07777; };
55+
56+
void set_permissions(perms_type perms) { mode = type() | (perms & 07777); }
57+
58+
bool is_directory() const { return type() == posix_file_type::directory; }
59+
60+
bool is_regular_file() const { return type() == posix_file_type::regular; }
61+
62+
bool is_symlink() const { return type() == posix_file_type::symlink; }
63+
64+
bool is_device() const {
65+
auto t = type();
66+
return t == posix_file_type::block || t == posix_file_type::character;
67+
}
68+
69+
dev_type dev;
70+
ino_type ino;
71+
nlink_type nlink;
72+
mode_type mode;
73+
uid_type uid;
74+
gid_type gid;
75+
dev_type rdev;
76+
off_type size;
77+
blksize_type blksize;
78+
blkcnt_type blocks;
79+
time_type atime;
80+
time_type mtime;
81+
time_type ctime;
82+
};
83+
84+
template <typename T>
85+
void copy_file_stat(T* out, file_stat const& in) {
86+
out->st_dev = in.dev;
87+
out->st_ino = in.ino;
88+
out->st_nlink = in.nlink;
89+
out->st_mode = in.mode;
90+
out->st_uid = in.uid;
91+
out->st_gid = in.gid;
92+
out->st_rdev = in.rdev;
93+
out->st_size = in.size;
94+
out->st_blksize = in.blksize;
95+
out->st_blocks = in.blocks;
96+
out->st_atime = in.atime;
97+
out->st_mtime = in.mtime;
98+
out->st_ctime = in.ctime;
99+
}
100+
101+
} // namespace dwarfs

include/dwarfs/file_type.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* vim:set ts=2 sw=2 sts=2 et: */
2+
/**
3+
* \author Marcus Holland-Moritz ([email protected])
4+
* \copyright Copyright (c) Marcus Holland-Moritz
5+
*
6+
* This file is part of dwarfs.
7+
*
8+
* dwarfs is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* dwarfs is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
#pragma once
23+
24+
#include <cstdint>
25+
#include <filesystem>
26+
27+
namespace dwarfs {
28+
29+
struct posix_file_type {
30+
enum value : uint16_t {
31+
mask = 0170000,
32+
socket = 0140000,
33+
symlink = 0120000,
34+
regular = 0100000,
35+
block = 0060000,
36+
directory = 0040000,
37+
character = 0020000,
38+
fifo = 0010000,
39+
};
40+
};
41+
42+
std::filesystem::file_status file_mode_to_status(uint16_t mode);
43+
uint16_t file_status_to_mode(std::filesystem::file_status status);
44+
45+
} // namespace dwarfs

include/dwarfs/filesystem_v2.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,20 @@
3232
#include <string>
3333
#include <utility>
3434

35-
#include <sys/types.h>
36-
3735
#include <folly/Expected.h>
3836
#include <folly/dynamic.h>
3937

4038
#include "dwarfs/fstypes.h"
4139
#include "dwarfs/metadata_types.h"
4240

43-
struct stat;
44-
struct statvfs;
45-
4641
namespace dwarfs {
4742

4843
struct cache_tidy_config;
4944
struct filesystem_options;
5045
struct rewrite_options;
5146
struct iovec_read_buf;
47+
struct file_stat;
48+
struct vfs_stat;
5249

5350
class filesystem_writer;
5451
class logger;
@@ -107,7 +104,7 @@ class filesystem_v2 {
107104
return impl_->find(inode, name);
108105
}
109106

110-
int getattr(inode_view entry, struct ::stat* stbuf) const {
107+
int getattr(inode_view entry, file_stat* stbuf) const {
111108
return impl_->getattr(entry, stbuf);
112109
}
113110

@@ -134,7 +131,7 @@ class filesystem_v2 {
134131
return impl_->readlink(entry);
135132
}
136133

137-
int statvfs(struct ::statvfs* stbuf) const { return impl_->statvfs(stbuf); }
134+
int statvfs(vfs_stat* stbuf) const { return impl_->statvfs(stbuf); }
138135

139136
int open(inode_view entry) const { return impl_->open(entry); }
140137

@@ -176,7 +173,7 @@ class filesystem_v2 {
176173
virtual std::optional<inode_view> find(int inode) const = 0;
177174
virtual std::optional<inode_view>
178175
find(int inode, const char* name) const = 0;
179-
virtual int getattr(inode_view entry, struct ::stat* stbuf) const = 0;
176+
virtual int getattr(inode_view entry, file_stat* stbuf) const = 0;
180177
virtual int
181178
access(inode_view entry, int mode, uid_t uid, gid_t gid) const = 0;
182179
virtual std::optional<directory_view> opendir(inode_view entry) const = 0;
@@ -186,7 +183,7 @@ class filesystem_v2 {
186183
virtual int readlink(inode_view entry, std::string* buf) const = 0;
187184
virtual folly::Expected<std::string, int>
188185
readlink(inode_view entry) const = 0;
189-
virtual int statvfs(struct ::statvfs* stbuf) const = 0;
186+
virtual int statvfs(vfs_stat* stbuf) const = 0;
190187
virtual int open(inode_view entry) const = 0;
191188
virtual ssize_t
192189
read(uint32_t inode, char* buf, size_t size, off_t offset) const = 0;

include/dwarfs/metadata_v2.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,19 @@
3232
#include <utility>
3333
#include <vector>
3434

35-
#include <sys/types.h>
36-
3735
#include <folly/Expected.h>
3836
#include <folly/dynamic.h>
3937

4038
#include "dwarfs/metadata_types.h"
4139

42-
struct stat;
43-
struct statvfs;
44-
4540
namespace dwarfs {
4641

4742
class logger;
4843

4944
struct metadata_options;
50-
5145
struct filesystem_info;
46+
struct file_stat;
47+
struct vfs_stat;
5248

5349
namespace thrift::metadata {
5450
class metadata;
@@ -98,7 +94,7 @@ class metadata_v2 {
9894
return impl_->find(inode, name);
9995
}
10096

101-
int getattr(inode_view iv, struct ::stat* stbuf) const {
97+
int getattr(inode_view iv, file_stat* stbuf) const {
10298
return impl_->getattr(iv, stbuf);
10399
}
104100

@@ -127,7 +123,7 @@ class metadata_v2 {
127123
return impl_->readlink(iv);
128124
}
129125

130-
int statvfs(struct ::statvfs* stbuf) const { return impl_->statvfs(stbuf); }
126+
int statvfs(vfs_stat* stbuf) const { return impl_->statvfs(stbuf); }
131127

132128
std::optional<chunk_range> get_chunks(int inode) const {
133129
return impl_->get_chunks(inode);
@@ -163,7 +159,7 @@ class metadata_v2 {
163159
virtual std::optional<inode_view>
164160
find(int inode, const char* name) const = 0;
165161

166-
virtual int getattr(inode_view iv, struct ::stat* stbuf) const = 0;
162+
virtual int getattr(inode_view iv, file_stat* stbuf) const = 0;
167163

168164
virtual std::optional<directory_view> opendir(inode_view iv) const = 0;
169165

@@ -180,7 +176,7 @@ class metadata_v2 {
180176

181177
virtual folly::Expected<std::string, int> readlink(inode_view iv) const = 0;
182178

183-
virtual int statvfs(struct ::statvfs* stbuf) const = 0;
179+
virtual int statvfs(vfs_stat* stbuf) const = 0;
184180

185181
virtual std::optional<chunk_range> get_chunks(int inode) const = 0;
186182

0 commit comments

Comments
 (0)