llvm.org GIT mirror llvm / 69cce81
For PR789: * Add a method: bool isAbsolute() const, which determines if the path name is absolute or not. * Implement caching of file status information in the Path object. Allow it to be updated forcefully or lazily re-fetched from the cached value. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35456 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Spencer 13 years ago
3 changed file(s) with 67 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
122122 /// Find the path to a library using its short name. Use the system
123123 /// dependent library paths to locate the library.
124124 /// @brief Find a library.
125 static Path FindLibrary(std::string& short_name);
125 static Path FindLibrary(std::string& short_name);
126126
127127 /// Construct a path to the default LLVM configuration directory. The
128128 /// implementation must ensure that this is a well-known (same on many
161161 /// provided so that they can be used to indicate null or error results in
162162 /// other lib/System functionality.
163163 /// @brief Construct an empty (and invalid) path.
164 Path() : path() {}
164 Path() : path(), status(0) {}
165165
166166 /// This constructor will accept a std::string as a path. No checking is
167167 /// done on this path to determine if it is valid. To determine validity
168168 /// of the path, use the isValid method.
169169 /// @param p The path to assign.
170170 /// @brief Construct a Path from a string.
171 explicit Path(const std::string& p) : path(p) {}
171 explicit Path(const std::string& p) : path(p), status(0) {}
172172
173173 /// @}
174174 /// @name Operators
264264 /// @brief Determines if the path references the root directory.
265265 bool isRootDirectory() const;
266266
267 /// This function determines if the path name is absolute, as opposed to
268 /// relative.
269 /// @breif Determine if the path is absolute.
270 bool isAbsolute() const;
271
267272 /// This function opens the file associated with the path name provided by
268273 /// the Path object and reads its magic number. If the magic number at the
269274 /// start of the file matches \p magic, true is returned. In all other
351356 /// of the file system. This returns false on success, or true on error
352357 /// and fills in the specified error string if specified.
353358 /// @brief Get file status.
354 bool getFileStatus(FileStatus &Status, std::string *Error = 0) const;
359 bool getFileStatus(
360 FileStatus &Status, ///< The resulting file status
361 bool forceUpdate = false, ///< Force an update from the file system
362 std::string *Error = 0 ///< Optional place to return an error msg.
363 ) const;
355364
356365 /// @}
357366 /// @name Path Mutators
510519 /// @{
511520 private:
512521 mutable std::string path; ///< Storage for the path name.
522 mutable FileStatus *status; ///< Status information.
513523
514524 /// @}
515525 };
7878 return i >= len;
7979 }
8080
81 bool
82 Path::isAbsolute() const {
83 if (path.empty())
84 return false;
85 return path[0] == '/';
86 }
8187 Path
8288 Path::GetRootDirectory() {
8389 Path result;
356362 }
357363
358364 bool
359 Path::getFileStatus(FileStatus &info, std::string *ErrStr) const {
360 struct stat buf;
361 if (0 != stat(path.c_str(), &buf))
362 return MakeErrMsg(ErrStr,
363 path + ": can't get status of file '" + path + "'");
364 info.fileSize = buf.st_size;
365 info.modTime.fromEpochTime(buf.st_mtime);
366 info.mode = buf.st_mode;
367 info.user = buf.st_uid;
368 info.group = buf.st_gid;
369 info.isDir = S_ISDIR(buf.st_mode);
370 info.isFile = S_ISREG(buf.st_mode);
365 Path::getFileStatus(FileStatus &info, bool update, std::string *ErrStr) const {
366 if (status == 0 || update) {
367 struct stat buf;
368 if (0 != stat(path.c_str(), &buf))
369 return MakeErrMsg(ErrStr, path + ": can't get status of file");
370 if (status == 0)
371 status = new FileStatus;
372 status->fileSize = buf.st_size;
373 status->modTime.fromEpochTime(buf.st_mtime);
374 status->mode = buf.st_mode;
375 status->user = buf.st_uid;
376 status->group = buf.st_gid;
377 status->isDir = S_ISDIR(buf.st_mode);
378 status->isFile = S_ISREG(buf.st_mode);
379 }
380 info = *status;
371381 return false;
372382 }
373383
104104 return true;
105105 }
106106
107 bool
108 Path::isAbsolute() const {
109 if (path.length() < 3)
110 return false;
111 return path[0] == 'C' && path[1] == ':' && path[2] == '\\';
112 }
113
107114 static Path *TempDirectory = NULL;
108115
109116 Path
293300 }
294301
295302 bool
296 Path::getFileStatus(FileStatus &info, std::string *ErrStr) const {
297 WIN32_FILE_ATTRIBUTE_DATA fi;
298 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
299 return MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
300 ": Can't get status: ");
301
302 info.fileSize = fi.nFileSizeHigh;
303 info.fileSize <<= sizeof(fi.nFileSizeHigh)*8;
304 info.fileSize += fi.nFileSizeLow;
305
306 info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
307 info.user = 9999; // Not applicable to Windows, so...
308 info.group = 9999; // Not applicable to Windows, so...
309
310 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
311 info.modTime.fromWin32Time(ft);
312
313 info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
303 Path::getFileStatus(FileStatus &info, bool update, std::string *ErrStr) const {
304 if (status == 0 || update) {
305 WIN32_FILE_ATTRIBUTE_DATA fi;
306 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
307 return MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
308 ": Can't get status: ");
309
310 if (status == 0)
311 status = new FileStatus;
312
313 status->fileSize = fi.nFileSizeHigh;
314 status->fileSize <<= sizeof(fi.nFileSizeHigh)*8;
315 status->fileSize += fi.nFileSizeLow;
316
317 status->mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
318 status->user = 9999; // Not applicable to Windows, so...
319 status->group = 9999; // Not applicable to Windows, so...
320
321 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
322 status->modTime.fromWin32Time(ft);
323
324 status->isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
325 }
326 info = *status;
314327 return false;
315328 }
316329