1 /*
2  * Copyright 2003-2021 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_FS_FILE_INFO_HXX
21 #define MPD_FS_FILE_INFO_HXX
22 
23 #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 #endif
31 
32 #include <chrono>
33 #include <cstdint>
34 
35 class FileInfo {
36 	friend bool GetFileInfo(Path path, FileInfo &info,
37 				bool follow_symlinks);
38 	friend class FileReader;
39 
40 #ifdef _WIN32
41 	WIN32_FILE_ATTRIBUTE_DATA data;
42 #else
43 	struct stat st;
44 #endif
45 
46 public:
47 	FileInfo() = default;
48 
FileInfo(Path path,bool follow_symlinks=true)49 	FileInfo(Path path, bool follow_symlinks=true) {
50 		if (!GetFileInfo(path, *this, follow_symlinks)) {
51 #ifdef _WIN32
52 			throw FormatLastError("Failed to access %s",
53 					      path.ToUTF8().c_str());
54 #else
55 			throw FormatErrno("Failed to access %s",
56 					  path.ToUTF8().c_str());
57 #endif
58 		}
59 	}
60 
IsRegular() const61 	bool IsRegular() const {
62 #ifdef _WIN32
63 		return (data.dwFileAttributes &
64 			(FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE)) == 0;
65 #else
66 		return S_ISREG(st.st_mode);
67 #endif
68 	}
69 
IsDirectory() const70 	bool IsDirectory() const {
71 #ifdef _WIN32
72 		return data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
73 #else
74 		return S_ISDIR(st.st_mode);
75 #endif
76 	}
77 
GetSize() const78 	uint64_t GetSize() const {
79 #ifdef _WIN32
80 		return ConstructUint64(data.nFileSizeLow, data.nFileSizeHigh);
81 #else
82 		return st.st_size;
83 #endif
84 	}
85 
GetModificationTime() const86 	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
GetUid() const95 	uid_t GetUid() const {
96 		return st.st_uid;
97 	}
98 
GetMode() const99 	mode_t GetMode() const {
100 		return st.st_mode;
101 	}
102 
GetDevice() const103 	dev_t GetDevice() const {
104 		return st.st_dev;
105 	}
106 
GetInode() const107 	ino_t GetInode() const {
108 		return st.st_ino;
109 	}
110 #endif
111 };
112 
113 inline bool
GetFileInfo(Path path,FileInfo & info,bool follow_symlinks=true)114 GetFileInfo(Path path, FileInfo &info, bool follow_symlinks=true)
115 {
116 #ifdef _WIN32
117 	(void)follow_symlinks;
118 	return GetFileAttributesEx(path.c_str(), GetFileExInfoStandard,
119 				   &info.data);
120 #else
121 	int ret = follow_symlinks
122 		? stat(path.c_str(), &info.st)
123 		: lstat(path.c_str(), &info.st);
124 	return ret == 0;
125 #endif
126 }
127 
128 #endif
129