• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 
16 #ifndef PHONECLONE_INSTALLDUNTARFILE_H
17 #define PHONECLONE_INSTALLDUNTARFILE_H
18 #include "installd_tar_utils.h"
19 #include "tar_util.h"
20 #include <string>
21 #include <vector>
22 
23 namespace installd {
24 
25 struct ParseTarPath {
26     char *longName = nullptr;
27     char *longLink = nullptr;
28     char *fullPath = nullptr;
29     char *realName = nullptr;
30     char *realLink = nullptr;
31 };
32 
33 struct TarFileInfo {
34     off_t fileSize;
35     off_t fileBlockCnt;
36     off_t pos;
37 };
38 
39 // helper function.
40 off_t ParseOctalStr(const char *p, size_t n);
41 
42 class UnTarFile {
43 public:
44     UnTarFile(const char *TarPath);
45     virtual ~UnTarFile(void);
46 
47 public:
48     void Reset();
49     bool CheckIsSplitTar(const std::string &tarFile, const std::string &rootpath);
50     int UnSplitTar(const std::string &tarFile, const std::string &rootpath);
51 
52 public:
53     typedef enum { eList = 0, eUnpack = 1, eCheckSplit = 2 } EParseType;
54 
55 private:
56     bool IsProcessTarEnd(char *buff, int &ret);
57     int ParseTarFile(const char *rootPath = "", EParseType type = eList);
58     bool IsEmptyBlock(const char *p);
59     int CheckFileAndInitPath(const char *rootPath, ParseTarPath *parseTarPath);
60     void SetFileChmodAndChown(char *buff, ParseTarPath *parseTarPath, bool &isSoftLink);
61     void HandleGnuLongLink(ParseTarPath *parseTarPath, bool &isSkip, TarFileInfo &tarFileInfo);
62     void HandleGnuLongName(ParseTarPath *parseTarPath, bool &isSkip, TarFileInfo &tarFileInfo);
63     void HandleRegularFile(char *buff, EParseType type, ParseTarPath *parseTarPath, bool &isSkip,
64         TarFileInfo &tarFileInfo);
65     bool FileReadAndWrite(char *destBuff, FILE *destF, size_t readBuffSize);
66     void HandleRegularEUnpackFile(char *buff, ParseTarPath *parseTarPath, bool &isSkip, TarFileInfo &tarFileInfo);
67     bool ProcessTarBlock(char *buff, EParseType type, ParseTarPath *parseTarPath, bool &isSkip, bool &isSoftLink);
68     bool IsValidTarBlock(const TarHeader *tarHeader);
69     bool VerifyChecksum(const TarHeader *tarHeader);
70     void FreePointer(ParseTarPath *parseTarPath);
71     void FreeLongTypePointer(ParseTarPath *parseTarPath);
72     bool CreateDirWithRecursive(const std::string &filePath, mode_t mode = (mode_t)448);
73 
74 private:
75     FILE *FilePtr;
76     off_t tarSize;
77     uid_t newOwner;
78     std::string m_srcPath;
79     bool isSplit = false;
80     std::vector<std::string> file_names;
81     std::vector<off_t> file_sizes;
82     std::vector<off_t> file_data_addrs;
83 };
84 } // namespace installd
85 #endif // PHONECLONE_INSTALLDUNTARFILE_H