2 // Copyright (C) 2001 Mike Krueger
\r
4 // This program is free software; you can redistribute it and/or
\r
5 // modify it under the terms of the GNU General Public License
\r
6 // as published by the Free Software Foundation; either version 2
\r
7 // of the License, or (at your option) any later version.
\r
9 // This program is distributed in the hope that it will be useful,
\r
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 // GNU General Public License for more details.
\r
14 // You should have received a copy of the GNU General Public License
\r
15 // along with this program; if not, write to the Free Software
\r
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
\r
18 // Linking this library statically or dynamically with other modules is
\r
19 // making a combined work based on this library. Thus, the terms and
\r
20 // conditions of the GNU General Public License cover the whole
\r
23 // As a special exception, the copyright holders of this library give you
\r
24 // permission to link this library with independent modules to produce an
\r
25 // executable, regardless of the license terms of these independent
\r
26 // modules, and to copy and distribute the resulting executable under
\r
27 // terms of your choice, provided that you also meet, for each linked
\r
28 // independent module, the terms and conditions of the license of that
\r
29 // module. An independent module is a module which is not derived from
\r
30 // or based on this library. If you modify this library, you may extend
\r
31 // this exception to your version of the library, but you are not
\r
32 // obligated to do so. If you do not wish to do so, delete this
\r
33 // exception statement from your version.
\r
39 namespace ICSharpCode.SharpZipLib.Tar
\r
43 /// This class represents an entry in a Tar archive. It consists
\r
44 /// of the entry's header, as well as the entry's File. Entries
\r
45 /// can be instantiated in one of three ways, depending on how
\r
46 /// they are to be used.
\r
48 /// TarEntries that are created from the header bytes read from
\r
49 /// an archive are instantiated with the TarEntry( byte[] )
\r
50 /// constructor. These entries will be used when extracting from
\r
51 /// or listing the contents of an archive. These entries have their
\r
52 /// header filled in using the header bytes. They also set the File
\r
53 /// to null, since they reference an archive entry not a file.</p>
\r
55 /// TarEntries that are created from Files that are to be written
\r
56 /// into an archive are instantiated with the TarEntry( File )
\r
57 /// constructor. These entries have their header filled in using
\r
58 /// the File's information. They also keep a reference to the File
\r
59 /// for convenience when writing entries.</p>
\r
61 /// Finally, TarEntries can be constructed from nothing but a name.
\r
62 /// This allows the programmer to construct the entry by hand, for
\r
63 /// instance when only an InputStream is available for writing to
\r
64 /// the archive, and the header information is constructed from
\r
65 /// other information. In this case the header fields are set to
\r
66 /// defaults and the File is set to null.</p>
\r
68 /// <see cref="TarHeader"/>
\r
70 public class TarEntry
\r
73 /// If this entry represents a File, this references it.
\r
78 /// This is the entry's header information.
\r
83 /// Only Create Entries with the static CreateXYZ methods or a headerBuffer.
\r
90 /// Construct an entry from an archive's header bytes. File is set
\r
93 /// <param name = "headerBuf">
\r
94 /// The header bytes from a tar archive entry.
\r
96 public TarEntry(byte[] headerBuf)
\r
99 this.header.ParseBuffer(headerBuf);
\r
103 public TarEntry(TarHeader header)
\r
106 this.header = header;
\r
110 /// Construct an entry with only a name. This allows the programmer
\r
111 /// to construct the entry's header "by hand". File is set to null.
\r
113 public static TarEntry CreateTarEntry(string name)
\r
115 TarEntry entry = new TarEntry();
\r
116 entry.Initialize();
\r
117 entry.NameTarHeader(entry.header, name);
\r
122 /// Construct an entry for a file. File is set to file, and the
\r
123 /// header is constructed from information from the file.
\r
125 /// <param name = "fileName">
\r
126 /// The file that the entry represents.
\r
128 public static TarEntry CreateEntryFromFile(string fileName)
\r
130 TarEntry entry = new TarEntry();
\r
131 entry.Initialize();
\r
132 entry.GetFileTarHeader(entry.header, fileName);
\r
137 /// Initialization code common to all pseudo constructors.
\r
142 this.header = new TarHeader();
\r
146 /// Determine if the two entries are equal. Equality is determined
\r
147 /// by the header names being equal.
\r
150 /// True if the entries are equal.
\r
152 public override bool Equals(object it)
\r
154 if (!(it is TarEntry))
\r
158 return this.header.name.ToString().Equals(((TarEntry)it).header.name.ToString());
\r
162 /// Must be overridden when you override Equals.
\r
164 public override int GetHashCode()
\r
166 return this.header.name.ToString().GetHashCode();
\r
171 /// Determine if the given entry is a descendant of this entry.
\r
172 /// Descendancy is determined by the name of the descendant
\r
173 /// starting with this entry's name.
\r
175 /// <param name = "desc">
\r
176 /// Entry to be checked as a descendent of this.
\r
179 /// True if entry is a descendant of this.
\r
181 public bool IsDescendent(TarEntry desc)
\r
183 return desc.header.name.ToString().StartsWith(this.header.name.ToString());
\r
187 /// Get this entry's header.
\r
190 /// This entry's TarHeader.
\r
192 public TarHeader TarHeader
\r
196 return this.header;
\r
201 /// Get/Set this entry's name.
\r
203 public string Name
\r
207 return this.header.name.ToString();
\r
211 this.header.name = new StringBuilder(value);
\r
216 /// Get/set this entry's user id.
\r
222 return this.header.userId;
\r
226 this.header.userId = value;
\r
231 /// Get/set this entry's group id.
\r
233 public int GroupId
\r
237 return this.header.groupId;
\r
241 this.header.groupId = value;
\r
246 /// Get/set this entry's user name.
\r
248 public string UserName
\r
252 return this.header.userName.ToString();
\r
256 this.header.userName = new StringBuilder(value);
\r
261 /// Get/set this entry's group name.
\r
263 public string GroupName
\r
267 return this.header.groupName.ToString();
\r
271 this.header.groupName = new StringBuilder(value);
\r
276 /// Convenience method to set this entry's group and user ids.
\r
278 /// <param name="userId">
\r
279 /// This entry's new user id.
\r
281 /// <param name="groupId">
\r
282 /// This entry's new group id.
\r
284 public void SetIds(int userId, int groupId)
\r
291 /// Convenience method to set this entry's group and user names.
\r
293 /// <param name="userName">
\r
294 /// This entry's new user name.
\r
296 /// <param name="groupName">
\r
297 /// This entry's new group name.
\r
299 public void SetNames(string userName, string groupName)
\r
301 UserName = userName;
\r
302 GroupName = groupName;
\r
307 // * Set this entry's modification time. The parameter passed
\r
308 // * to this method is in "Java time".
\r
310 // * @param time This entry's new modification time.
\r
312 // public void setModTime( long time )
\r
314 // this.header.modTime = time / 1000;
\r
317 /// Convert time to DateTimes
\r
319 * Get/Set this entry's modification time.
\r
321 * @param time This entry's new modification time.
\r
323 public DateTime ModTime
\r
327 return this.header.modTime;
\r
331 this.header.modTime = value;
\r
336 /// Get this entry's file.
\r
339 /// This entry's file.
\r
341 public string File
\r
350 /// Get/set this entry's file size.
\r
356 return this.header.size;
\r
360 this.header.size = value;
\r
365 /// Convenience method that will modify an entry's name directly
\r
366 /// in place in an entry header buffer byte array.
\r
368 /// <param name="outbuf">
\r
369 /// The buffer containing the entry header to modify.
\r
371 /// <param name="newName">
\r
372 /// The new name to place into the header buffer.
\r
374 public void AdjustEntryName(byte[] outbuf, string newName)
\r
377 offset = TarHeader.GetNameBytes(new StringBuilder(newName), outbuf, offset, TarHeader.NAMELEN);
\r
381 /// Return whether or not this entry represents a directory.
\r
384 /// True if this entry is a directory.
\r
386 public bool IsDirectory
\r
390 if (this.file != null)
\r
392 return Directory.Exists(file);
\r
395 if (this.header != null)
\r
397 if (this.header.typeFlag == TarHeader.LF_DIR || this.header.name.ToString().EndsWith( "/" ))
\r
407 /// Fill in a TarHeader with information from a File.
\r
409 /// <param name="hdr">
\r
410 /// The TarHeader to fill in.
\r
412 /// <param name="file">
\r
413 /// The file from which to get the header information.
\r
415 public void GetFileTarHeader(TarHeader hdr, string file)
\r
419 // bugfix from torhovl from #D forum:
\r
420 string name = file;
\r
422 // -jr- 23-Jan-2004 HAK HAK HAK, GnuTar allows device names in path where the name is not local to the current directory
\r
423 if (Environment.CurrentDirectory == Path.GetDirectoryName(name))
\r
425 name = Path.GetFileName(name);
\r
428 if (Path.DirectorySeparatorChar == '\\')
\r
429 { // check if the OS is Windows
\r
430 // Strip off drive letters!
\r
431 if (name.Length > 2)
\r
433 char ch1 = name[0];
\r
434 char ch2 = name[1];
\r
436 if (ch2 == ':' && Char.IsLetter(ch1))
\r
438 name = name.Substring(2);
\r
444 name = name.Replace(Path.DirectorySeparatorChar, '/').ToLower();
\r
446 // No absolute pathnames
\r
447 // Windows (and Posix?) paths can start with UNC style "\\NetworkDrive\",
\r
448 // so we loop on starting /'s.
\r
449 while (name.StartsWith("/")) {
\r
450 name = name.Substring(1);
\r
453 hdr.linkName = new StringBuilder(String.Empty);
\r
454 hdr.name = new StringBuilder(name);
\r
456 if (Directory.Exists(file)) {
\r
457 hdr.mode = 1003; // 01753 -jr- no octal constants!! 040755; // Magic number for security access for a UNIX filesystem
\r
458 hdr.typeFlag = TarHeader.LF_DIR;
\r
459 if (hdr.name.Length == 0 || hdr.name[hdr.name.Length - 1] != '/') {
\r
460 hdr.name.Append("/");
\r
465 hdr.mode = 33216; // 0100700 -jr- // 0100644; // Magic number for security access for a UNIX filesystem
\r
466 hdr.typeFlag = TarHeader.LF_NORMAL;
\r
467 hdr.size = new FileInfo(file.Replace('/', Path.DirectorySeparatorChar)).Length;
\r
470 // UNDONE When File lets us get the userName, use it!
\r
471 hdr.modTime = System.IO.File.GetLastWriteTimeUtc(file.Replace('/', Path.DirectorySeparatorChar)); // -jr- Unix times are in UTC
\r
478 /// If this entry represents a file, and the file is a directory, return
\r
479 /// an array of TarEntries for this entry's children.
\r
482 /// An array of TarEntry's for this entry's children.
\r
484 public TarEntry[] GetDirectoryEntries()
\r
486 if (this.file == null || !Directory.Exists(this.file))
\r
488 return new TarEntry[0];
\r
491 string[] list = Directory.GetFileSystemEntries(this.file);
\r
492 TarEntry[] result = new TarEntry[list.Length];
\r
494 for (int i = 0; i < list.Length; ++i)
\r
496 result[i] = TarEntry.CreateEntryFromFile(list[i]);
\r
503 /// Write an entry's header information to a header buffer.
\r
505 /// <param name = "outbuf">
\r
506 /// The tar entry header buffer to fill in.
\r
508 public void WriteEntryHeader(byte[] outbuf)
\r
510 this.header.WriteHeader(outbuf);
\r
514 /// Fill in a TarHeader given only the entry's name.
\r
516 /// <param name="hdr">
\r
517 /// The TarHeader to fill in.
\r
519 /// <param name="name">
\r
520 /// The tar entry name.
\r
522 public void NameTarHeader(TarHeader hdr, string name)
\r
524 bool isDir = name.EndsWith("/"); // -jr- this is true for BSD tar but not all others I think?
\r
528 hdr.name = new StringBuilder(name);
\r
529 // hdr.mode = isDir ? 040755 : 0100644; // TODO : I think I've seen these magics before ...
\r
530 hdr.mode = isDir ? 1003 : 33216;
\r
536 hdr.modTime = DateTime.UtcNow; // -jr- 24-Jan-2004 Unix times are in utc!
\r
537 // hdr.modTime = DateTime.Now; // (new java.util.Date()).getTime() / 1000;
\r
539 hdr.typeFlag = isDir ? TarHeader.LF_DIR : TarHeader.LF_NORMAL;
\r
541 hdr.linkName = new StringBuilder(String.Empty);
\r
542 hdr.userName = new StringBuilder(String.Empty);
\r
543 hdr.groupName = new StringBuilder(String.Empty);
\r
553 /* The original Java file had this header:
\r
555 ** Authored by Timothy Gerard Endres
\r
556 ** <mailto:time@gjt.org> <http://www.trustice.com>
\r
558 ** This work has been placed into the public domain.
\r
559 ** You may use this work in any way and for any purpose you wish.
\r
561 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
\r
562 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
\r
563 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
\r
564 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
\r
565 ** REDISTRIBUTION OF THIS SOFTWARE.
\r