blob: e3963bbeb29038327b46c72e2e7382b946d1c905 [file] [log] [blame]
Dees_Troye34c1332013-02-06 19:13:00 +00001/*
Dees Troy3be70a82013-10-22 14:25:12 +00002 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
Dees_Troye34c1332013-02-06 19:13:00 +00004
Dees Troy3be70a82013-10-22 14:25:12 +00005 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
Dees_Troye34c1332013-02-06 19:13:00 +00009
Dees Troy3be70a82013-10-22 14:25:12 +000010 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
Dees_Troye34c1332013-02-06 19:13:00 +000014
Dees Troy3be70a82013-10-22 14:25:12 +000015 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
Dees_Troye34c1332013-02-06 19:13:00 +000017*/
18
19#include <fcntl.h>
20#include "libtar/libtar.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000021#include "twcommon.h"
Dees_Troye34c1332013-02-06 19:13:00 +000022
23int flush = 0, eot_count = -1;
24unsigned char *write_buffer;
25unsigned buffer_size = 4096;
26unsigned buffer_loc = 0;
Dees_Troy83bd4832013-05-04 12:39:56 +000027int buffer_status = 0;
Dees_Troye34c1332013-02-06 19:13:00 +000028
29void reinit_libtar_buffer(void) {
30 flush = 0;
31 eot_count = -1;
32 buffer_loc = 0;
Dees_Troy83bd4832013-05-04 12:39:56 +000033 buffer_status = 1;
Dees_Troye34c1332013-02-06 19:13:00 +000034}
35
36void init_libtar_buffer(unsigned new_buff_size) {
37 if (new_buff_size != 0)
38 buffer_size = new_buff_size;
39
40 reinit_libtar_buffer();
41 write_buffer = (unsigned char*) malloc(sizeof(char *) * buffer_size);
42}
43
44void free_libtar_buffer(void) {
Dees_Troy83bd4832013-05-04 12:39:56 +000045 if (buffer_status > 0)
46 free(write_buffer);
47 buffer_status = 0;
Dees_Troye34c1332013-02-06 19:13:00 +000048}
49
50ssize_t write_libtar_buffer(int fd, const void *buffer, size_t size) {
51 void* ptr;
52
53 if (flush == 0) {
54 ptr = write_buffer + buffer_loc;
55 memcpy(ptr, buffer, size);
56 buffer_loc += size;
57 if (eot_count >= 0 && eot_count < 2)
58 eot_count++;
59 /* At the end of the tar file, libtar will add 2 blank blocks.
60 Once we have received both EOT blocks, we will immediately
61 write anything in the buffer to the file.
62 */
63
64 if (buffer_loc >= buffer_size || eot_count >= 2) {
65 flush = 1;
66 }
67 }
68 if (flush == 1) {
69 flush = 0;
70 if (buffer_loc == 0) {
71 // nothing to write
72 return 0;
73 }
74 if (write(fd, write_buffer, buffer_loc) != buffer_loc) {
Dees_Troy2673cec2013-04-02 20:22:16 +000075 LOGERR("Error writing tar file!\n");
Dees_Troye34c1332013-02-06 19:13:00 +000076 buffer_loc = 0;
77 return -1;
78 } else {
79 buffer_loc = 0;
80 return size;
81 }
82 } else {
83 return size;
84 }
85 // Shouldn't ever get here
86 return -1;
87}
88
89void flush_libtar_buffer(int fd) {
90 eot_count = 0;
Ethan Yonker74db84e2015-06-01 09:55:00 -050091 if (buffer_status)
92 buffer_status = 2;
Dees_Troye34c1332013-02-06 19:13:00 +000093}