2000-05-23 01:55:31 +00:00
|
|
|
/*
|
|
|
|
* Nulib2
|
2007-02-19 23:11:55 +00:00
|
|
|
* Copyright (C) 2000-2007 by Andy McFadden, All Rights Reserved.
|
2000-05-23 01:55:31 +00:00
|
|
|
* This is free software; you can redistribute it and/or modify it under the
|
2007-02-19 23:11:55 +00:00
|
|
|
* terms of the BSD License, see the file COPYING.
|
2000-05-23 01:55:31 +00:00
|
|
|
*
|
|
|
|
* Extract files and test archives.
|
|
|
|
*/
|
|
|
|
#include "Nulib2.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract all of the records from the archive, pulling out and displaying
|
|
|
|
* comment threads.
|
|
|
|
*
|
|
|
|
* The "bulk extract" call doesn't deal with comments. Since we want to
|
|
|
|
* show them while we're extracting the files, we have to manually find
|
|
|
|
* and extract them.
|
|
|
|
*/
|
|
|
|
static NuError
|
|
|
|
ExtractAllRecords(NulibState* pState, NuArchive* pArchive)
|
|
|
|
{
|
2002-09-21 00:59:25 +00:00
|
|
|
NuError err;
|
|
|
|
const NuRecord* pRecord;
|
|
|
|
const NuThread* pThread;
|
|
|
|
NuRecordIdx recordIdx;
|
|
|
|
NuAttr numRecords;
|
|
|
|
int idx, threadIdx;
|
|
|
|
|
|
|
|
DBUG(("--- doing manual extract\n"));
|
2002-10-09 00:15:24 +00:00
|
|
|
Assert(NState_GetCommand(pState) == kCommandExtract); /* no "-p" here */
|
2002-09-21 00:59:25 +00:00
|
|
|
|
|
|
|
err = NuGetAttr(pArchive, kNuAttrNumRecords, &numRecords);
|
|
|
|
for (idx = 0; idx < (int) numRecords; idx++) {
|
|
|
|
err = NuGetRecordIdxByPosition(pArchive, idx, &recordIdx);
|
|
|
|
if (err != kNuErrNone) {
|
|
|
|
fprintf(stderr, "ERROR: couldn't get record #%d (err=%d)\n",
|
|
|
|
idx, err);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = NuGetRecord(pArchive, recordIdx, &pRecord);
|
|
|
|
if (err != kNuErrNone) {
|
|
|
|
fprintf(stderr, "ERROR: unable to get recordIdx %ld\n", recordIdx);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do we want to extract this record? */
|
|
|
|
if (!IsSpecified(pState, pRecord))
|
|
|
|
continue;
|
|
|
|
NState_IncMatchCount(pState);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for a comment thread.
|
|
|
|
*/
|
|
|
|
for (threadIdx = 0; (ulong)threadIdx < pRecord->recTotalThreads;
|
|
|
|
threadIdx++)
|
|
|
|
{
|
|
|
|
pThread = NuGetThread(pRecord, threadIdx);
|
2002-10-09 00:15:24 +00:00
|
|
|
Assert(pThread != nil);
|
2002-09-21 00:59:25 +00:00
|
|
|
|
|
|
|
if (NuGetThreadID(pThread) == kNuThreadIDComment &&
|
|
|
|
pThread->actualThreadEOF > 0)
|
|
|
|
{
|
|
|
|
printf("----- '%s':\n", pRecord->filename);
|
|
|
|
err = NuExtractThread(pArchive, pThread->threadIdx,
|
|
|
|
NState_GetCommentSink(pState));
|
|
|
|
if (err != kNuErrNone) {
|
|
|
|
printf("[comment extraction failed, continuing\n");
|
|
|
|
} else {
|
|
|
|
printf("\n-----\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* extract the record, using the usual mechanisms */
|
|
|
|
err = NuExtractRecord(pArchive, recordIdx);
|
|
|
|
if (err != kNuErrNone)
|
|
|
|
goto bail;
|
|
|
|
}
|
2000-05-23 01:55:31 +00:00
|
|
|
|
|
|
|
bail:
|
2002-09-21 00:59:25 +00:00
|
|
|
return err;
|
2000-05-23 01:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract the specified files.
|
|
|
|
*/
|
|
|
|
NuError
|
|
|
|
DoExtract(NulibState* pState)
|
|
|
|
{
|
2002-09-21 00:59:25 +00:00
|
|
|
NuError err;
|
|
|
|
NuArchive* pArchive = nil;
|
|
|
|
|
2002-10-09 00:15:24 +00:00
|
|
|
Assert(pState != nil);
|
|
|
|
|
|
|
|
if (NState_GetModBinaryII(pState))
|
|
|
|
return BNYDoExtract(pState);
|
2002-09-21 00:59:25 +00:00
|
|
|
|
|
|
|
err = OpenArchiveReadOnly(pState);
|
2002-10-09 00:15:24 +00:00
|
|
|
if (err == kNuErrIsBinary2)
|
|
|
|
return BNYDoExtract(pState);
|
2002-09-21 00:59:25 +00:00
|
|
|
if (err != kNuErrNone)
|
|
|
|
goto bail;
|
|
|
|
pArchive = NState_GetNuArchive(pState);
|
2002-10-09 00:15:24 +00:00
|
|
|
Assert(pArchive != nil);
|
2002-09-21 00:59:25 +00:00
|
|
|
|
|
|
|
NState_SetMatchCount(pState, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're not interested in comments, just use the "bulk" extract
|
|
|
|
* call. If we want comments, we need to do this one at a time.
|
|
|
|
*/
|
|
|
|
if (!NState_GetModComments(pState)) {
|
|
|
|
err = NuExtract(pArchive);
|
|
|
|
if (err != kNuErrNone)
|
|
|
|
goto bail;
|
|
|
|
} else {
|
|
|
|
err = ExtractAllRecords(pState, pArchive);
|
|
|
|
if (err != kNuErrNone)
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NState_GetMatchCount(pState))
|
|
|
|
printf("%s: no records match\n", gProgName);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
|
|
|
bail:
|
2002-09-21 00:59:25 +00:00
|
|
|
if (pArchive != nil)
|
|
|
|
(void) NuClose(pArchive);
|
|
|
|
return err;
|
2000-05-23 01:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract the specified files to stdout.
|
|
|
|
*/
|
|
|
|
NuError
|
|
|
|
DoExtractToPipe(NulibState* pState)
|
|
|
|
{
|
2002-09-21 00:59:25 +00:00
|
|
|
/* we handle the "to pipe" part farther down */
|
|
|
|
return DoExtract(pState);
|
2000-05-23 01:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do an integrity check on one or more records in the archive.
|
|
|
|
*/
|
|
|
|
NuError
|
|
|
|
DoTest(NulibState* pState)
|
|
|
|
{
|
2002-09-21 00:59:25 +00:00
|
|
|
NuError err;
|
|
|
|
NuArchive* pArchive = nil;
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-10-09 00:15:24 +00:00
|
|
|
Assert(pState != nil);
|
|
|
|
|
|
|
|
if (NState_GetModBinaryII(pState))
|
|
|
|
return BNYDoTest(pState);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:59:25 +00:00
|
|
|
err = OpenArchiveReadOnly(pState);
|
2002-10-09 00:15:24 +00:00
|
|
|
if (err == kNuErrIsBinary2)
|
|
|
|
return BNYDoTest(pState);
|
2002-09-21 00:59:25 +00:00
|
|
|
if (err != kNuErrNone)
|
|
|
|
goto bail;
|
|
|
|
pArchive = NState_GetNuArchive(pState);
|
2002-10-09 00:15:24 +00:00
|
|
|
Assert(pArchive != nil);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:59:25 +00:00
|
|
|
NState_SetMatchCount(pState, 0);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:59:25 +00:00
|
|
|
err = NuTest(pArchive);
|
|
|
|
if (err != kNuErrNone)
|
|
|
|
goto bail;
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:59:25 +00:00
|
|
|
if (!NState_GetMatchCount(pState))
|
|
|
|
printf("%s: no records match\n", gProgName);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
|
|
|
bail:
|
2002-09-21 00:59:25 +00:00
|
|
|
if (pArchive != nil)
|
|
|
|
(void) NuClose(pArchive);
|
|
|
|
return err;
|
2000-05-23 01:55:31 +00:00
|
|
|
}
|
|
|
|
|