blob: 98fa14cad55aca33c879e82961f7fea39a403507 [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>
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050020#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
Dees_Troye34c1332013-02-06 19:13:00 +000023#include "libtar/libtar.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000024#include "twcommon.h"
Dees_Troye34c1332013-02-06 19:13:00 +000025
26int flush = 0, eot_count = -1;
27unsigned char *write_buffer;
28unsigned buffer_size = 4096;
29unsigned buffer_loc = 0;
Dees_Troy83bd4832013-05-04 12:39:56 +000030int buffer_status = 0;
Dees_Troye34c1332013-02-06 19:13:00 +000031
32void reinit_libtar_buffer(void) {
33 flush = 0;
34 eot_count = -1;
35 buffer_loc = 0;
Dees_Troy83bd4832013-05-04 12:39:56 +000036 buffer_status = 1;
Dees_Troye34c1332013-02-06 19:13:00 +000037}
38
39void init_libtar_buffer(unsigned new_buff_size) {
40 if (new_buff_size != 0)
41 buffer_size = new_buff_size;
42
43 reinit_libtar_buffer();
44 write_buffer = (unsigned char*) malloc(sizeof(char *) * buffer_size);
45}
46
47void free_libtar_buffer(void) {
Dees_Troy83bd4832013-05-04 12:39:56 +000048 if (buffer_status > 0)
49 free(write_buffer);
50 buffer_status = 0;
Dees_Troye34c1332013-02-06 19:13:00 +000051}
52
53ssize_t write_libtar_buffer(int fd, const void *buffer, size_t size) {
54 void* ptr;
55
56 if (flush == 0) {
57 ptr = write_buffer + buffer_loc;
58 memcpy(ptr, buffer, size);
59 buffer_loc += size;
60 if (eot_count >= 0 && eot_count < 2)
61 eot_count++;
62 /* At the end of the tar file, libtar will add 2 blank blocks.
63 Once we have received both EOT blocks, we will immediately
64 write anything in the buffer to the file.
65 */
66
67 if (buffer_loc >= buffer_size || eot_count >= 2) {
68 flush = 1;
69 }
70 }
71 if (flush == 1) {
72 flush = 0;
73 if (buffer_loc == 0) {
74 // nothing to write
75 return 0;
76 }
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050077 if (write(fd, write_buffer, buffer_loc) != (int)buffer_loc) {
Dees_Troy2673cec2013-04-02 20:22:16 +000078 LOGERR("Error writing tar file!\n");
Dees_Troye34c1332013-02-06 19:13:00 +000079 buffer_loc = 0;
80 return -1;
81 } else {
82 buffer_loc = 0;
83 return size;
84 }
85 } else {
86 return size;
87 }
88 // Shouldn't ever get here
89 return -1;
90}
91
92void flush_libtar_buffer(int fd) {
93 eot_count = 0;
Ethan Yonker74db84e2015-06-01 09:55:00 -050094 if (buffer_status)
95 buffer_status = 2;
Dees_Troye34c1332013-02-06 19:13:00 +000096}