RStudio Launcher Plugin SDK  1.1.3
A software development kit for creating plugins that work the the RStudio Launcher.
FilePath.hpp
1 /*
2  * FilePath.hpp
3  *
4  * Copyright (C) 2022 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant to the terms of a commercial license agreement
7  * with RStudio, then this program is licensed to you under the following terms:
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
10  * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
11  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #ifndef LAUNCHER_PLUGINS_FILE_PATH_HPP
25 #define LAUNCHER_PLUGINS_FILE_PATH_HPP
26 
27 #include <Noncopyable.hpp>
28 
29 #include <cstdint>
30 #include <ctime>
31 
32 #include <functional>
33 #include <iosfwd>
34 #include <memory>
35 #include <string>
36 #include <vector>
37 
38 #include <Error.hpp>
39 #include <PImpl.hpp>
40 #include <logging/Logger.hpp>
41 
42 namespace rstudio {
43 namespace launcher_plugins {
44 namespace system {
45 
46 class User;
47 
48 } // namespace system
49 } // namespace launcher_plugins
50 } // namespace rstudio
51 
52 namespace rstudio {
53 namespace launcher_plugins {
54 namespace system {
55 
60 enum class FileMode
61 {
62  USER_READ_WRITE,
63  USER_READ_WRITE_EXECUTE,
64  USER_READ_WRITE_GROUP_READ,
65  USER_READ_WRITE_ALL_READ,
66  USER_READ_WRITE_EXECUTE_ALL_READ_EXECUTE,
67  USER_READ_WRITE_EXECUTE_GROUP_READ_WRITE_EXECUTE_ALL_READ_EXECUTE,
68  ALL_READ,
69  ALL_READ_WRITE,
70  ALL_READ_WRITE_EXECUTE
71 };
72 
77 class FilePath
78 {
79 public:
83  enum MoveType
84  {
87 
90  };
91 
100  typedef std::function<bool(int, const FilePath&)> RecursiveIterationFunction;
101 
105  FilePath();
106 
112  explicit FilePath(const std::string& in_absolutePath);
113 
119  explicit FilePath(const char* in_absolutePath);
120 
128  bool operator==(const FilePath& in_other) const;
129 
137  bool operator!=(const FilePath& in_other) const;
138 
148  bool operator<(const FilePath& in_other) const;
149 
158  static std::string createAliasedPath(const FilePath& in_filePath, const FilePath& in_userHomePath);
159 
167  static bool exists(const std::string& in_filePath);
168 
178  static bool isEqualCaseInsensitive(const FilePath& in_filePath1, const FilePath& in_filePath2);
179 
187  static bool isRootPath(const std::string& in_filePath);
188 
196  static Error makeCurrent(const std::string& in_filePath);
197 
206  static FilePath resolveAliasedPath(const std::string& in_aliasedPath, const FilePath& in_userHomePath);
207 
216  static FilePath safeCurrentPath(const FilePath& in_revertToPath);
217 
226  static Error tempFilePath(FilePath& out_filePath);
227 
238  static Error tempFilePath(const std::string& in_extension, FilePath& out_filePath);
239 
249  static Error uniqueFilePath(const std::string& in_basePath, FilePath& out_filePath);
250 
262  static Error uniqueFilePath(const std::string& in_basePath, const std::string& in_extension, FilePath& out_filePath);
263 
271  Error changeFileMode(const std::string& in_fileModeStr) const;
272 
281  Error changeFileMode(FileMode in_fileMode, bool in_setStickyBit = false) const;
282 
297  const system::User& in_newUser,
298  bool in_recursive = false,
299  const RecursiveIterationFunction& in_shouldChown = RecursiveIterationFunction()) const;
300 
308  FilePath completeChildPath(const std::string& in_filePath) const;
309 
320  Error completeChildPath(const std::string& in_filePath, FilePath& out_childPath) const;
321 
330  FilePath completePath(const std::string& in_filePath) const;
331 
340  Error copy(const FilePath& in_targetPath, bool overwrite = false) const;
341 
350  Error copyDirectoryRecursive(const FilePath& in_targetPath, bool overwrite = false) const;
351 
359  Error createDirectory(const std::string& in_filePath) const;
360 
366  Error ensureDirectory() const;
367 
373  Error ensureFile() const;
374 
380  bool exists() const;
381 
387  std::string getAbsolutePath() const;
388 
394  std::string getAbsolutePathNative() const;
395 
402  std::string getCanonicalPath() const;
403 
411  Error getChildren(std::vector<FilePath>& out_filePaths) const;
412 
420  Error getChildrenRecursive(const RecursiveIterationFunction& in_iterationFunction) const;
421 
427  std::string getExtension() const;
428 
434  std::string getExtensionLowerCase() const;
435 
443  Error getFileMode(FileMode& out_fileMode) const;
444 
450  std::string getFilename() const;
451 
457  std::time_t getLastWriteTime() const;
458 
465  std::string getLexicallyNormalPath() const;
466 
475  std::string getMimeContentType(const std::string& in_defaultType = "text/plain") const;
476 
482  FilePath getParent() const;
483 
492  std::string getRelativePath(const FilePath& in_parentPath) const;
493 
499  uintmax_t getSize() const;
500 
506  uintmax_t getSizeRecursive() const;
507 
513  std::string getStem() const;
514 
522  bool hasExtension(const std::string& in_extension) const;
523 
531  bool hasExtensionLowerCase(const std::string& in_extension) const;
532 
538  bool hasTextMimeType() const;
539 
545  bool isDirectory() const;
546 
552  bool isEmpty() const;
553 
562  bool isEquivalentTo(const FilePath& in_other) const;
563 
569  bool isHidden() const;
570 
579  Error isReadable(bool& out_readable) const;
580 
586  bool isRegularFile() const;
587 
593  bool isSymlink() const;
594 
602  bool isWithin(const FilePath& in_scopePath) const;
603 
612  Error isWriteable(bool& out_writeable) const;
613 
622  Error makeCurrentPath(bool in_autoCreate = false) const;
623 
634  Error move(const FilePath& in_targetPath, MoveType in_type = MoveCrossDevice, bool overwrite = false) const;
635 
644  Error moveIndirect(const FilePath& in_targetPath, bool overwrite = false) const;
645 
653  Error openForRead(std::shared_ptr<std::istream>& out_stream) const;
654 
663  Error openForWrite(std::shared_ptr<std::ostream>& out_stream, bool in_truncate = true) const;
664 
670  Error remove() const;
671 
677  Error removeIfExists() const;
678 
684  Error resetDirectory() const;
685 
692  FilePath resolveSymlink() const;
693 
699  void setLastWriteTime(std::time_t in_time = ::time(nullptr)) const;
700 
711  Error testWritePermissions() const;
712 
713 private:
714  // The private implementation of FilePath.
715  PRIVATE_IMPL_SHARED(m_impl);
716 };
717 
721 struct PathScopeImpl;
722 
727 {
731  void operator()(PathScopeImpl*);
732 };
733 
738 {
739 public:
747  RestoreCurrentPathScope(FilePath in_restorePath, ErrorLocation in_location);
748 
752  virtual ~RestoreCurrentPathScope();
753 
754 private:
755  // The private implementation of RestoreCurrentPathScope
756  std::unique_ptr<PathScopeImpl, PathScopeImplDeleter> m_impl;
757 };
758 
763 {
764 public:
772  RemoveOnExitScope(FilePath in_restorePath, ErrorLocation in_location);
773 
777  virtual ~RemoveOnExitScope();
778 
779 private:
780  // The private implementation of RemoveOnExitScope
781  std::unique_ptr<PathScopeImpl, PathScopeImplDeleter> m_impl;
782 };
783 
792 std::ostream& operator<<(std::ostream& io_ostream, const FilePath& in_filePath);
793 
801 Error fileExistsError(const ErrorLocation& in_location);
802 
811 Error fileExistsError(const FilePath& in_filePath, const ErrorLocation& in_location);
812 
820 bool isFileNotFoundError(const Error& in_error);
821 
829 Error fileNotFoundError(const ErrorLocation& in_location);
830 
839 Error fileNotFoundError(const std::string& in_filePath, const ErrorLocation& in_location);
840 
849 Error fileNotFoundError(const FilePath& in_filePath, const ErrorLocation& in_location);
850 
858 bool isPathNotFoundError(const Error& error);
859 
867 Error pathNotFoundError(const ErrorLocation& in_location);
868 
877 Error pathNotFoundError(const std::string& in_filePath, const ErrorLocation& in_location);
878 
886 bool isNotFoundError(const Error& in_error);
887 
888 } // namespace system
889 } // namespace launcher_plugins
890 } // namespace rstudio
891 
892 #endif
rstudio::launcher_plugins::system::FilePath::remove
Error remove() const
Removes this file or directory from the filesystem.
rstudio::launcher_plugins::system::FilePath::createAliasedPath
static std::string createAliasedPath(const FilePath &in_filePath, const FilePath &in_userHomePath)
Creates a path in which the user home path will be replaced by the ~ alias.
rstudio::launcher_plugins::system::FilePath::isEquivalentTo
bool isEquivalentTo(const FilePath &in_other) const
Checks whether this file path points to the same location in the filesystem as the specified file pat...
rstudio::launcher_plugins::system::PathScopeImplDeleter
Struct which implements the deleter for PathScopeImpl.
Definition: FilePath.hpp:726
rstudio::launcher_plugins::system::FilePath::getLastWriteTime
std::time_t getLastWriteTime() const
Get the last time this file path was written.
rstudio::launcher_plugins::system::FilePath::isHidden
bool isHidden() const
Checks whether this file path is a hidden file or directory.
rstudio::launcher_plugins::system::FilePath::resolveAliasedPath
static FilePath resolveAliasedPath(const std::string &in_aliasedPath, const FilePath &in_userHomePath)
Resolves the '~' alias within the path to the user's home path.
rstudio::launcher_plugins::system::FilePath::uniqueFilePath
static Error uniqueFilePath(const std::string &in_basePath, FilePath &out_filePath)
Creates a file with a random name in the specified directory.
rstudio::launcher_plugins::system::FilePath::getSizeRecursive
uintmax_t getSizeRecursive() const
Gets the size of this file path and all sub-directories and files in it, in bytes.
rstudio::launcher_plugins::system::FilePath::resolveSymlink
FilePath resolveSymlink() const
Resolves this symbolic link to the location to which it is pointing. If this FilePath is not a symbol...
rstudio::launcher_plugins::system::FilePath
Class which represents a path on the system. May be any type of file (e.g. directory,...
Definition: FilePath.hpp:77
rstudio::launcher_plugins::system::FilePath::getSize
uintmax_t getSize() const
Gets the size of this file path in bytes.
rstudio::launcher_plugins::ErrorLocation
Class which represents the location of an error.
Definition: Error.hpp:74
rstudio::launcher_plugins::system::FilePath::makeCurrentPath
Error makeCurrentPath(bool in_autoCreate=false) const
Changes the current working directory to location represented by this file path.
rstudio::launcher_plugins::system::FilePath::ensureDirectory
Error ensureDirectory() const
Creates this directory, if it does not exist.
rstudio::launcher_plugins::system::FilePath::MoveDirect
Definition: FilePath.hpp:86
rstudio::launcher_plugins::system::RemoveOnExitScope::RemoveOnExitScope
RemoveOnExitScope(FilePath in_restorePath, ErrorLocation in_location)
Constructor.
rstudio::launcher_plugins::system::FilePath::isDirectory
bool isDirectory() const
Checks whether this file path is a directory.
rstudio::launcher_plugins::system::User
Class which represents a system user.
Definition: User.hpp:55
rstudio::launcher_plugins::system::RestoreCurrentPathScope
RAII class for restoring the current working directory.
Definition: FilePath.hpp:737
rstudio::launcher_plugins::system::FilePath::ensureFile
Error ensureFile() const
Creates this file, if it does not exist.
rstudio::launcher_plugins::system::RemoveOnExitScope::~RemoveOnExitScope
virtual ~RemoveOnExitScope()
Destructor. Removes the path that was provided in the constructor from the filesystem.
rstudio::launcher_plugins::system::FilePath::getAbsolutePathNative
std::string getAbsolutePathNative() const
Gets the full absolute representation of this file path in native format.
rstudio::launcher_plugins::system::FilePath::changeFileMode
Error changeFileMode(const std::string &in_fileModeStr) const
Changes the file mode to the specified file mode.
rstudio::launcher_plugins::system::RestoreCurrentPathScope::RestoreCurrentPathScope
RestoreCurrentPathScope(FilePath in_restorePath, ErrorLocation in_location)
Constructor.
rstudio::launcher_plugins::system::FilePath::isWithin
bool isWithin(const FilePath &in_scopePath) const
Checks whether this file path is within the specified file path.
rstudio::launcher_plugins::system::FilePath::getCanonicalPath
std::string getCanonicalPath() const
Gets the canonical representation of this file path. The path must exist so that its canonical locati...
rstudio::launcher_plugins::system::FilePath::getLexicallyNormalPath
std::string getLexicallyNormalPath() const
Gets the lexically normal representation of this file path, with . and .. components resolved and/or ...
rstudio::launcher_plugins::system::FilePath::moveIndirect
Error moveIndirect(const FilePath &in_targetPath, bool overwrite=false) const
Performs an indirect move by copying this directory to the target and then deleting this directory.
rstudio::launcher_plugins::system::FilePath::resetDirectory
Error resetDirectory() const
Removes the directory represented by this FilePath, if it exists, and recreates it.
rstudio::launcher_plugins::system::FilePath::RecursiveIterationFunction
std::function< bool(int, const FilePath &)> RecursiveIterationFunction
Function which recursively iterates over FilePath objects.
Definition: FilePath.hpp:100
rstudio::launcher_plugins::system::FilePath::isReadable
Error isReadable(bool &out_readable) const
Checks whether this file path is readable.
rstudio::launcher_plugins::system::FilePath::operator<
bool operator<(const FilePath &in_other) const
Less-than operator to establish natural order. The natural order is based on the absolute representat...
rstudio::launcher_plugins::system::FilePath::completeChildPath
FilePath completeChildPath(const std::string &in_filePath) const
Gets the provided relative path as a child of this path.
rstudio::launcher_plugins::system::FilePath::isSymlink
bool isSymlink() const
Checks whether this file path is a symbolic link.
rstudio::launcher_plugins::system::FilePath::MoveType
MoveType
Enum which represents the type of move to perform.
Definition: FilePath.hpp:83
rstudio::launcher_plugins::system::FilePath::openForRead
Error openForRead(std::shared_ptr< std::istream > &out_stream) const
Opens this file for read.
rstudio::launcher_plugins::system::FilePath::isEmpty
bool isEmpty() const
Checks whether this file path contains a path or not.
rstudio::launcher_plugins::system::FilePath::safeCurrentPath
static FilePath safeCurrentPath(const FilePath &in_revertToPath)
Checks whether the current working directory exists. If it does not, moves the current working direct...
rstudio::launcher_plugins::system::FilePath::operator!=
bool operator!=(const FilePath &in_other) const
Comparison operator. File paths are equal if their absolute representations are equal.
rstudio::launcher_plugins::system::FilePath::getChildren
Error getChildren(std::vector< FilePath > &out_filePaths) const
Gets the children of this directory. Sub-directories will not be traversed.
rstudio::launcher_plugins::system::RestoreCurrentPathScope::~RestoreCurrentPathScope
virtual ~RestoreCurrentPathScope()
Destructor. Returns the working directory to the original path.
rstudio::launcher_plugins::system::FilePath::tempFilePath
static Error tempFilePath(FilePath &out_filePath)
Creates a randomly named file in the temp directory.
rstudio::launcher_plugins::system::RemoveOnExitScope
RAII class for restoring the current working directory.
Definition: FilePath.hpp:762
rstudio::launcher_plugins::system::FilePath::getRelativePath
std::string getRelativePath(const FilePath &in_parentPath) const
Gets the representation of this path, relative to the provided path.
rstudio::launcher_plugins::system::FilePath::changeOwnership
Error changeOwnership(const system::User &in_newUser, bool in_recursive=false, const RecursiveIterationFunction &in_shouldChown=RecursiveIterationFunction()) const
Changes the ownership of the file or directory to the specified user.
rstudio::launcher_plugins::system::FilePath::getAbsolutePath
std::string getAbsolutePath() const
Gets the full absolute representation of this file path.
rstudio::launcher_plugins::system::FilePath::hasTextMimeType
bool hasTextMimeType() const
Checks whether this file has a text mime content type.
rstudio::launcher_plugins::system::FilePath::isRootPath
static bool isRootPath(const std::string &in_filePath)
Checks whether the specified path is a root path or a relative path.
rstudio::launcher_plugins::system::FilePath::openForWrite
Error openForWrite(std::shared_ptr< std::ostream > &out_stream, bool in_truncate=true) const
Opens this file for write.
rstudio::launcher_plugins::system::FilePath::hasExtensionLowerCase
bool hasExtensionLowerCase(const std::string &in_extension) const
Checks whether this file has the specified extension when it is converted to lower case.
rstudio::launcher_plugins::system::PathScopeImplDeleter::operator()
void operator()(PathScopeImpl *)
Deletion operator.
rstudio::launcher_plugins::system::FilePath::MoveCrossDevice
Definition: FilePath.hpp:89
rstudio::launcher_plugins::system::FilePath::getFileMode
Error getFileMode(FileMode &out_fileMode) const
Gets the posix file mode of this file or directory.
rstudio::launcher_plugins::system::FilePath::completePath
FilePath completePath(const std::string &in_filePath) const
Completes the provided path relative to this path. If the provided path is not relative,...
rstudio::launcher_plugins::system::FilePath::removeIfExists
Error removeIfExists() const
Removes this file or directory from the filesystem, if it exists.
rstudio::launcher_plugins::Error
Class which represents an error.
Definition: Error.hpp:174
rstudio::launcher_plugins::system::FilePath::isEqualCaseInsensitive
static bool isEqualCaseInsensitive(const FilePath &in_filePath1, const FilePath &in_filePath2)
Checks whether the two provided files are equal, ignoring case. Two files are equal if their absolute...
rstudio::launcher_plugins::system::FilePath::isWriteable
Error isWriteable(bool &out_writeable) const
Checks whether this file path is writeable.
Logger.hpp
rstudio::launcher_plugins::system::FilePath::createDirectory
Error createDirectory(const std::string &in_filePath) const
Creates the specified directory.
rstudio::launcher_plugins::system::FilePath::getFilename
std::string getFilename() const
Gets only the name of the file, including the extension.
rstudio::launcher_plugins::system::FilePath::getParent
FilePath getParent() const
Gets the parent directory of this file path.
rstudio::launcher_plugins::system::FilePath::hasExtension
bool hasExtension(const std::string &in_extension) const
Checks whether this file has the specified extension.
rstudio::launcher_plugins::system::FilePath::getExtensionLowerCase
std::string getExtensionLowerCase() const
Gets the extension of the file in lower case, including the leading '.'.
rstudio::launcher_plugins::system::FilePath::setLastWriteTime
void setLastWriteTime(std::time_t in_time=::time(nullptr)) const
Sets the last time that this file was modified to the specified time.
rstudio::launcher_plugins::system::FilePath::getStem
std::string getStem() const
Gets only the name of the file, excluding the extension.
rstudio::launcher_plugins::Noncopyable
Class which can be inherited from to disallow copying of its child classes.
Definition: Noncopyable.hpp:34
rstudio::launcher_plugins::system::FilePath::copyDirectoryRecursive
Error copyDirectoryRecursive(const FilePath &in_targetPath, bool overwrite=false) const
Copies this directory recursively to the specified location.
rstudio::launcher_plugins::system::FilePath::operator==
bool operator==(const FilePath &in_other) const
Comparison operator. File paths are equal if their absolute representations are equal.
rstudio::launcher_plugins::system::FilePath::copy
Error copy(const FilePath &in_targetPath, bool overwrite=false) const
Copies this file path to the specified location.
rstudio::launcher_plugins::system::FilePath::getMimeContentType
std::string getMimeContentType(const std::string &in_defaultType="text/plain") const
Gets the mime content type of this file.
rstudio::launcher_plugins::system::FilePath::exists
bool exists() const
Checks whether this file path exists in the file system.
rstudio::launcher_plugins::system::FilePath::getExtension
std::string getExtension() const
Gets the extension of the file, including the leading '.'.
rstudio::launcher_plugins::system::FilePath::FilePath
FilePath()
Default constructor.
rstudio::launcher_plugins::system::FilePath::makeCurrent
static Error makeCurrent(const std::string &in_filePath)
Changes the current working directory to the specified path.
rstudio::launcher_plugins::system::FilePath::testWritePermissions
Error testWritePermissions() const
Checks if a file can be written to by opening the file.
rstudio::launcher_plugins::system::FilePath::getChildrenRecursive
Error getChildrenRecursive(const RecursiveIterationFunction &in_iterationFunction) const
Gets the children of this directory recursively. Sub-directories will be traversed.
rstudio::launcher_plugins::system::FilePath::move
Error move(const FilePath &in_targetPath, MoveType in_type=MoveCrossDevice, bool overwrite=false) const
Moves the current directory to the specified directory.
rstudio::launcher_plugins::system::FilePath::isRegularFile
bool isRegularFile() const
Checks whether this file path is a regular file.