资源简介
tar打包tar打包tar打包tar打包tar打包tar打包tar打包tar打包
代码片段和文件信息
#include “tar.h“
#include // for strlen and memset
#include // for sprintf snprintf and sscanf
#include // for rand
#include // for time
#include
#include
#define ENABLE_LOGGING
#ifdef ENABLE_LOGGING
# define LOG printf
#else
# define LOG(fmt args ...) ((void)0)
#endif
namespace tar
{
const char FILL_CHAR = ‘\0‘;
const int FILE_NAME_LENGTH = 100;
// From http://en.wikipedia.org/wiki/Tar_(computing)#UStar_format
typedef enum tar_file_type
{
tar_file_type_normal = ‘0‘
tar_file_type_hard_link = ‘1‘
tar_file_type_soft_link = ‘2‘
tar_file_type_directory = ‘5‘
} tar_file_type_t;
struct tar_header
{
char name[FILE_NAME_LENGTH]; // file name
char mode[8]; // file mode
char uid[8]; // Owner‘s numeric user ID
char gid[8]; // Group‘s numeric user ID
char size[12]; // File size in bytes (octal base)
char mtime[12]; // Last modification time in
// numeric Unix time format (octal)
char checksum[8]; // Checksum for header record
char typeflag[1]; // file type see tar_file_type_t
char linkname[100]; // Name of linked file
char magic[6]; // UStar indicator “ustar“
char version[2]; // UStar version “00“
char uname[32]; // Owner user name
char gname[32]; // Owner group name
char devmajor[8]; // Device major number
char devminor[8]; // Device minor number
char prefix[155]; // Filename prefix
char pad[12]; // padding
};
void header_set_metadata(tar_header* header)
{
std::memset(header 0 sizeof(tar_header));
std::sprintf(header->magic “ustar“);
std::sprintf(header->mtime “%011lo“ std::time(NULL));
std::sprintf(header->mode “%07o“ 0644);
//TODO
std::sprintf(header->uname “unkown“); // ... a bit random
std::sprintf(header->gname “users“);
header->typeflag[0] = 0; // always just a normal file
}
/* From Wikipedia: The checksum is calculated by taking the sum of the
* unsigned byte values of the header record with the eight checksum
* bytes taken to be ascii spaces. */
void header_set_checksum(tar_header* header)
{
unsigned int sum = 0;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 12117 2018-02-01 16:31 tar\tar.cpp
文件 2972 2018-01-23 09:59 tar\tar.h
文件 339 2018-02-05 18:58 tar\tar_test.txt
文件 54 2018-02-02 14:12 tar\资料\资料.txt
目录 0 2018-02-02 14:12 tar\资料
目录 0 2018-02-02 14:12 tar
----------- --------- ---------- ----- ----
15482 6
评论
共有 条评论