1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 
  34 #ifndef FILEPATH_H
  35 #define FILEPATH_H
  36 
  37 #include "Platform.h"
  38 #include "PlatformString.h"
  39 
  40 #include <vector>
  41 
  42 enum FileAttribute {
  43 #ifdef WINDOWS
  44     faArchive = FILE_ATTRIBUTE_ARCHIVE,
  45     faCompressed = FILE_ATTRIBUTE_COMPRESSED,
  46     faDevice = FILE_ATTRIBUTE_DEVICE,
  47     faDirectory = FILE_ATTRIBUTE_DIRECTORY,
  48     faEncrypted = FILE_ATTRIBUTE_ENCRYPTED,
  49     faHidden = FILE_ATTRIBUTE_HIDDEN,
  50     //faIntegrityStream = FILE_ATTRIBUTE_INTEGRITY_STREAM,
  51     faNormal = FILE_ATTRIBUTE_NORMAL,
  52     faNotContentIndexed = FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,
  53     //faNoScrubData = FILE_ATTRIBUTE_NO_SCRUB_DATA,
  54     faOffline = FILE_ATTRIBUTE_OFFLINE,
  55     faSystem = FILE_ATTRIBUTE_SYSTEM,
  56     faSymbolicLink = FILE_ATTRIBUTE_REPARSE_POINT,
  57     faSparceFile = FILE_ATTRIBUTE_SPARSE_FILE,
  58     faReadOnly = FILE_ATTRIBUTE_READONLY,
  59     faTemporary = FILE_ATTRIBUTE_TEMPORARY,
  60     faVirtual = FILE_ATTRIBUTE_VIRTUAL
  61 #endif //WINDOWS
  62 #ifdef POSIX
  63     faBlockSpecial,
  64     faCharacterSpecial,
  65     faFIFOSpecial,
  66     faNormal,
  67     faDirectory,
  68     faSymbolicLink,
  69     faSocket,
  70 
  71     // Owner
  72     faReadOnly,
  73     faWriteOnly,
  74     faReadWrite,
  75     faExecute,
  76 
  77     // Group
  78     faGroupReadOnly,
  79     faGroupWriteOnly,
  80     faGroupReadWrite,
  81     faGroupExecute,
  82 
  83     // Others
  84     faOthersReadOnly,
  85     faOthersWriteOnly,
  86     faOthersReadWrite,
  87     faOthersExecute,
  88 
  89     faHidden
  90 #endif //POSIX
  91 };
  92 
  93 class FileAttributes {
  94 private:
  95     TString FFileName;
  96     bool FFollowLink;
  97     std::vector<FileAttribute> FAttributes;
  98 
  99     bool WriteAttributes();
 100     bool ReadAttributes();
 101     bool Valid(const FileAttribute Value);
 102 
 103 public:
 104     FileAttributes(const TString FileName, bool FollowLink = true);
 105 
 106     void Append(const FileAttribute Value);
 107     bool Contains(const FileAttribute Value);
 108     void Remove(const FileAttribute Value);
 109 };
 110 
 111 class FilePath {
 112 private:
 113     FilePath(void) {}
 114     ~FilePath(void) {}
 115 
 116 public:
 117     static bool FileExists(const TString FileName);
 118     static bool DirectoryExists(const TString DirectoryName);
 119 
 120     static bool DeleteFile(const TString FileName);
 121     static bool DeleteDirectory(const TString DirectoryName);
 122 
 123     static TString ExtractFilePath(TString Path);
 124     static TString ExtractFileExt(TString Path);
 125     static TString ExtractFileName(TString Path);
 126     static TString ChangeFileExt(TString Path, TString Extension);
 127 
 128     static TString IncludeTrailingSeparater(const TString value);
 129     static TString IncludeTrailingSeparater(const char* value);
 130     static TString IncludeTrailingSeparater(const wchar_t* value);
 131     static TString FixPathForPlatform(TString Path);
 132     static TString FixPathSeparatorForPlatform(TString Path);
 133     static TString PathSeparator();
 134 
 135     static bool CreateDirectory(TString Path, bool ownerOnly);
 136     static void ChangePermissions(TString FileName, bool ownerOnly);
 137 };
 138 
 139 #endif //FILEPATH_H