]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2001, Robert Collins | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by Robert Collins <rbtcollins@hotmail.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | /* An individual stream from a tar archive. */ | |
17 | ||
18 | #include <errno.h> | |
19 | #include <algorithm> | |
20 | ||
21 | #include "io_stream.h" | |
22 | #include "compress.h" | |
23 | #include "archive.h" | |
24 | #include "archive_tar.h" | |
25 | ||
26 | archive_tar_file::archive_tar_file (tar_state & newstate):read_something (false), state (newstate) | |
27 | { | |
28 | } | |
29 | ||
30 | archive_tar_file::~archive_tar_file () | |
31 | { | |
32 | if (read_something) | |
33 | state.header_read = 0; | |
34 | } | |
35 | ||
36 | /* Virtual memebrs */ | |
37 | ssize_t archive_tar_file::read (void *buffer, size_t len) | |
38 | { | |
39 | /* how many bytes do we want to give the user */ | |
40 | int | |
41 | want = std::min ((off_t) len, state.file_length - state.file_offset); | |
42 | /* how many do we need to read after that to line up the file pointer */ | |
43 | int | |
44 | roundup = (512 - (want % 512)) % 512; | |
45 | read_something = true; | |
46 | if (want) | |
47 | { | |
48 | int need = want; | |
49 | char *p = (char *)buffer; | |
50 | while (need > 0) | |
51 | { | |
52 | ssize_t got = state.parent->read (p, need); | |
53 | if (got <= 0) | |
54 | { | |
55 | /* unexpected EOF or read error in the tar parent stream */ | |
56 | /* the user can query the parent for the error */ | |
57 | state.lasterr = EIO; | |
58 | return -1; | |
59 | } | |
60 | p += got; | |
61 | need -= got; | |
62 | } | |
63 | ||
64 | char throwaway[512]; | |
65 | p = &throwaway[0]; | |
66 | while (roundup > 0) | |
67 | { | |
68 | ssize_t got2 = state.parent->read (p, roundup); | |
69 | if (got2 <= 0) | |
70 | { | |
71 | /* unexpected EOF or read error in the tar parent stream */ | |
72 | /* the user can query the parent for the error */ | |
73 | state.lasterr = EIO; | |
74 | return -1; | |
75 | } | |
76 | p += got2; | |
77 | roundup -= got2; | |
78 | } | |
79 | ||
80 | state.file_offset += want; | |
81 | return want; | |
82 | } | |
83 | return 0; | |
84 | } | |
85 | ||
86 | /* provide data to (double duh!) */ | |
87 | ssize_t archive_tar_file::write (const void *buffer, size_t len) | |
88 | { | |
89 | /* write not supported */ | |
90 | state.lasterr = EBADF; | |
91 | return -1; | |
92 | } | |
93 | ||
94 | /* read data without removing it from the class's internal buffer */ | |
95 | ssize_t archive_tar_file::peek (void *buffer, size_t len) | |
96 | { | |
97 | int | |
98 | want = std::min ((off_t) len, state.file_length - state.file_offset); | |
99 | if (want) | |
100 | { | |
101 | ssize_t | |
102 | got = state.parent->peek (buffer, want); | |
103 | if (got == want) | |
104 | { | |
105 | return got; | |
106 | } | |
107 | else | |
108 | { | |
109 | /* unexpected EOF or read error in the tar parent stream */ | |
110 | /* the user can query the parent for the error */ | |
111 | state.lasterr = EIO; | |
112 | return -1; | |
113 | } | |
114 | } | |
115 | return 0; | |
116 | } | |
117 | ||
118 | off_t | |
119 | archive_tar_file::tell () | |
120 | { | |
121 | return state.file_offset; | |
122 | } | |
123 | ||
124 | off_t | |
125 | archive_tar_file::seek (off_t where, io_stream_seek_t whence) | |
126 | { | |
127 | /* nothing needs seeking here yet. Implement when needed | |
128 | */ | |
129 | state.lasterr = EINVAL; | |
130 | return -1; | |
131 | } | |
132 | ||
133 | /* try guessing this one */ | |
134 | int | |
135 | archive_tar_file::error () | |
136 | { | |
137 | return state.lasterr; | |
138 | } | |
139 | ||
140 | time_t | |
141 | archive_tar_file::get_mtime () | |
142 | { | |
143 | unsigned long mtime; | |
144 | sscanf (state.tar_header.mtime, "%lo", &mtime); | |
145 | return (time_t) mtime; | |
146 | } | |
147 | ||
148 | mode_t | |
149 | archive_tar_file::get_mode () | |
150 | { | |
151 | unsigned long mode; | |
152 | sscanf (state.tar_header.mode, "%lo", &mode); | |
153 | return (mode_t) mode; | |
154 | } |