1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_FILE_PATH_H
16 #define FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_FILE_PATH_H
17 #include <string>
18 #include <vector>
19 namespace OHOS {
20 namespace AppExecFwk {
21 namespace LIBZIP {
22 
23 #define FILE_PATH_LITERAL(x) x
24 #define PRFilePath "s"
25 
26 template<typename T, size_t N>
27 char (&ArraySizeHelper(T (&array)[N]))[N];
28 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
29 
30 class FilePath {
31 public:
32     using CharType = std::string::value_type;
33 
34     FilePath();
35     FilePath(const FilePath &that);
36     explicit FilePath(const std::string &path);
37     ~FilePath();
38     FilePath &operator=(const FilePath &that);
39     bool operator==(const FilePath &that) const;
40     bool operator!=(const FilePath &that) const;
41     bool operator<(const FilePath &that) const;
42 
43     static const CharType kSeparators[];
44     static const size_t kSeparatorsLength;
45     static const CharType kCurrentDirectory[];
46     static const CharType kParentDirectory[];
47     static const CharType kExtensionSeparator;
48 
49     static FilePath FromUTF8Unsafe(const std::string &utf8);
50     static bool IsSeparator(CharType character);
51     static bool CreateDirectory(const FilePath &fullPath);
52     static bool DirectoryExists(const FilePath &path);
53     static bool PathIsValid(const FilePath &path);
54     static bool PathIsReadable(const FilePath &path);
55     static bool PathIsWriteable(const FilePath &path);
56     static bool IsDir(const FilePath &path);
57     static bool GetZipAllDirFiles(const std::string &path, std::vector<std::string> &files);
58     // Returns a FilePath by appending a separator and the supplied path
59     // component to this object's path.  Append takes care to avoid adding
60     // excessive separators if this object's path already ends with a separator.
61     // If this object's path is kCurrentDirectory, a new FilePath corresponding
62     // only to |component| is returned.  |component| must be a relative path;
63     // it is an error to pass an absolute path.
64     FilePath Append(const std::string &component);
65     FilePath Append(FilePath &component);
66     void AppendSeparator(void);
67     // If IsParent(child) holds, appends to path (if non-NULL) the
68     // relative path to child and returns true.
69     bool AppendRelativePath(const FilePath &child, FilePath *path);
70 
71     bool ReferencesParent();
72     void GetComponents(std::vector<std::string> &components);
73     FilePath DirName();
74     FilePath BaseName();
75     bool IsAbsolute();
76     std::string Value();
77     std::string CheckDestDirTail();
78     static bool IsNeedCheckFilePathBaseOnAPIVersion();
79     static bool HasRelativePathBaseOnAPIVersion(const std::string &path);
80     static bool HasRelativePathBaseOnAPIVersion(const std::vector<std::string> &paths);
81 private:
82     std::string path_;
83 
84     void StripTrailingSeparatorsInternal();
85     bool AreAllSeparators(const std::string &input);
86 };
87 }  // namespace LIBZIP
88 }  // namespace AppExecFwk
89 }  // namespace OHOS
90 
91 #endif  // FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_FILE_PATH_H
92