]>
Commit | Line | Data |
---|---|---|
b24c88b3 RC |
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 | #if 0 | |
19 | static const char *cvsid = "\n%%% $Id$\n"; | |
20 | #endif | |
21 | ||
22 | #include "win32.h" | |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <sys/types.h> | |
26 | #include <sys/fcntl.h> | |
27 | #include <errno.h> | |
28 | ||
29 | #include "zlib/zlib.h" | |
30 | #include "io_stream.h" | |
31 | #include "compress.h" | |
32 | #include "archive.h" | |
33 | #include "archive_tar.h" | |
34 | #include "log.h" | |
b24c88b3 RC |
35 | |
36 | #include "port.h" | |
37 | ||
38 | archive_tar_file::archive_tar_file (tar_state & newstate):state (newstate) | |
39 | { | |
40 | } | |
41 | ||
42 | archive_tar_file::~archive_tar_file () | |
43 | { | |
44 | state.header_read = 0; | |
e9440f0f | 45 | destroyed = 1; |
b24c88b3 RC |
46 | } |
47 | ||
48 | /* Virtual memebrs */ | |
49 | ssize_t archive_tar_file::read (void *buffer, size_t len) | |
50 | { | |
51 | /* how many bytes do we want to give the user */ | |
52 | int | |
53 | want = min (len, state.file_length - state.file_offset); | |
54 | /* how many do we need to read after that to line up the file pointer */ | |
55 | int | |
56 | roundup = (512 - (want % 512)) % 512; | |
57 | if (want) | |
58 | { | |
59 | ssize_t | |
60 | got = state.parent->read (buffer, want); | |
61 | char | |
62 | throwaway[512]; | |
63 | ssize_t | |
64 | got2 = state.parent->read (throwaway, roundup); | |
65 | if (got == want && got2 == roundup) | |
66 | { | |
67 | state.file_offset += got; | |
68 | return got; | |
69 | } | |
70 | else | |
71 | { | |
72 | /* unexpected EOF or read error in the tar parent stream */ | |
73 | /* the user can query the parent for the error */ | |
74 | state.lasterr = EIO; | |
75 | return EIO; | |
76 | } | |
77 | } | |
78 | return 0; | |
79 | } | |
80 | ||
81 | /* provide data to (double duh!) */ | |
ca9506cc | 82 | ssize_t archive_tar_file::write (const void *buffer, size_t len) |
b24c88b3 RC |
83 | { |
84 | /* write not supported */ | |
85 | return EBADF; | |
86 | } | |
87 | ||
88 | /* read data without removing it from the class's internal buffer */ | |
89 | ssize_t archive_tar_file::peek (void *buffer, size_t len) | |
90 | { | |
91 | int | |
92 | want = min (len, state.file_length - state.file_offset); | |
93 | if (want) | |
94 | { | |
95 | ssize_t | |
96 | got = state.parent->peek (buffer, want); | |
97 | if (got == want) | |
98 | { | |
99 | return got; | |
100 | } | |
101 | else | |
102 | { | |
103 | /* unexpected EOF or read error in the tar parent stream */ | |
104 | /* the user can query the parent for the error */ | |
105 | state.lasterr = EIO; | |
106 | return EIO; | |
107 | } | |
108 | } | |
109 | return 0; | |
110 | } | |
111 | ||
112 | long | |
113 | archive_tar_file::tell () | |
114 | { | |
115 | return state.file_offset; | |
116 | } | |
117 | ||
ca9506cc RC |
118 | int |
119 | archive_tar_file::seek (long where, io_stream_seek_t whence) | |
120 | { | |
121 | /* nothing needs seeking here yet. Implement when needed | |
122 | */ | |
123 | return -1; | |
124 | } | |
125 | ||
b24c88b3 RC |
126 | /* try guessing this one */ |
127 | int | |
128 | archive_tar_file::error () | |
129 | { | |
130 | return state.lasterr; | |
131 | } | |
132 | ||
133 | int | |
134 | archive_tar_file::get_mtime () | |
135 | { | |
136 | int mtime; | |
137 | sscanf (state.tar_header.mtime, "%o", &mtime); | |
138 | return mtime; | |
139 | } |