FileInfo.hxx 2.74 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_FS_FILE_INFO_HXX
#define MPD_FS_FILE_INFO_HXX

#include "Path.hxx"
24
#include "system/Error.hxx"
25

26
#ifdef _WIN32
27
#include "time/FileTime.hxx"
28
#else
29
#include <sys/stat.h>
30 31
#endif

32
#include <chrono>
33
#include <cstdint>
34

35 36 37
class FileInfo {
	friend bool GetFileInfo(Path path, FileInfo &info,
				bool follow_symlinks);
38
	friend class FileReader;
39

40
#ifdef _WIN32
41 42
	WIN32_FILE_ATTRIBUTE_DATA data;
#else
43
	struct stat st;
44
#endif
45 46

public:
47 48 49 50
	FileInfo() = default;

	FileInfo(Path path, bool follow_symlinks=true) {
		if (!GetFileInfo(path, *this, follow_symlinks)) {
51
#ifdef _WIN32
52 53 54 55 56 57 58 59 60
			throw FormatLastError("Failed to access %s",
					      path.ToUTF8().c_str());
#else
			throw FormatErrno("Failed to access %s",
					  path.ToUTF8().c_str());
#endif
		}
	}

61
	bool IsRegular() const {
62
#ifdef _WIN32
63 64
		return (data.dwFileAttributes &
			(FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE)) == 0;
65
#else
66
		return S_ISREG(st.st_mode);
67
#endif
68 69 70
	}

	bool IsDirectory() const {
71
#ifdef _WIN32
72 73
		return data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
#else
74
		return S_ISDIR(st.st_mode);
75
#endif
76 77 78
	}

	uint64_t GetSize() const {
79
#ifdef _WIN32
80 81
		return ConstructUint64(data.nFileSizeLow, data.nFileSizeHigh);
#else
82
		return st.st_size;
83
#endif
84 85
	}

86
	std::chrono::system_clock::time_point GetModificationTime() const {
87
#ifdef _WIN32
88
		return FileTimeToChrono(data.ftLastWriteTime);
89
#else
90
		return std::chrono::system_clock::from_time_t(st.st_mtime);
91
#endif
92 93
	}

94
#ifndef _WIN32
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	uid_t GetUid() const {
		return st.st_uid;
	}

	mode_t GetMode() const {
		return st.st_mode;
	}

	dev_t GetDevice() const {
		return st.st_dev;
	}

	ino_t GetInode() const {
		return st.st_ino;
	}
#endif
};

inline bool
GetFileInfo(Path path, FileInfo &info, bool follow_symlinks=true)
{
116
#ifdef _WIN32
117
	(void)follow_symlinks;
118 119
	return GetFileAttributesEx(path.c_str(), GetFileExInfoStandard,
				   &info.data);
120 121 122 123 124 125 126 127 128
#else
	int ret = follow_symlinks
		? stat(path.c_str(), &info.st)
		: lstat(path.c_str(), &info.st);
	return ret == 0;
#endif
}

#endif