📄 main.cs
字号:
try {
this.blockingFactor = Int32.Parse(argValue);
if ( blockingFactor <= 0 ) {
Console.Error.WriteLine("Blocking factor {0} is invalid", blockingFactor);
bailOut = true;
}
} catch {
Console.Error.WriteLine("invalid blocking factor");
}
}
} else if (arg.Equals("--verbose")) {
verbose = true;
} else if (arg.Equals("--keep-old-files")) {
keepOldFiles = true;
} else if (arg.Equals("--record-size")) {
if (argValue == null || argValue.Length == 0) {
Console.Error.WriteLine("expected numeric record size");
bailOut = true;
} else {
int size;
try
{
size = Int32.Parse(argValue);
if (size % TarBuffer.BlockSize != 0) {
Console.Error.WriteLine("Record size must be a multiple of " + TarBuffer.BlockSize.ToString());
bailOut = true;
} else
blockingFactor = size / TarBuffer.BlockSize;
} catch {
Console.Error.WriteLine("non-numeric record size");
bailOut = true;
}
}
} else {
Console.Error.WriteLine("unknown option: " + arg);
ShowHelp();
Environment.Exit(1);
}
} else {
for (int cIdx = 1; cIdx < arg.Length; ++cIdx) {
switch (arg[cIdx])
{
case '?':
ShowHelp();
Environment.Exit(1);
break;
case 'f':
this.archiveName = args[++idx];
break;
case 'j':
compression = Compression.Bzip2;
break;
case 'z':
compression = Compression.Gzip;
break;
case 'Z':
compression = Compression.Compress;
break;
case 'e':
asciiTranslate = true;
break;
case 'c':
gotOP = true;
operation = Operation.Create;
break;
case 'x':
gotOP = true;
operation = Operation.Extract;
break;
case 't':
gotOP = true;
operation = Operation.List;
break;
case 'k':
keepOldFiles = true;
break;
case 'b':
blockingFactor = Int32.Parse(args[++idx]);
break;
case 'u':
userName = args[++idx];
break;
case 'U':
userId = Int32.Parse(args[ ++idx ]);
break;
case 'g':
groupName = args[++idx];
break;
case 'G':
groupId = Int32.Parse(args[ ++idx ]);
break;
case 'v':
verbose = true;
break;
default:
Console.Error.WriteLine("unknown option: " + arg[cIdx]);
ShowHelp();
Environment.Exit(1);
break;
}
}
}
}
if (!gotOP) {
Console.Error.WriteLine("you must specify an operation option (c, x, or t)");
Console.Error.WriteLine("Try tar --help");
bailOut = true;
}
if (bailOut == true) {
Environment.Exit(1);
}
return idx;
}
static string[] GetFilesForSpec(string spec)
{
string dir = Path.GetDirectoryName(spec);
if (dir == null || dir.Length == 0)
dir = Directory.GetCurrentDirectory();
return System.IO.Directory.GetFiles(dir, Path.GetFileName(spec));
}
static string DecodeType(int type, bool slashTerminated)
{
string result = "?";
switch (type)
{
case TarHeader.LF_OLDNORM: // -jr- TODO this decoding is incomplete, not all possible known values are decoded...
case TarHeader.LF_NORMAL:
case TarHeader.LF_LINK:
if (slashTerminated)
result = "d";
else
result = "-";
break;
case TarHeader.LF_DIR:
result = "d";
break;
case TarHeader.LF_GNU_VOLHDR:
result = "V";
break;
case TarHeader.LF_GNU_MULTIVOL:
result = "M";
break;
case TarHeader.LF_CONTIG:
result = "C";
break;
case TarHeader.LF_FIFO:
result = "p";
break;
case TarHeader.LF_SYMLINK:
result = "l";
break;
case TarHeader.LF_CHR:
result = "c";
break;
case TarHeader.LF_BLK:
result = "b";
break;
}
return result;
}
static string DecodeMode(int mode)
{
const int S_ISUID = 0x0800;
const int S_ISGID = 0x0400;
const int S_ISVTX = 0x0200;
const int S_IRUSR = 0x0100;
const int S_IWUSR = 0x0080;
const int S_IXUSR = 0x0040;
const int S_IRGRP = 0x0020;
const int S_IWGRP = 0x0010;
const int S_IXGRP = 0x0008;
const int S_IROTH = 0x0004;
const int S_IWOTH = 0x0002;
const int S_IXOTH = 0x0001;
System.Text.StringBuilder result = new System.Text.StringBuilder();
result.Append((mode & S_IRUSR) != 0 ? 'r' : '-');
result.Append((mode & S_IWUSR) != 0 ? 'w' : '-');
result.Append((mode & S_ISUID) != 0
? ((mode & S_IXUSR) != 0 ? 's' : 'S')
: ((mode & S_IXUSR) != 0 ? 'x' : '-'));
result.Append((mode & S_IRGRP) != 0 ? 'r' : '-');
result.Append((mode & S_IWGRP) != 0 ? 'w' : '-');
result.Append((mode & S_ISGID) != 0
? ((mode & S_IXGRP) != 0 ? 's' : 'S')
: ((mode & S_IXGRP) != 0 ? 'x' : '-'));
result.Append((mode & S_IROTH) != 0 ? 'r' : '-');
result.Append((mode & S_IWOTH) != 0 ? 'w' : '-');
result.Append( (mode & S_ISVTX) != 0
? ((mode & S_IXOTH) != 0 ? 't' : 'T')
: ((mode & S_IXOTH) != 0 ? 'x' : '-'));
return result.ToString();
}
static string SharpZipVersion()
{
System.Reflection.Assembly zipAssembly = System.Reflection.Assembly.GetAssembly(new TarHeader().GetType());
Version v = zipAssembly.GetName().Version;
return "#ZipLib v" + v.Major + "." + v.Minor + "." + v.Build + "." + v.Revision;
}
/// <summary>
/// Print version information.
/// </summary>
static void Version()
{
Console.Error.WriteLine( "tar 2.0.6.2" );
Console.Error.WriteLine( "" );
Console.Error.WriteLine( "{0}", SharpZipVersion() );
Console.Error.WriteLine( "Copyright (c) 2002 by Mike Krueger" );
Console.Error.WriteLine( "Copyright (c) 1998,1999 by Tim Endres (Java version)" );
Console.Error.WriteLine( "" );
Console.Error.WriteLine( "This program is free software licensed to you under the" );
Console.Error.WriteLine( "GNU General Public License. See the accompanying LICENSE" );
Console.Error.WriteLine( "file, or the webpage <http://www.gjt.org/doc/gpl> or," );
Console.Error.WriteLine( "visit www.gnu.org for more details." );
Console.Error.WriteLine( "" );
}
/// <summary>
/// Print help information.
/// </summary>
static private void ShowHelp()
{
Console.Error.WriteLine( "Usage: tar [option]... [file]..." );
Console.Error.WriteLine( "" );
Console.Error.WriteLine( "Examples:" );
Console.Error.WriteLine( " tar -cf archive.tar foo bar # create archive.tar from files foo and bar" );
Console.Error.WriteLine( " tar -tvf archive.tar # List all files in archive tar verbosely" );
Console.Error.WriteLine( " tar -xvf archive.tar # Extract all files from archive.tar" );
Console.Error.WriteLine( "" );
Console.Error.WriteLine( "Main operation mode:" );
Console.Error.WriteLine( " -t, --list list the contents of an archive" );
Console.Error.WriteLine( " -x, --extract extract files from an archive" );
Console.Error.WriteLine( " -c, --create create a new archive" );
Console.Error.WriteLine( "" );
Console.Error.WriteLine( "Options:" );
Console.Error.WriteLine( " -f file, use 'file' as the tar archive" );
Console.Error.WriteLine( " -e, Turn on ascii translation" );
Console.Error.WriteLine( " -z, --gzip use gzip compression" );
Console.Error.WriteLine( " -Z, --compress use unix compress" );
Console.Error.WriteLine( " -j, --bzip2 use bzip2 compression" );
Console.Error.WriteLine( " -k, --keep-old-files dont overwrite existing files when extracting" );
Console.Error.WriteLine( " -b blks, set blocking factor (blks * 512 bytes per record)" );
Console.Error.WriteLine( " --record-size=SIZE SIZE bytes per record, multiple of 512");
Console.Error.WriteLine( " -u name, set user name to 'name'" );
Console.Error.WriteLine( " -U id, set user id to 'id'" );
Console.Error.WriteLine( " -g name, set group name to 'name'" );
Console.Error.WriteLine( " -G id, set group id to 'id'" );
Console.Error.WriteLine( "" );
Console.Error.WriteLine( "Informative output:" );
Console.Error.WriteLine( " -?, --help print this help then exit" );
Console.Error.WriteLine( " --version, print tar program version information" );
Console.Error.WriteLine( " -v, --verbose verbosely list files processed" );
Console.Error.WriteLine( "" );
Console.Error.WriteLine( "The translation option -e will translate from local line" );
Console.Error.WriteLine( "endings to UNIX line endings of '\\n' when writing tar" );
Console.Error.WriteLine( "archives, and from UNIX line endings into local line endings" );
Console.Error.WriteLine( "when extracting archives." );
Console.Error.WriteLine( "" );
Console.Error.WriteLine( "This tar defaults to -b " + TarBuffer.DefaultBlockFactor.ToString());
Environment.Exit(1);
}
}
/*
** Authored by Timothy Gerard Endres
** <mailto:time@gjt.org> <http://www.trustice.com>
**
** This work has been placed into the public domain.
** You may use this work in any way and for any purpose you wish.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -