MPD  0.20.18
FileInfo.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 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 "check.h"
24 #include "Path.hxx"
25 #include "system/Error.hxx"
26 
27 #include <stdint.h>
28 
29 #ifdef _WIN32
30 #include <fileapi.h>
31 #else
32 #include <sys/stat.h>
33 #endif
34 
35 #ifdef _WIN32
36 
37 static inline constexpr uint64_t
38 ConstructUint64(DWORD lo, DWORD hi)
39 {
40  return uint64_t(lo) | (uint64_t(hi) << 32);
41 }
42 
43 static constexpr time_t
44 FileTimeToTimeT(FILETIME ft)
45 {
46  return (ConstructUint64(ft.dwLowDateTime, ft.dwHighDateTime)
47  - 116444736000000000) / 10000000;
48 }
49 
50 #endif
51 
52 class FileInfo {
53  friend bool GetFileInfo(Path path, FileInfo &info,
54  bool follow_symlinks);
55  friend class FileReader;
56 
57 #ifdef _WIN32
58  WIN32_FILE_ATTRIBUTE_DATA data;
59 #else
60  struct stat st;
61 #endif
62 
63 public:
64  FileInfo() = default;
65 
66  FileInfo(Path path, bool follow_symlinks=true) {
67  if (!GetFileInfo(path, *this, follow_symlinks)) {
68 #ifdef _WIN32
69  throw FormatLastError("Failed to access %s",
70  path.ToUTF8().c_str());
71 #else
72  throw FormatErrno("Failed to access %s",
73  path.ToUTF8().c_str());
74 #endif
75  }
76  }
77 
78  bool IsRegular() const {
79 #ifdef _WIN32
80  return (data.dwFileAttributes &
81  (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE)) == 0;
82 #else
83  return S_ISREG(st.st_mode);
84 #endif
85  }
86 
87  bool IsDirectory() const {
88 #ifdef _WIN32
89  return data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
90 #else
91  return S_ISDIR(st.st_mode);
92 #endif
93  }
94 
95  uint64_t GetSize() const {
96 #ifdef _WIN32
97  return ConstructUint64(data.nFileSizeLow, data.nFileSizeHigh);
98 #else
99  return st.st_size;
100 #endif
101  }
102 
103  time_t GetModificationTime() const {
104 #ifdef _WIN32
105  return FileTimeToTimeT(data.ftLastWriteTime);
106 #else
107  return st.st_mtime;
108 #endif
109  }
110 
111 #ifndef _WIN32
112  uid_t GetUid() const {
113  return st.st_uid;
114  }
115 
116  mode_t GetMode() const {
117  return st.st_mode;
118  }
119 
120  dev_t GetDevice() const {
121  return st.st_dev;
122  }
123 
124  ino_t GetInode() const {
125  return st.st_ino;
126  }
127 #endif
128 };
129 
130 inline bool
131 GetFileInfo(Path path, FileInfo &info, bool follow_symlinks=true)
132 {
133 #ifdef _WIN32
134  (void)follow_symlinks;
135  return GetFileAttributesEx(path.c_str(), GetFileExInfoStandard,
136  &info.data);
137 #else
138  int ret = follow_symlinks
139  ? stat(path.c_str(), &info.st)
140  : lstat(path.c_str(), &info.st);
141  return ret == 0;
142 #endif
143 }
144 
145 #endif
bool IsRegular() const
Definition: FileInfo.hxx:78
dev_t GetDevice() const
Definition: FileInfo.hxx:120
uid_t GetUid() const
Definition: FileInfo.hxx:112
gcc_pure const_pointer_type c_str() const noexcept
Returns the value as a const C string.
Definition: Path.hxx:107
bool IsDirectory() const
Definition: FileInfo.hxx:87
friend bool GetFileInfo(Path path, FileInfo &info, bool follow_symlinks)
Definition: FileInfo.hxx:131
static std::system_error FormatErrno(int code, const char *fmt, Args &&... args)
Definition: Error.hxx:135
A path name in the native file system character set.
Definition: Path.hxx:39
time_t GetModificationTime() const
Definition: FileInfo.hxx:103
bool GetFileInfo(Path path, FileInfo &info, bool follow_symlinks=true)
Definition: FileInfo.hxx:131
uint64_t GetSize() const
Definition: FileInfo.hxx:95
ino_t GetInode() const
Definition: FileInfo.hxx:124
gcc_pure std::string ToUTF8() const noexcept
Convert the path to UTF-8.
FileInfo()=default
FileInfo(Path path, bool follow_symlinks=true)
Definition: FileInfo.hxx:66
mode_t GetMode() const
Definition: FileInfo.hxx:116