]> cygwin.com Git - cygwin-apps/setup.git/blame - archive_tar_file.cc
Added dpiAwareness element to manifest
[cygwin-apps/setup.git] / archive_tar_file.cc
CommitLineData
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
b24c88b3 18#include <errno.h>
b34fb59f 19#include <algorithm>
b24c88b3 20
b24c88b3
RC
21#include "io_stream.h"
22#include "compress.h"
23#include "archive.h"
24#include "archive_tar.h"
b24c88b3 25
bb000e11 26archive_tar_file::archive_tar_file (tar_state & newstate):read_something (false), state (newstate)
b24c88b3
RC
27{
28}
29
30archive_tar_file::~archive_tar_file ()
31{
bb000e11
CF
32 if (read_something)
33 state.header_read = 0;
b24c88b3
RC
34}
35
36/* Virtual memebrs */
37ssize_t archive_tar_file::read (void *buffer, size_t len)
38{
39 /* how many bytes do we want to give the user */
40 int
44d31ea0 41 want = std::min ((off_t) len, state.file_length - state.file_offset);
b24c88b3
RC
42 /* how many do we need to read after that to line up the file pointer */
43 int
44 roundup = (512 - (want % 512)) % 512;
d0c6ddb9 45 read_something = true;
b24c88b3
RC
46 if (want)
47 {
dda929ee
JT
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;
b24c88b3
RC
82 }
83 return 0;
84}
85
86/* provide data to (double duh!) */
ca9506cc 87ssize_t archive_tar_file::write (const void *buffer, size_t len)
b24c88b3
RC
88{
89 /* write not supported */
99c0e3de
JT
90 state.lasterr = EBADF;
91 return -1;
b24c88b3
RC
92}
93
94/* read data without removing it from the class's internal buffer */
95ssize_t archive_tar_file::peek (void *buffer, size_t len)
96{
97 int
44d31ea0 98 want = std::min ((off_t) len, state.file_length - state.file_offset);
b24c88b3
RC
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;
99c0e3de 112 return -1;
b24c88b3
RC
113 }
114 }
115 return 0;
116}
117
44d31ea0 118off_t
b24c88b3
RC
119archive_tar_file::tell ()
120{
121 return state.file_offset;
122}
123
44d31ea0
CV
124off_t
125archive_tar_file::seek (off_t where, io_stream_seek_t whence)
ca9506cc
RC
126{
127 /* nothing needs seeking here yet. Implement when needed
128 */
99c0e3de 129 state.lasterr = EINVAL;
ca9506cc
RC
130 return -1;
131}
132
b24c88b3
RC
133/* try guessing this one */
134int
135archive_tar_file::error ()
136{
137 return state.lasterr;
138}
139
b41c2908 140time_t
b24c88b3
RC
141archive_tar_file::get_mtime ()
142{
b41c2908
CV
143 unsigned long mtime;
144 sscanf (state.tar_header.mtime, "%lo", &mtime);
145 return (time_t) mtime;
146}
147
148mode_t
149archive_tar_file::get_mode ()
150{
151 unsigned long mode;
152 sscanf (state.tar_header.mode, "%lo", &mode);
153 return (mode_t) mode;
b24c88b3 154}
This page took 0.148117 seconds and 6 git commands to generate.