]> cygwin.com Git - cygwin-apps/setup.git/blob - archive_tar_file.cc
2002-01-21 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / archive_tar_file.cc
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"
35 #include "concat.h"
36
37 #include "port.h"
38
39 archive_tar_file::archive_tar_file (tar_state & newstate):state (newstate)
40 {
41 }
42
43 archive_tar_file::~archive_tar_file ()
44 {
45 state.header_read = 0;
46 destroyed = 1;
47 }
48
49 /* Virtual memebrs */
50 ssize_t archive_tar_file::read (void *buffer, size_t len)
51 {
52 /* how many bytes do we want to give the user */
53 int
54 want = min (len, state.file_length - state.file_offset);
55 /* how many do we need to read after that to line up the file pointer */
56 int
57 roundup = (512 - (want % 512)) % 512;
58 if (want)
59 {
60 ssize_t
61 got = state.parent->read (buffer, want);
62 char
63 throwaway[512];
64 ssize_t
65 got2 = state.parent->read (throwaway, roundup);
66 if (got == want && got2 == roundup)
67 {
68 state.file_offset += got;
69 return got;
70 }
71 else
72 {
73 /* unexpected EOF or read error in the tar parent stream */
74 /* the user can query the parent for the error */
75 state.lasterr = EIO;
76 return EIO;
77 }
78 }
79 return 0;
80 }
81
82 /* provide data to (double duh!) */
83 ssize_t archive_tar_file::write (const void *buffer, size_t len)
84 {
85 /* write not supported */
86 return EBADF;
87 }
88
89 /* read data without removing it from the class's internal buffer */
90 ssize_t archive_tar_file::peek (void *buffer, size_t len)
91 {
92 int
93 want = min (len, state.file_length - state.file_offset);
94 if (want)
95 {
96 ssize_t
97 got = state.parent->peek (buffer, want);
98 if (got == want)
99 {
100 return got;
101 }
102 else
103 {
104 /* unexpected EOF or read error in the tar parent stream */
105 /* the user can query the parent for the error */
106 state.lasterr = EIO;
107 return EIO;
108 }
109 }
110 return 0;
111 }
112
113 long
114 archive_tar_file::tell ()
115 {
116 return state.file_offset;
117 }
118
119 int
120 archive_tar_file::seek (long where, io_stream_seek_t whence)
121 {
122 /* nothing needs seeking here yet. Implement when needed
123 */
124 return -1;
125 }
126
127 /* try guessing this one */
128 int
129 archive_tar_file::error ()
130 {
131 return state.lasterr;
132 }
133
134 int
135 archive_tar_file::get_mtime ()
136 {
137 int mtime;
138 sscanf (state.tar_header.mtime, "%o", &mtime);
139 return mtime;
140 }
This page took 0.045146 seconds and 6 git commands to generate.