2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2015-02-28 20:42:50 +01:00
|
|
|
|
2023-10-07 10:19:56 +02:00
|
|
|
#pragma once
|
2015-02-28 20:42:50 +01:00
|
|
|
|
|
|
|
#include "Path.hxx"
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2019-05-08 15:47:04 +02:00
|
|
|
#include "time/FileTime.hxx"
|
2015-02-28 21:49:06 +01:00
|
|
|
#else
|
2015-02-28 20:42:50 +01:00
|
|
|
#include <sys/stat.h>
|
2015-02-28 21:49:06 +01:00
|
|
|
#endif
|
|
|
|
|
2017-02-10 23:44:51 +01:00
|
|
|
#include <chrono>
|
2020-03-13 01:08:53 +01:00
|
|
|
#include <cstdint>
|
2017-02-10 23:44:51 +01:00
|
|
|
|
2023-10-07 12:37:26 +02:00
|
|
|
class FileDescriptor;
|
|
|
|
|
2015-02-28 20:42:50 +01:00
|
|
|
class FileInfo {
|
|
|
|
friend bool GetFileInfo(Path path, FileInfo &info,
|
2023-10-07 10:19:56 +02:00
|
|
|
bool follow_symlinks) noexcept;
|
2023-10-07 12:11:38 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
friend bool GetFileInfoByHandle(HANDLE handle, FileInfo &info) noexcept;
|
|
|
|
#endif
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2015-02-28 21:49:06 +01:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA data;
|
|
|
|
#else
|
2015-02-28 20:42:50 +01:00
|
|
|
struct stat st;
|
2015-02-28 21:49:06 +01:00
|
|
|
#endif
|
2015-02-28 20:42:50 +01:00
|
|
|
|
|
|
|
public:
|
2023-10-07 10:19:56 +02:00
|
|
|
constexpr FileInfo() noexcept = default;
|
2015-12-16 11:05:33 +01:00
|
|
|
|
2023-10-07 12:26:51 +02:00
|
|
|
explicit FileInfo(Path path, bool follow_symlinks=true);
|
2015-12-16 11:05:33 +01:00
|
|
|
|
2023-10-07 12:11:38 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
explicit FileInfo(HANDLE handle);
|
2023-10-07 12:37:26 +02:00
|
|
|
#else
|
|
|
|
explicit FileInfo(FileDescriptor fd);
|
2023-10-07 12:11:38 +02:00
|
|
|
#endif
|
|
|
|
|
2023-10-07 10:19:56 +02:00
|
|
|
constexpr bool IsRegular() const noexcept {
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2015-03-05 09:43:51 +01:00
|
|
|
return (data.dwFileAttributes &
|
|
|
|
(FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE)) == 0;
|
2015-02-28 21:49:06 +01:00
|
|
|
#else
|
2015-02-28 20:42:50 +01:00
|
|
|
return S_ISREG(st.st_mode);
|
2015-02-28 21:49:06 +01:00
|
|
|
#endif
|
2015-02-28 20:42:50 +01:00
|
|
|
}
|
|
|
|
|
2023-10-07 10:19:56 +02:00
|
|
|
constexpr bool IsDirectory() const noexcept {
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2015-02-28 21:49:06 +01:00
|
|
|
return data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
#else
|
2015-02-28 20:42:50 +01:00
|
|
|
return S_ISDIR(st.st_mode);
|
2015-02-28 21:49:06 +01:00
|
|
|
#endif
|
2015-02-28 20:42:50 +01:00
|
|
|
}
|
|
|
|
|
2023-10-07 12:29:04 +02:00
|
|
|
constexpr uint_least64_t GetSize() const noexcept {
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2015-02-28 21:49:06 +01:00
|
|
|
return ConstructUint64(data.nFileSizeLow, data.nFileSizeHigh);
|
|
|
|
#else
|
2015-02-28 20:42:50 +01:00
|
|
|
return st.st_size;
|
2015-02-28 21:49:06 +01:00
|
|
|
#endif
|
2015-02-28 20:42:50 +01:00
|
|
|
}
|
|
|
|
|
2023-10-07 10:19:56 +02:00
|
|
|
[[gnu::pure]]
|
|
|
|
std::chrono::system_clock::time_point GetModificationTime() const noexcept {
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2017-02-10 23:44:51 +01:00
|
|
|
return FileTimeToChrono(data.ftLastWriteTime);
|
2015-02-28 21:49:06 +01:00
|
|
|
#else
|
2017-02-10 23:44:51 +01:00
|
|
|
return std::chrono::system_clock::from_time_t(st.st_mtime);
|
2015-02-28 21:49:06 +01:00
|
|
|
#endif
|
2015-02-28 20:42:50 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifndef _WIN32
|
2023-10-07 10:19:56 +02:00
|
|
|
constexpr uid_t GetUid() const noexcept {
|
2015-02-28 20:42:50 +01:00
|
|
|
return st.st_uid;
|
|
|
|
}
|
|
|
|
|
2023-10-07 10:19:56 +02:00
|
|
|
constexpr mode_t GetMode() const noexcept {
|
2015-02-28 20:42:50 +01:00
|
|
|
return st.st_mode;
|
|
|
|
}
|
|
|
|
|
2023-10-07 10:19:56 +02:00
|
|
|
constexpr dev_t GetDevice() const noexcept {
|
2015-02-28 20:42:50 +01:00
|
|
|
return st.st_dev;
|
|
|
|
}
|
|
|
|
|
2023-10-07 10:19:56 +02:00
|
|
|
constexpr ino_t GetInode() const noexcept {
|
2015-02-28 20:42:50 +01:00
|
|
|
return st.st_ino;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool
|
2023-10-07 10:19:56 +02:00
|
|
|
GetFileInfo(Path path, FileInfo &info, bool follow_symlinks=true) noexcept
|
2015-02-28 20:42:50 +01:00
|
|
|
{
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2015-02-28 20:42:50 +01:00
|
|
|
(void)follow_symlinks;
|
2015-02-28 21:49:06 +01:00
|
|
|
return GetFileAttributesEx(path.c_str(), GetFileExInfoStandard,
|
|
|
|
&info.data);
|
2015-02-28 20:42:50 +01:00
|
|
|
#else
|
|
|
|
int ret = follow_symlinks
|
|
|
|
? stat(path.c_str(), &info.st)
|
|
|
|
: lstat(path.c_str(), &info.st);
|
|
|
|
return ret == 0;
|
|
|
|
#endif
|
|
|
|
}
|
2023-10-07 12:11:38 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
bool
|
|
|
|
GetFileInfoByHandle(HANDLE handle, FileInfo &info) noexcept;
|
|
|
|
|
|
|
|
#endif
|