UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2018 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef TAR_H 30 #define TAR_H 31 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <time.h> 35 #include <inttypes.h> 36 37 #ifdef __cplusplus 38 extern "C" { 39 #endif 40 41 #define T_ISUID 04000 42 #define T_ISGID 02000 43 #define T_IRUSR 00400 44 #define T_IWUSR 00200 45 #define T_IXUSR 00100 46 #define T_IRGRP 00040 47 #define T_IWGRP 00020 48 #define T_IXGRP 00010 49 #define T_IROTH 00004 50 #define T_IWOTH 00002 51 #define T_IXOTH 00001 52 53 #define TAR_TYPE_FILE '0' 54 #define TAR_TYPE_DIRECTORY '5' 55 56 enum TarError { 57 TAR_OK = 0, 58 TAR_PATH_TOO_LONG, 59 TAR_FILE_TOO_LARGE, 60 TAR_CONTENT_TOO_LARGE, 61 TAR_UNFINISHED_FILE, 62 TAR_CONTENT_BROKEN, 63 TAR_ERROR 64 }; 65 66 typedef enum TarError TarError; 67 68 typedef struct TarHeader { 69 char name[100]; 70 char mode[8]; 71 char uid[8]; 72 char gid[8]; 73 char size[12]; 74 char mtime[12]; 75 char chksum[8]; 76 char typeflag; 77 char linkname[100]; 78 char magic[6]; 79 char version[2]; 80 char uname[32]; 81 char gname[32]; 82 char devmajor[8]; 83 char devminor[8]; 84 char prefix[155]; 85 char padding[12]; 86 } TarHeader; 87 88 typedef struct TarOutputStream { 89 FILE *file; 90 uint64_t cur_filesize; 91 uint64_t cur_written; 92 TarError error; 93 } TarOutputStream; 94 95 typedef struct TarEntry { 96 char *path; 97 uint64_t size; 98 int type; 99 } TarEntry; 100 101 typedef struct TarInputStream { 102 FILE *file; 103 TarEntry cur_entry; 104 uint64_t cur_read; 105 TarError error; 106 } TarInputStream; 107 108 const char* tar_error2str(TarError error); 109 110 TarOutputStream* tar_open(FILE *f); 111 int tar_add_dir(TarOutputStream *tar, char *path, uint32_t mode, time_t mtime); 112 int tar_begin_file( 113 TarOutputStream *tar, 114 char *path, 115 uint32_t mode, 116 uint64_t size, 117 time_t mtime); 118 size_t tar_fwrite(const void *ptr, size_t s, size_t n, TarOutputStream *stream); 119 int tar_end_file(TarOutputStream *tar); 120 int tar_close(TarOutputStream *tar); 121 122 TarInputStream* tar_inputstream_open(FILE *f); 123 TarEntry* tar_read_entry(TarInputStream *tar); 124 size_t tar_fread(void *ptr, size_t s, size_t n, TarInputStream *stream); 125 int tar_seek(TarInputStream *stream, long offset, int whence); 126 int tar_inputstream_close(TarInputStream *tar); 127 128 129 #ifdef __cplusplus 130 } 131 #endif 132 133 #endif /* TAR_H */ 134 135