src/jdk.jpackage/windows/native/libjpackage/FileUtils.h
branchJDK-8200758-branch
changeset 57909 c7de06ed4b54
parent 57413 45c74e654794
equal deleted inserted replaced
57908:9a005146bc1c 57909:c7de06ed4b54
    41 
    41 
    42     // checks is the specified file is a directory
    42     // checks is the specified file is a directory
    43     // returns false if the path does not exist
    43     // returns false if the path does not exist
    44     bool isDirectory(const tstring &filePath);
    44     bool isDirectory(const tstring &filePath);
    45 
    45 
    46     //checks if the specified directory is not empty
    46     // checks if the specified directory is not empty
    47     //returns true if the path is an existing directory and it contains at least one file other than "." or "..".
    47     // returns true if the path is an existing directory and
       
    48     // it contains at least one file other than "." or "..".
    48     bool isDirectoryNotEmpty(const tstring &dirPath);
    49     bool isDirectoryNotEmpty(const tstring &dirPath);
    49 
    50 
    50     // returns directory part of the path.
    51     // returns directory part of the path.
    51     // returns empty string if the path contains only filename.
    52     // returns empty string if the path contains only filename.
    52     // if the path ends with slash/backslash, returns removeTrailingSlashes(path).
    53     // if the path ends with slash/backslash,
       
    54     // returns removeTrailingSlashes(path).
    53     tstring dirname(const tstring &path);
    55     tstring dirname(const tstring &path);
    54 
    56 
    55     // returns basename part of the path
    57     // returns basename part of the path
    56     // if the path ends with slash/backslash, returns empty string.
    58     // if the path ends with slash/backslash, returns empty string.
    57     tstring basename(const tstring &path);
    59     tstring basename(const tstring &path);
    71     tstring combinePath(const tstring& parent, const tstring& child);
    73     tstring combinePath(const tstring& parent, const tstring& child);
    72 
    74 
    73     // removes trailing slashes and backslashes in the path if any
    75     // removes trailing slashes and backslashes in the path if any
    74     tstring removeTrailingSlash(const tstring& path);
    76     tstring removeTrailingSlash(const tstring& path);
    75 
    77 
    76     // Creates a file with unique name in the specified base directory, throws an exception if operation fails
    78     // Creates a file with unique name in the specified base directory,
       
    79     // throws an exception if operation fails
    77     // path is constructed as <prefix><random number><suffix>.
    80     // path is constructed as <prefix><random number><suffix>.
    78     // The function fails and throws exception if 'path' doesn't exist.
    81     // The function fails and throws exception if 'path' doesn't exist.
    79     tstring createTempFile(const tstring &prefix = _T(""), const tstring &suffix = _T(".tmp"), const tstring &path=SysInfo::getTempDir());
    82     tstring createTempFile(const tstring &prefix = _T(""),
    80 
    83             const tstring &suffix = _T(".tmp"),
    81     // Creates a directory with unique name in the specified base directory, throws an exception if operation fails
    84             const tstring &path=SysInfo::getTempDir());
       
    85 
       
    86     // Creates a directory with unique name in the specified base directory,
       
    87     // throws an exception if operation fails
    82     // path is constructed as <prefix><random number><suffix>
    88     // path is constructed as <prefix><random number><suffix>
    83     // The function fails and throws exception if 'path' doesn't exist.
    89     // The function fails and throws exception if 'path' doesn't exist.
    84     tstring createTempDirectory(const tstring &prefix = _T(""), const tstring &suffix = _T(".tmp"), const tstring &basedir=SysInfo::getTempDir());
    90     tstring createTempDirectory(const tstring &prefix = _T(""),
    85 
    91             const tstring &suffix = _T(".tmp"),
    86     // If the file referenced with "prototype" parameter DOES NOT exist, the return
    92             const tstring &basedir=SysInfo::getTempDir());
    87     // value is the given path. No new files created.
    93 
       
    94     // If the file referenced with "prototype" parameter DOES NOT exist,
       
    95     // the return value is the given path. No new files created.
    88     // Otherwise the function creates another file in the same directory as
    96     // Otherwise the function creates another file in the same directory as
    89     // the given file with the same suffix and with the basename from the
    97     // the given file with the same suffix and with the basename from the
    90     // basename of the given file with some random chars appended to ensure
    98     // basename of the given file with some random chars appended to ensure
    91     // created file is unique.
    99     // created file is unique.
    92     tstring createUniqueFile(const tstring &prototype);
   100     tstring createUniqueFile(const tstring &prototype);
    95     // Currently supports only "standard" path like "c:\bla-bla"
   103     // Currently supports only "standard" path like "c:\bla-bla"
    96     // If 'createdDirs' parameter is not NULL, the given array is appended with
   104     // If 'createdDirs' parameter is not NULL, the given array is appended with
    97     // all subdirectories created by this function call.
   105     // all subdirectories created by this function call.
    98     void createDirectory(const tstring &path, tstring_array* createdDirs=0);
   106     void createDirectory(const tstring &path, tstring_array* createdDirs=0);
    99 
   107 
   100     // copies file from fromPath to toPath. Creates output directory if doesn't exist.
   108     // copies file from fromPath to toPath.
   101     void copyFile(const tstring& fromPath, const tstring& toPath, bool failIfExists);
   109     // Creates output directory if doesn't exist.
   102 
   110     void copyFile(const tstring& fromPath, const tstring& toPath,
   103     // moves file from fromPath to toPath. Creates output directory if doesn't exist.
   111             bool failIfExists);
   104     void moveFile(const tstring& fromPath, const tstring& toPath, bool failIfExists);
   112 
       
   113     // moves file from fromPath to toPath.
       
   114     // Creates output directory if doesn't exist.
       
   115     void moveFile(const tstring& fromPath, const tstring& toPath,
       
   116             bool failIfExists);
   105 
   117 
   106     // Throws exception if fails to delete specified 'path'.
   118     // Throws exception if fails to delete specified 'path'.
   107     // Exits normally if 'path' doesn't exist or it has been deleted.
   119     // Exits normally if 'path' doesn't exist or it has been deleted.
   108     // Attempts to strip R/O attribute if delete fails and retry delete.
   120     // Attempts to strip R/O attribute if delete fails and retry delete.
   109     void deleteFile(const tstring &path);
   121     void deleteFile(const tstring &path);
   129     // Returns 'false' if 'dirPath' references existing file system object
   141     // Returns 'false' if 'dirPath' references existing file system object
   130     // which is not a directory or if failed to delete one ore more files in
   142     // which is not a directory or if failed to delete one ore more files in
   131     // 'dirPath' directory.
   143     // 'dirPath' directory.
   132     // Doesn't abort iteration over files if the given directory after the
   144     // Doesn't abort iteration over files if the given directory after the
   133     // first failure to delete a file.
   145     // first failure to delete a file.
   134     bool deleteFilesInDirectory(const tstring &dirPath, const std::nothrow_t &) throw();
   146     bool deleteFilesInDirectory(const tstring &dirPath,
       
   147             const std::nothrow_t &) throw();
   135     // Like deleteFilesInDirectory, but deletes subdirectories as well
   148     // Like deleteFilesInDirectory, but deletes subdirectories as well
   136     void deleteDirectoryRecursive(const tstring &dirPath);
   149     void deleteDirectoryRecursive(const tstring &dirPath);
   137     bool deleteDirectoryRecursive(const tstring &dirPath, const std::nothrow_t &) throw();
   150     bool deleteDirectoryRecursive(const tstring &dirPath,
       
   151             const std::nothrow_t &) throw();
   138 
   152 
   139     class DirectoryCallback {
   153     class DirectoryCallback {
   140     public:
   154     public:
   141         virtual ~DirectoryCallback() {};
   155         virtual ~DirectoryCallback() {};
   142 
   156 
   199         bool theWithFolders;
   213         bool theWithFolders;
   200         tstring root;
   214         tstring root;
   201         tstring_array items;
   215         tstring_array items;
   202     };
   216     };
   203 
   217 
   204     // Returns array of all the files/sub-folders from the given directory, empty array if basedir is not a directory. The returned
   218     // Returns array of all the files/sub-folders from the given directory,
   205     // array is ordered from top down (i.e. dirs are listed first followed by subfolders and files).
   219     // empty array if basedir is not a directory. The returned
   206     // Order of subfolders and files is undefined but usually they are sorted by names.
   220     // array is ordered from top down (i.e. dirs are listed first followed
       
   221     // by subfolders and files).
       
   222     // Order of subfolders and files is undefined
       
   223     // but usually they are sorted by names.
   207     inline tstring_array listAllContents(const tstring& basedir) {
   224     inline tstring_array listAllContents(const tstring& basedir) {
   208         return DirectoryIterator(basedir).findItems();
   225         return DirectoryIterator(basedir).findItems();
   209     }
   226     }
   210 
   227 
   211     // Helper to construct path from multiple components.
   228     // Helper to construct path from multiple components.
   238 
   255 
   239     struct Directory {
   256     struct Directory {
   240         Directory() {
   257         Directory() {
   241         }
   258         }
   242 
   259 
   243         Directory(const tstring &parent, const tstring &subdir) : parent(parent), subdir(subdir)  {
   260         Directory(const tstring &parent,
       
   261                 const tstring &subdir) : parent(parent), subdir(subdir)  {
   244         }
   262         }
   245 
   263 
   246         operator tstring () const {
   264         operator tstring () const {
   247             return getPath();
   265             return getPath();
   248         }
   266         }
   311                 appendFile(FileUtils::mkpath() << dirname << *it);
   329                 appendFile(FileUtils::mkpath() << dirname << *it);
   312             }
   330             }
   313             return *this;
   331             return *this;
   314         }
   332         }
   315 
   333 
   316         // Schedule empty directory for deletion with empty roots (up to Directory.parent).
   334         // Schedule empty directory for deletion with empty roots
       
   335         // (up to Directory.parent).
   317         Deleter& appendEmptyDirectory(const Directory& dir);
   336         Deleter& appendEmptyDirectory(const Directory& dir);
   318 
   337 
   319         // Schedule empty directory for deletion without roots.
   338         // Schedule empty directory for deletion without roots.
   320         // This is a particular case of appendEmptyDirectory(const Directory& dir)
   339         // This is a particular case of
       
   340         // appendEmptyDirectory(const Directory& dir)
   321         // with Directory(dirname(path), basename(path)).
   341         // with Directory(dirname(path), basename(path)).
   322         Deleter& appendEmptyDirectory(const tstring& path);
   342         Deleter& appendEmptyDirectory(const tstring& path);
   323 
   343 
   324         // Schedule all file from the given directory for deletion.
   344         // Schedule all file from the given directory for deletion.
   325         Deleter& appendAllFilesInDirectory(const tstring& path);
   345         Deleter& appendAllFilesInDirectory(const tstring& path);