]>
cygwin.com Git - cygwin-apps/setup.git/blob - archive.cc
2 * Copyright (c) 2001, Robert Collins.
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.
9 * A copy of the GNU General Public License can be found at
12 * Written by Robert Collins <rbtcollins@hotmail.com>
16 /* Archive IO operations
19 #include "LogSingleton.h"
21 #include "io_stream.h"
23 #include "archive_tar.h"
25 /* This file is the sole user of alloca(), so do this here.
26 * This will go away when this file is useing proper C++ string handling. */
31 #define alloca __builtin_alloca
35 /* In case you are wondering why the file magic is not in one place:
36 * It could be. But there is little (any?) benefit.
37 * What is important is that the file magic required for any _task_ is centralised.
38 * One such task is identifying archives
40 * to federate into each class one might add a magic parameter to the constructor, which
41 * the class could test itself.
45 * offset 257 string ustar\040\040\0
49 #define longest_magic 265
52 archive::extract (io_stream
* original
)
56 char magic
[longest_magic
];
57 if (original
->peek (magic
, longest_magic
) > 0)
59 if (memcmp (&magic
[257], "ustar\040\040\0", 8) == 0
60 || memcmp (&magic
[257], "ustar\0", 6) == 0)
63 archive_tar
*rv
= new archive_tar (original
);
69 else if (memcmp (magic
, "BZh", 3) == 0)
71 archive_bz
*rv
= new archive_bz (original
);
81 archive::extract_results
82 archive::extract_file (archive
* source
, const std::string
& prefixURL
,
83 const std::string
& prefixPath
, std::string suffix
)
85 extract_results res
= extract_other
;
88 const std::string destfilename
= prefixURL
+ prefixPath
89 + source
->next_file_name() + suffix
;
90 switch (source
->next_file_type ())
92 case ARCHIVE_FILE_REGULAR
:
94 /* TODO: remove in-the-way directories via mkpath_p */
95 if (io_stream::mkpath_p (PATH_TO_FILE
, destfilename
, 0755))
97 Log (LOG_TIMESTAMP
) << "Failed to make the path for " << destfilename
102 io_stream::remove (destfilename
);
103 io_stream
*in
= source
->extract_file ();
106 Log (LOG_TIMESTAMP
) << "Failed to extract the file "
107 << destfilename
<< " from the archive"
112 io_stream
*tmp
= io_stream::open (destfilename
, "wb", in
->get_mode ());
116 Log (LOG_TIMESTAMP
) << "Failed to open " << destfilename
;
117 Log (LOG_TIMESTAMP
) << " for writing." << endLog
;
120 else if (io_stream::copy (in
, tmp
))
122 Log (LOG_TIMESTAMP
) << "Failed to output " << destfilename
126 io_stream::remove (destfilename
);
131 tmp
->set_mtime (in
->get_mtime ());
138 case ARCHIVE_FILE_SYMLINK
:
139 if (io_stream::mkpath_p (PATH_TO_FILE
, destfilename
, 0755))
141 Log (LOG_TIMESTAMP
) << "Failed to make the path for %s"
142 << destfilename
<< endLog
;
147 io_stream::remove (destfilename
);
148 int x
= io_stream::mklink (destfilename
,
149 prefixURL
+ source
->linktarget (),
151 /* FIXME: check what tar's filelength is set to for symlinks */
152 source
->skip_file ();
153 res
= x
== 0 ? extract_ok
: extract_inuse
;
156 case ARCHIVE_FILE_HARDLINK
:
157 if (io_stream::mkpath_p (PATH_TO_FILE
, destfilename
, 0755))
159 Log (LOG_TIMESTAMP
) << "Failed to make the path for %s"
160 << destfilename
<< endLog
;
165 io_stream::remove (destfilename
);
166 int x
= io_stream::mklink (destfilename
,
167 prefixURL
+ prefixPath
+ source
->linktarget (),
169 /* FIXME: check what tar's filelength is set to for hardlinks */
170 source
->skip_file ();
171 res
= x
== 0 ? extract_ok
: extract_inuse
;
174 case ARCHIVE_FILE_DIRECTORY
:
176 char *path
= (char *) alloca (destfilename
.size());
177 strcpy (path
, destfilename
.c_str());
178 while (path
[0] && path
[strlen (path
) - 1] == '/')
179 path
[strlen (path
) - 1] = 0;
180 io_stream
*in
= source
->extract_file ();
181 int x
= io_stream::mkpath_p (PATH_TO_DIR
, path
, in
->get_mode ());
183 source
->skip_file ();
184 res
= x
== 0 ? extract_ok
: extract_other
;
187 case ARCHIVE_FILE_INVALID
:
188 source
->skip_file ();
196 archive::~archive () {};
199 ssize_t
archive::read (void *buffer
, size_t len
)
201 Log (LOG_TIMESTAMP
, "archive::read called");
205 ssize_t
archive::write (void *buffer
, size_t len
)
207 Log (LOG_TIMESTAMP
, "archive::write called");
211 ssize_t
archive::peek (void *buffer
, size_t len
)
213 Log (LOG_TIMESTAMP
, "archive::peek called");
220 Log (LOG_TIMESTAMP
, "bz::tell called");
227 Log (LOG_TIMESTAMP
, "archive::error called");
232 archive::next_file_name ()
234 Log (LOG_TIMESTAMP
, "archive::next_file_name called");
This page took 0.045518 seconds and 5 git commands to generate.