]> cygwin.com Git - cygwin-apps/setup.git/blob - archive_tar_file.cc
2001-11-13 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 }
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!) */
82 ssize_t archive_tar_file::write (void *buffer, size_t len)
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
118 /* try guessing this one */
119 int
120 archive_tar_file::error ()
121 {
122 return state.lasterr;
123 }
124
125 int
126 archive_tar_file::get_mtime ()
127 {
128 int mtime;
129 sscanf (state.tar_header.mtime, "%o", &mtime);
130 return mtime;
131 }
This page took 0.040901 seconds and 5 git commands to generate.