📄 zooadd.c
字号:
}
#else
# ifdef CHEKUDIR
/* Don't add if this is a directory */
if (isuadir (this_path)) {
zooclose (this_file);
continue;
}
# endif /* CHEKUDIR */
#endif /* CHEKDIR */
/* Create directory entry for new file (but don't add just yet) */
/* NOTE: we already got file date and time above for update option */
/* add tag, type, timezone, struc, system_id, and var_dir_len */
newdir (&direntry);
if (!genson && zoo_status == NEW_ZOO ||
(zoo_header.vdata & VFL_ON) == 0) {
direntry.vflag = 0;
direntry.version_no = 0;
}
/*
Write a null direntry entry. Thus, if an error occurs or the program
is interrupted, the end of the archive will still be meaningful.
Special check needed for first one written.
*/
direntry.next = direntry.offset = 0L; /* trailing null entry */
this_dir_offset = zootell (zoo_file);
if (!firstfile) {
writedir (&direntry, zoo_file);
} else {
/*
Before adding the first file to the archive, we must make sure that
the previous directory chain (if any) is properly terminated with a
null entry of the right size. If this is a new archive, we simply
write a new null entry of the right size. If this is an existing
archive, we must check the size of the previous trailing null entry.
If it is too small, we will back up to the most recent real directory
entry and change its .next field to point to end of file.
*/
if (zoo_status == NEW_ZOO) {
writedir (&direntry, zoo_file); /* write null dir entry */
} else {
struct direntry tmpentry;
long tmppos;
int oldlen, newlen;
tmppos = zootell (zoo_file);
frd_dir (&tmpentry, zoo_file);
#define DIRLEN(x) ((x.type<2) ? SIZ_DIR : (SIZ_DIRL+x.var_dir_len))
oldlen = DIRLEN(tmpentry); /* get length of direntry */
newlen = DIRLEN(direntry); /* ditto */
if (newlen > oldlen) { /* trouble */
zooseek (zoo_file, last_old, 0); /* back to previous entry */
frd_dir (&tmpentry, zoo_file);
zooseek (zoo_file, 0L, 2); /* get EOF position */
tmpentry.next = zootell (zoo_file); /* point to EOF */
zooseek (zoo_file, last_old, 0); /* back to previous entry */
writedir (&tmpentry, zoo_file); /* update it */
zooseek (zoo_file, 0L, 2); /* to EOF ... */
this_dir_offset = zootell (zoo_file);
writedir (&direntry, zoo_file); /* ...write null dir entry */
} else
zooseek (zoo_file, tmppos, 0); /* long enough -- let it be */
} /* if (zoo_status == NEW_ZOO) ... */
} /* if (!firstfile) ... */
/* Now `this_dir_offset' is where the next directory entry will go */
/* first file added goes at EOF to avoid overwriting comments */
if (firstfile) {
zooseek (zoo_file, 0L, 2); /* EOF */
direntry.offset = zootell (zoo_file) + SIZ_FLDR;
} else {
direntry.offset = this_dir_offset + SIZ_DIRL +
direntry.var_dir_len + SIZ_FLDR;
}
if (use_lzh) {
direntry.major_ver = MAJOR_LZH_VER; /* minimum version number needed */
direntry.minor_ver = MINOR_LZH_VER; /* .. to extract */
} else {
direntry.major_ver = MAJOR_EXT_VER; /* minimum version number needed */
direntry.minor_ver = MINOR_EXT_VER; /* .. to extract */
}
direntry.deleted = 0; /* not deleted, naturally */
direntry.comment = 0L; /* no comment (yet) */
direntry.cmt_size = 0; /* .. so no size either */
save_position = direntry.offset; /* save position in case of error */
(void) zooseek (zoo_file, direntry.offset - SIZ_FLDR, 0);
(void) zoowrite (zoo_file, file_leader, SIZ_FLDR);
#ifdef PORTABLE
prterror ('m', "%-*s -- ", longest, this_path);
#else
if (z_fmt)
prterror ('m', "%-12s <== %-*s -- ",
direntry.fname, longest, this_path);
else
prterror ('m', "%-*s -- ", longest, this_path);
#endif /* PORTABLE */
crccode = 0;
#ifndef PORTABLE
if (z_fmt)
{
direntry.packing_method = tiny_header.packing_method;
zooseek (this_file, (long) (sizeof(tiny_header)+tiny_header.cmt_size), 0);
status = getfile (this_file, zoo_file, tiny_header.size_now, 1);
} else
#endif
if (suppress) { /* suppress compression */
direntry.packing_method = 0; /* no compression */
status = getfile (this_file, zoo_file, -1L, 1);
} else {
#ifdef UNBUF_IO /* unbuffered I/O */
long lseek PARMS ((int, long, int));
long tell PARMS ((int));
int this_fd, zoo_fd;
#endif
if (use_lzh)
direntry.packing_method = 2;
else
direntry.packing_method = 1;
#ifdef UNBUF_IO
#include "UNBUF_IO not currently supported"
this_fd = fileno (this_file); /* get .. */
zoo_fd = fileno (zoo_file); /* ... file descriptors */
zooseek (zoo_file, zootell (zoo_file), 0); /* synch */
zooseek (this_file, zootell (this_file), 0); /* synch */
status = lzc(this_fd, zoo_fd); /* add with compression */
zooseek (zoo_file, tell (zoo_fd), 0); /* resynch */
zooseek (this_file, tell (this_fd), 0); /* resynch */
#else
if (use_lzh)
status = lzh_encode(this_file, zoo_file);
else
status = lzc(this_file, zoo_file);
#endif /* UNBUF_IO */
}
if (status != 0) { /* if I */
++exit_status; /* remember error */
if (status == 1)
prterror ('F', no_memory);
else if (status == 2)
prterror ('F', disk_full);
else if (status == 3)
prterror ('F', "Read error.\n");
else
prterror ('F', internal_error);
success = 0;
} else {
direntry.next = zootell (zoo_file);
direntry.size_now = direntry.next - direntry.offset;
/* find and store original size of file just compressed */
/*DEBUG VMS*/ zooseek (this_file, 0L, 2); /* seek to EOF */
direntry.org_size = zootell (this_file); /* should be EOF already */
/* If the compressed one is bigger, just copy */
if (direntry.size_now >= direntry.org_size && /* if II */
direntry.packing_method != 0) {
zooseek (zoo_file, save_position, 0); /* ..restore file pointer */
zootrunc (zoo_file); /* ..truncate file */
direntry.packing_method = 0; /* ..and just copy */
zooseek (this_file, 0L, 0); /* (but rewind first!) */
crccode = 0; /* re-start crc from 0 */
status = getfile (this_file, zoo_file, -1L, 1);
if (status != 0) { /* if III */
success = 0;
printf (disk_full);
exit_status++;
} else {
success = 1;
direntry.next = zootell (zoo_file);
direntry.size_now = direntry.next - direntry.offset;
} /* end if III */
} else {
success = 1;
} /* end if II */
} /* end if I */
if (success) { /* file successfully added */
addcount++; /* how many added */
direntry.file_crc = crccode;
/* remember most recent date and time */
if (cmpnum (direntry.date,direntry.time,latest_date,latest_time) > 0) {
latest_date = direntry.date;
latest_time = direntry.time;
}
#if 0
/* mark any previous version of this file in archive as deleted */
dir2entry.comment = 0L; /* for later use assigning to direntry */
dir2entry.cmt_size = 0;
#endif
if (!z_fmt)
prterror ('M', " (%2d%%) ", cfactor (direntry.org_size, direntry.size_now));
oldcmtsiz = 0; /* assume no old comment */
oldcmtpos = 0L;
if (prev_pos > 0) { /* in archive */
int delold = 0; /* delete old? */
/* if versions active both archive-wide and for file */
if ((zoo_header.vdata & VFL_ON) && (high_vflag & VFL_ON)) {
/* next test is optimization, to avoid redundant I/O */
if (high_pos != prev_pos || this_version_no == 1) {
/* prev highest is no longer highest so adjust vflag */
long save_pos = zootell (zoo_file); /*DEBUG*/
zooseek (zoo_file, high_pos, 0);
readdir (&dir2entry, zoo_file, 1);
oldcmtpos = dir2entry.comment;
oldcmtsiz = dir2entry.cmt_size;
dir2entry.vflag &= (~VFL_LAST); /* no longer highest */
zooseek (zoo_file, high_pos, 0);
writedir (&dir2entry, zoo_file);
zooseek (zoo_file, save_pos, 0); /*DEBUG*/
}
direntry.version_no = high_version_no + 1; /* ..one higher */
direntry.vflag = high_vflag;
/* now see if we need to delete older version */
fgens = high_vflag & VFL_GEN;
if (fgens == 0)
fgens = zgens;
if (zgens != 0 && zgens < fgens)
fgens = zgens;
if (fgens != 0 && direntry.version_no - this_version_no >= fgens) {
delold = 1;
prterror ('M', "replaced+\n");
} else
prterror ('M', "added+\n");
} else {
prterror ('M', "replaced\n");
delold = 1;
}
if (delold) { /* deleting old file */
long save_pos = zootell (zoo_file); /*DEBUG*/
++delcount; /* remember to pack */
zooseek (zoo_file, prev_pos, 0);
readdir (&dir2entry, zoo_file, 1);
if (dir2entry.cmt_size != 0) { /* propagate latest comment */
oldcmtpos = dir2entry.comment;
oldcmtsiz = dir2entry.cmt_size;
}
dir2entry.deleted = 1; /* mark as deleted */
/* following line is optimization if only 1 generation */
dir2entry.vflag &= (~VFL_LAST); /* no longer highest */
zooseek (zoo_file, prev_pos, 0);
writedir (&dir2entry, zoo_file);
zooseek (zoo_file, save_pos, 0); /*DEBUG*/
}
} else /* not in archive */
prterror ('M', "added\n");
/* Preserve any old comment if we replaced or superseded the file */
direntry.comment = oldcmtpos;
direntry.cmt_size = oldcmtsiz;
#ifndef PORTABLE
/* Copy comment if any from Z format file */
if (z_fmt && tiny_header.cmt_size != 0) {
zooseek (this_file, (long) sizeof(tiny_header), 0); /* to comment */
direntry.comment = zootell (zoo_file);
direntry.cmt_size = tiny_header.cmt_size;
/* 4th param is 0 for no CRC */
getfile (this_file, zoo_file, (long) tiny_header.cmt_size, 0);
direntry.next = zootell (zoo_file);
}
#endif
/* if user requested comments, any previous comment in a Z format
file may now be manually overwritten */
if (add_comment && !feof (stdin)) {
show_comment (&direntry, zoo_file, 1, whichname);
get_comment (&direntry, zoo_file, this_path);
direntry.next = zootell (zoo_file); /* update .next ptr */
} /* end if */
#ifndef PORTABLE
/* if adding Z format archive, copy relevant fields from its header */
if (z_fmt) { /* moved out to shorten code & allow optimizer to work */
copyfields (&direntry, &tiny_header);
}
#endif
debug((printf ("zooadd: our new .next = [%lx].\n", direntry.next)))
{
long savepos = zootell (zoo_file); /* save position */
zooseek (zoo_file, this_dir_offset, 0);
writedir (&direntry, zoo_file);
zooseek (zoo_file, savepos, 0); /* restore position */
}
} else { /* file was not properly added */
zooseek (zoo_file, save_position, 0); /* ..restore file pointer */
zootrunc (zoo_file); /* ..truncate file */
} /* end if */
zooclose (this_file);
if (!success)
break;
firstfile = 0;
} /* end for */
save_position = zootell (zoo_file);
/* Write a null direntry entry */
zooseek (zoo_file, save_position, 0);
writenull (zoo_file, MAXDIRSIZE);
zootrunc (zoo_file); /* truncate */
#ifdef NIXTIME
zooclose (zoo_file);
setutime (zoo_path, latest_date, latest_time);
#else
settime (zoo_file, latest_date, latest_time);
zooclose (zoo_file);
#endif
if (!addcount) { /* no files added */
prterror ('m', "No files added.\n");
if (zoo_status == NEW_ZOO)
unlink (zoo_path);
} else {
if (delcount && pack) { /* pack if user asked and found deleted entries */
prterror ('M', "-----\nPacking...");
zoopack (zoo_path, "PP");
prterror ('M', "done\n");
}
/* If files to move & we added some and no error so far, delete originals */
if (move && !exit_status)
if (kill_files (flist, longest) != 0)
exit_status++;
}
/* right here we handle archive comment */
if (add_global_comment) {
comment(zoo_path, "_A");
add_global_comment = 0;
}
if (exit_status)
zooexit (1);
} /* end zoo_add */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -