1 /* 2 * Copyright (C) 2024 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 HDC_HEADER_H 16 #define HDC_HEADER_H 17 18 #include <cstdint> 19 #include <string> 20 #include "common.h" 21 22 namespace Hdc { 23 #define HEADER_LEN 512 24 #define HEADER_NAME_LEN 100 25 #define HEADER_MODE_LEN 8 26 #define HEADER_UID_LEN 8 27 #define HEADER_GID_LEN 8 28 #define HEADER_SIZE_LEN 12 29 #define HEADER_MTIME_LEN 12 30 #define HEADER_CHKSUM_LEN 8 31 #define HEADER_TYPEFLAGE_LEN 1 32 #define HEADER_LINKNAME_LEN 100 33 #define HEADER_MAGIC_LEN 6 34 #define HEADER_VERSION_LEN 2 35 #define HEADER_UNAME_LEN 32 36 #define HEADER_GNAME_LEN 32 37 #define HEADER_DEVMAJOR_LEN 8 38 #define HEADER_DEVMINOR_LEN 8 39 #define HEADER_PREFIX_LEN 155 40 #define HEADER_PAD_LEN 12 41 #define HEADER_MAX_FILE_LEN 255 42 43 typedef enum : uint8_t { 44 /// 无效值 45 INVALID = 0, 46 /// 0: 普通文件 47 ORDINARYFILE = '0', 48 /// 1: 硬链接 49 HARDLINK = '1', 50 /// 2: 软链接 51 SOFTLINK = '2', 52 /// 3: 字符设备 53 CHARACTERDEVICE = '3', 54 /// 4: 块设备 55 BLOCKDEVICE = '4', 56 /// 5: 文件夹 57 DIRECTORY = '5', 58 /// 6: 命名管道 59 FIFO = '6', 60 /// 7: 保留字 61 RESERVE = '7', 62 } TypeFlage; 63 64 struct Header { 65 /// 存储文件路径。tar只有100位,不够的使用prefix进行拼接 66 uint8_t name[100]; 67 /// 存储文件权限 68 uint8_t mode[8]; 69 /// 用户ID。和tar格式保持一致。暂不使用,预留字段 70 uint8_t uid[8]; 71 /// 组ID。和uid一样,预留 72 uint8_t gid[8]; 73 /// 文件大小。以8进制进行存储 74 /// 如果是目录,则填充11个0:00000000000+NUL 75 /// 如果是文件,则取出文件的字节大小,假设文件大小为;1024byte,转换到8进制字符串为:2000,不足前面补0: 00000002000+NUL 76 uint8_t size[12]; 77 /// 文件最后修改时间,10位时间戳的8进制字符。UTC时间。暂不使用 78 uint8_t mtime[12]; 79 /// 完整性校验。暂不使用 80 uint8_t chksum[8]; 81 /// 文件类型 82 uint8_t typeflage[1]; 83 /// 链接名。暂不使用 84 uint8_t linkname[100]; 85 /// TAR数据段标识字段。不需要填00000+NUL,否则填写:ustar+NUL,表示是TAR文件数据 86 uint8_t magic[6]; 87 /// 表示TAR文件结构的版本号 88 uint8_t version[2]; 89 /// 计算机用户名。暂不使用 90 uint8_t uname[32]; 91 /// 用户组名。暂不使用 92 uint8_t gname[32]; 93 /// 主设备号,暂不使用 94 uint8_t devmajor[8]; 95 /// 次设备号,暂不使用 96 uint8_t devminor[8]; 97 /// 文件路径前缀 98 uint8_t prefix[155]; 99 uint8_t pad[12]; 100 101 Header(); 102 explicit Header(uint8_t data[512], int dataLen); 103 std::string Name(); 104 bool UpdataName(std::string fileName); 105 uint64_t Size(); 106 void UpdataSize(size_t fileLen); 107 TypeFlage FileType(); 108 void UpdataFileType(TypeFlage fileType); 109 bool IsInvalid(); 110 void UpdataCheckSum(); 111 void GetBytes(uint8_t data[512], int dataLen); 112 }; 113 114 } 115 #endif 116