1 /*
   2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #ifndef FILEPATH_H
  27 #define FILEPATH_H
  28 
  29 #include "Platform.h"
  30 #include "PlatformString.h"
  31 
  32 #include <vector>
  33 
  34 enum FileAttribute {
  35 #ifdef WINDOWS
  36     faArchive = FILE_ATTRIBUTE_ARCHIVE,
  37     faCompressed = FILE_ATTRIBUTE_COMPRESSED,
  38     faDevice = FILE_ATTRIBUTE_DEVICE,
  39     faDirectory = FILE_ATTRIBUTE_DIRECTORY,
  40     faEncrypted = FILE_ATTRIBUTE_ENCRYPTED,
  41     faHidden = FILE_ATTRIBUTE_HIDDEN,
  42     //faIntegrityStream = FILE_ATTRIBUTE_INTEGRITY_STREAM,
  43     faNormal = FILE_ATTRIBUTE_NORMAL,
  44     faNotContentIndexed = FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,
  45     //faNoScrubData = FILE_ATTRIBUTE_NO_SCRUB_DATA,
  46     faOffline = FILE_ATTRIBUTE_OFFLINE,
  47     faSystem = FILE_ATTRIBUTE_SYSTEM,
  48     faSymbolicLink = FILE_ATTRIBUTE_REPARSE_POINT,
  49     faSparceFile = FILE_ATTRIBUTE_SPARSE_FILE,
  50     faReadOnly = FILE_ATTRIBUTE_READONLY,
  51     faTemporary = FILE_ATTRIBUTE_TEMPORARY,
  52     faVirtual = FILE_ATTRIBUTE_VIRTUAL
  53 #endif //WINDOWS
  54 #ifdef POSIX
  55     faBlockSpecial,
  56     faCharacterSpecial,
  57     faFIFOSpecial,
  58     faNormal,
  59     faDirectory,
  60     faSymbolicLink,
  61     faSocket,
  62 
  63     // Owner
  64     faReadOnly,
  65     faWriteOnly,
  66     faReadWrite,
  67     faExecute,
  68 
  69     // Group
  70     faGroupReadOnly,
  71     faGroupWriteOnly,
  72     faGroupReadWrite,
  73     faGroupExecute,
  74 
  75     // Others
  76     faOthersReadOnly,
  77     faOthersWriteOnly,
  78     faOthersReadWrite,
  79     faOthersExecute,
  80 
  81     faHidden
  82 #endif //POSIX
  83 };
  84 
  85 class FileAttributes {
  86 private:
  87     TString FFileName;
  88     bool FFollowLink;
  89     std::vector<FileAttribute> FAttributes;
  90 
  91     bool WriteAttributes();
  92     bool ReadAttributes();
  93     bool Valid(const FileAttribute Value);
  94 
  95 public:
  96     FileAttributes(const TString FileName, bool FollowLink = true);
  97 
  98     void Append(const FileAttribute Value);
  99     bool Contains(const FileAttribute Value);
 100     void Remove(const FileAttribute Value);
 101 };
 102 
 103 class FilePath {
 104 private:
 105     FilePath(void) {}
 106     ~FilePath(void) {}
 107 
 108 public:
 109     static bool FileExists(const TString FileName);
 110     static bool DirectoryExists(const TString DirectoryName);
 111 
 112     static bool DeleteFile(const TString FileName);
 113     static bool DeleteDirectory(const TString DirectoryName);
 114 
 115     static TString ExtractFilePath(TString Path);
 116     static TString ExtractFileExt(TString Path);
 117     static TString ExtractFileName(TString Path);
 118     static TString ChangeFileExt(TString Path, TString Extension);
 119 
 120     static TString IncludeTrailingSeparator(const TString value);
 121     static TString IncludeTrailingSeparator(const char* value);
 122     static TString IncludeTrailingSeparator(const wchar_t* value);
 123     static TString FixPathForPlatform(TString Path);
 124     static TString FixPathSeparatorForPlatform(TString Path);
 125     static TString PathSeparator();
 126 
 127     static bool CreateDirectory(TString Path, bool ownerOnly);
 128     static void ChangePermissions(TString FileName, bool ownerOnly);
 129 };
 130 
 131 #endif //FILEPATH_H