root/branches/3.00/src/archive_tar_file.cc

Revision 644, 2.7 kB (checked in by fujii, 3 years ago)

Add new netinstall source.

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