The hidden trouble of the tar command of UNIX operating system and the solving method

Source: Internet
Author: User
Tags define exit functions header include integer sprintf file permissions
Unix| solution at present, Unix operating system in China's financial sector is widely used, UNIX with its powerful functions (time-sharing, multitasking, multi-user, network interconnection, graphics interface, etc.), much favored by financial enterprises. Agricultural Bank of China is currently applying the SCO UNIX openserver50 is more powerful.
The bank's savings, accounting, credit cards and other computer business processing systems are running on the UNIX operating system platform. The development of electronic has expanded the bank's business field, improved the working efficiency, strengthened the accuracy, confidentiality and security of the business, established the social image of the Bank, and produced indirect economic benefits. The development of electronic bank puts forward higher requirements for the reliability of computer data.
According to the author, most of the procedures for backing up and restoring data on UNIX operating systems are implemented by the TAR command. The tar command features simple, easy to use and easy to learn. But the author in the process of using the tar command, found that the tar command for Chinese users have a serious problem: The file name is Chinese characters and long files can be archived packaging, but can not solve the file package.
For example:
1 first create a long Chinese character file name file:
# cd/tmp
# cat/etc/passwd>, long, long, long, long and long
2 Archive the file to the ABC package:
# Mtar CVF ABC *
3 to untie or view the ABC archive package:
# tar XVF ABC or ATR TVF ABC
The ABC portfolio will not be unpacked or viewed.

Second, analysis
The file generated by the Unix tar command is called the TAR format archive, which has the following format:
1 each file is added with a 512-byte file attribute header, and then a block of 512 bytes is kept in the packet, holding the whole number of blocks. The last block can not be filled, and then filled with 0x00.
2 If the file length is 0 bytes or linked files, only 512 bytes of file attribute headers.
The 3 uses a 1024-byte 0x00 as the end of the file.
4 File Property Header structure:
Union Hblock {
char dummy [512]; 512-byte file property header
struct Header {
Char name[100]; 100 bytes within filename
char mode [8]; octal file permissions
Char uid[8]; The master number of the octal file
Char gid[8]; octal file group number
Char size[12]; octal file length
Char mtime[12]; octal file modification time
Char chksum[8]; Octal Property Header Checksum
char 1inkf1ag; File connection Status
Char 1inkname[100]; connection file name
Char extno[4]; Continuous volume sub-volume number
Char extota1[4]; number of split volumes
Char efsize[12]; octal continuous volume file length
char compid; file compression status
}dbuf;
}dblock;
The byte checksum chksum in the file property header structure (header structure except chksum part byte and) plus (octal number 400) plus (file compression state value) is converted to octal system. File compression status of ' 1 ' indicates that the file content is in a compressed state, and the TAR command will automatically invoke compress to extract the contents of the file without changing the filename when the package is unpacked.
In the analysis of an archive file with long Chinese characters file name, I found that the Chksum value in the attribute header of long Chinese character file name is wrong. The reason for this error is: The byte of a Chinese character and the negative integer, the byte of the attribute header of the long Chinese character file name and the possible negative integer, the tar command source program was designed to fail to determine the attribute header byte and negative for the western language. When the file was created, the tar command converted the property header byte and the octal output to chksum with the sprintf () function, destroying the Chksum normal format. When the file is opened, the tar command does not get the correct data when it uses the sscanf () function to format the octal from the property header to read the Chksum, and the tar command aborts the expansion of the file.

Third, the solution
From the above analysis we draw the following conclusions: (1) To solve the problem, we must revise the TAR source program, fully consider the case that the byte checksum is negative in the file attribute header of the tar file package produced by the Chinese Unix. (2) write a patch to fix the error in the Tar profile property header in byte checksum chksum.
The first method needs to get the UNIX company source program level technical support or by the UNIX company technical personnel solves, this is also the author to the UNIX company's proposal, we can only expect.
The second method of the author made an effective attempt, and C wrote a patch mtar.c, compiled into Mtar run the program, this program has the following functions:
Amtar-v TARFI1E fixes any cause of the file chksum errors in the TAR Archive package (including the program-C feature).
Bmtar-t tarfi1e The file information in the TAR archive package.
Cmtar-c tarfi1e encrypts the tar file pack so that the tar command cannot open the package.
Dmtar-p tarfi1e The compression status flag for all files in the package.
Emtar-u tarfi1e The uncompressed status flag for all files in the package.

Iv. Practical Application
This program uses 5 options-t-c-v-p-u, only one parameter at a time, and one function for each parameter.
For example: MTAR-V ABC can repair the above mentioned ABC package not open the problem.
Mtar-v/DEV/FD0135DS18 can repair the TAR format 3″ floppy disk.
Mtar-c/dev/fd0135ds18 can encrypt the TAR format 3″ floppy disk.
Mtar-v/dev/fd0135ds18 can decrypt the TAR format 3″ floppy disk.
MTAR-T ABC can detail the file information in the ABC package, sum_v=0 indicates that the file property header checksum is normal, compress=[1] is automatically decompressed when the file is extracted.
This procedure from writing to date the author has made 14 revisions, so that the program is suitable for any file type of tar file package. And in SCO UNIX 3242 and SCO OpenServer 50 many times under the compilation and comprehensive functional testing. Now the procedure is sorted out, please the peer to add advice. The source program is attached.
MTAR.C contents are as follows:
#include″stdio.h″
#include″string.h″
#include″unistd.h″
#include″sys/types.h″
#include″sys/stat.h″
#include″fcntl.h″

#define TBLOCK 512
#define Nblock 20
#define NAMSIZ 100
Union Hblock {
Char Dummy[tblock];
struct Header {
Char Name[namsiz];
Char Mode[8];
Char Uid[8];
Char Gid[8];
Char size[12];
Char mtime[12];
Char Chksum[8];
Char Linkflag;
Char Linkname[namsiz];
Char extno[4];
Char extotal[4];
Char efsize[12];
Char Compid;
} dbuf,
} DBLock;
Main (ARGC,ARGV)
int argc;
Char *argv[];
{
Char compress;
int I,SEEKIP=0,IP,COMPC;
Long sum,sum_v,filesize=0,mvblock=0,total;
FILE*FP;
Ip=0;
if (strncmp (argv[1],″-c″,2) ==0) ip=1;
if (strncmp (argv[1],″-v″,2) ==0) ip=1;
if (strncmp (argv[1],″-t″,2) ==0) ip=1;
if (strncmp (argv[1],″-p″,2) ==0) ip=1;
if (strncmp (argv[1],″-u″,2) ==0) ip=1;

if (argc!=3| | IP!=1)
{
printf (″usage:mtar-[c,v,t,p,u]tarfile\n″);
Exit (1);
}
if ((Fp=fopen (Argv[2],″r+″)) ==null)
{
printf (″can not open the%s\n″,argv[2]);
Exit (1);
}
while (seekip==0)
{
if (Fread (dblock.dummy), TBLOCK,1,FP)!=1)
{
printf (″can not read the%s!\n″,argv[2]);
Break
}
sum=0;
compc=0;
for (i=0;i< tblock;i++) Sum=sum+dblock.dumm
Y[i];
for (i=0;i< 8,i++) sum=sum-dblock.dbuf.chksum[i];
if (sum==0) break;

Ip=0;
if (strncmp (argv[1],″-c″,2) ==0)
Compress=dblock.dbuf.compid;
sum_v=270*0xff+0400; Ip=1;
}
if (strncmp (argv[1],″-v″,2) ==0)
Compress=dblock.dbuf.compid;
sum_v=sum+0400; Ip=1
}
if (strncmp (argv[1],″-p″,2) ==0)
Compc=dblock.dbuf.compid;
COMPC=0X31-COMPC;
SUM_V=SUM+0400+COMPC;
compress=0x31; Ip=1;
}
if (strncmp (argv[1],″-u″,2) ==0)
Compc=dblock.dbuf.compid;
COMPC=0X00-COMPC;
SUM_V=SUM+0400+COMPC;
compress=0x00; Ip=1;
}
if (ip==1)
{
if (sum-v >=0) sprintf (dblock.dbuf.
CHKSUM,″%60″,SUM_V);
Else
{
dblock.dbuf.chksum[0]=′-′;
dblock.dbuf.chksum[6]=0x00;
dblock.dbuf.chksum[7]=0x00;
Sum_v=sum_v-dblock.dbuf.linkflag;
Sum=sum-dblock.dbuf.linkflag;
dblock.dbuf.linkflag=0x00;
for (i=0;i<namsiz;i++) {
Sum_v=sum_v-dblock.dbuf.linkname[i];
Sum=sum-dblock.dbuf.linkname[i];
dblock.dbuf.linkname[i]=0x00;
}
sprintf (DBLOCK.DBUF.CHKSUM+1,″%-50″,-SUM_V);
}
sprintf (&dblock.dbuf.compid,″%c″,compress);
Seekip=fseek (fp,-tblock,seek_cur);
if (seekip==0)
{
if (fwrite (dblock.dummy), TBLOCK,1,FP)!=1)
{
printf (″can not read the%s!\n″,argv[2]);
Break
}
Fflush (FP);
}
}
SSCANF (dblock.dbuf.size,″%12o″,&filesize);
SSCANF (DBLOCK,DBUF.CHKSUM,″%6O″,&SUM_V);
SUM_V=SUM_V-SUM-0400-COMPC;
if (filesize>0&& dblock.dbuf.linkflag==0x00| | dbloc
K.DBUF.CHKSUM[0]==0X33))
{
mvblock= (filesize-1)/tblock+1;
Seekip=fseek (FP, (long) Mvblock*tblock,seek
_cur);
}
Seekip=fseek (fp,0l,seek_cur);
if (Dblock.dbuf.linkflag==″1″)
printf (″%s\n\t\tnormal linked to%s\t\tcompress=[%c]\tsum_v=%o\n″,
Dblock.dbuf.name,dblock.dbuf.linkname,dblock.dbuf.
COMPID,SUM-V);
else if (Dblock.dbuf.linkflag==″2″)
printf (″%s\n\t\tsymbolic linked to%s\tcompress=[%c]\tsum_v=%o\n,
″dblock.dbuf.name,dblock.dbuf.linkname,dblock.dbuf.
COMPID,SUM_V);
Else
printf (″%s\n\t%8d byte-->%6d Tape_blocks\tcompress=[%c]\tsum_v=%o\n″,dblock.dbuf.name,filesize,mvblock, DBLOCK.DBUF.CMPID,SUM_V);

}
printf (″total=%dk\n″,ftell (FP)/1024);
Fclose (FP);
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.