mar_extract.c (1884B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include <sys/types.h> 8 #include <sys/stat.h> 9 #include <fcntl.h> 10 #include <string.h> 11 #include "mar_private.h" 12 #include "mar.h" 13 14 #ifdef XP_WIN 15 # include <io.h> 16 # include <direct.h> 17 # define fdopen _fdopen 18 #endif 19 20 /* Ensure that the directory containing this file exists */ 21 static int mar_ensure_parent_dir(const char* path) { 22 char* slash = strrchr(path, '/'); 23 if (slash) { 24 *slash = '\0'; 25 mar_ensure_parent_dir(path); 26 #ifdef XP_WIN 27 _mkdir(path); 28 #else 29 mkdir(path, 0755); 30 #endif 31 *slash = '/'; 32 } 33 return 0; 34 } 35 36 static int mar_test_callback(MarFile* mar, const MarItem* item, void* unused) { 37 FILE* fp; 38 uint8_t buf[BLOCKSIZE]; 39 int fd, len, offset = 0; 40 41 if (mar_ensure_parent_dir(item->name)) { 42 return -1; 43 } 44 45 #ifdef XP_WIN 46 fd = _open(item->name, _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY, 47 item->flags); 48 #else 49 fd = creat(item->name, item->flags); 50 #endif 51 if (fd == -1) { 52 fprintf(stderr, "ERROR: could not create file in mar_test_callback()\n"); 53 perror(item->name); 54 return -1; 55 } 56 57 fp = fdopen(fd, "wb"); 58 if (!fp) { 59 return -1; 60 } 61 62 while ((len = mar_read(mar, item, offset, buf, sizeof(buf))) > 0) { 63 if (fwrite(buf, len, 1, fp) != 1) { 64 break; 65 } 66 offset += len; 67 } 68 69 fclose(fp); 70 return len == 0 ? 0 : -1; 71 } 72 73 int mar_extract(const char* path) { 74 MarFile* mar; 75 int rv; 76 77 MarReadResult result = mar_open(path, &mar); 78 if (result != MAR_READ_SUCCESS) { 79 return -1; 80 } 81 82 rv = mar_enum_items(mar, mar_test_callback, NULL); 83 84 mar_close(mar); 85 return rv; 86 }