⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stafzip.cpp

📁 Software Testing Automation Framework (STAF)的开发代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:

        if (resultPtr->rc != 0) 
        {
            rc = resultPtr->rc;    
            result = resultPtr->result;
            
            break;
        }

        thisFile = resultPtr->result;

        if (thisFile.length() <= MAXFILENAME)
        {
            util.convertSTAFStringBuffer(thisFile.toCurrentCodePage(), deleteFile);
        }
        else
        {
            result = STAFString("STAFZip::handleDelete: FILE [")
                     + STAFString(deleteFile)
                     + "] file name length exceeds "
                     + MAXFILENAME
                     + " charactors.\n";
            rc = kSTAFInvalidValue;

            break;
        }

        // delete a file from zip archive

        STAFTrace::trace(kSTAFTraceServiceResult,
                         STAFString("handleDelete_CP1")
                             + " zipFileBackup ["
                             + zipFileBackup
                             + "]");

        // open the backup zip archive

        zf = new STAFZipFile(pData->fHandlePtr, file, &rc,
                             &resultBuffer, 1, newsize);

        if (rc != kSTAFOk)
        {
            delete zf;
            result = resultBuffer;

            break;
        }

        STAFTrace::trace(kSTAFTraceServiceResult,
                         STAFString("handleDelete_CP2")
                         + " deleteFile [" + deleteFile + "]");

        // delete one entry from the zip archive

        if ((rc = zf->deleteFile(deleteFile, (uLong*)&newsize, &resultBuffer))
            != kSTAFOk)
        {
            delete zf;
            result = resultBuffer;

            break;
        }

        delete zf;
    } // end deleting loop

    // file operation is done, let's close the file handle

    if (file != NULL)
    {
        fclose(file);
    }

    // to reduce file size after deleting file entry

    if (newsize > 0)
    {
        // change file size to the new size
        // after deleting the file entry

        if ((rc = util.changeFileSize(zipFileBackup, (uLong)newsize)) != 0)
        {
            result = STAFString("STAFZip::handleDelete:"
                     " Change file size fails: [")
                     + zipFileBackup
                     + STAFString("] to [")
                     + newsize
                     + STAFString("].\n");
            rc = kZIPGeneralZipError;
        }
    }

    if (rc == kSTAFOk && newsize > 0)
    {
        // if delete was successful,
        // remove the original zip archive
        // since we are working on the backup zip archive

        if (remove(zipFile) != 0)
        {
            result = STAFString("STAFZip::handleDelete:"
                                " Remove zipfile fails: [")
                     + zipFile
                     + STAFString("].\n");
            rc = kSTAFFileDeleteError;
        }

        // rename the backup zip archive to the original zip archive
        // since we are working on the backup zip archive

        if (rename(zipFileBackup, zipFile) != 0)
        {
            result = STAFString("STAFZip::handleDelete:"
                                " Rename zipfile fails: from [")
                     + zipFileBackup
                     + STAFString("] to [")
                     + zipFile
                     + STAFString("].\n");
            rc = kZIPGeneralZipError;
        }
    }
    else // either all entries are removed or remove failed
    {
        // remove the backup zip archive that we are working on

        remove(zipFileBackup);

        if (newsize == 0 && rc == kSTAFOk)
        {
            // if all files in the zip archive are removed,
            // remove the zip archive

            if ((rc = remove(zipFile)) != 0)
            {
                result = STAFString("STAFZip::handleDelete: "
                                "Remove file fails: [")
                         + STAFString(zipFile)
                         + STAFString("].\n");
                rc = kSTAFFileDeleteError;
            }
        }
    }

    return STAFResultPtr(new STAFResult(rc, result),
                         STAFResultPtr::INIT);
}


// Handles List requests
STAFResultPtr handleList(STAFServiceRequestLevel30 *pInfo,
                           STAFZipServiceData *pData, STAFString *mutexName)
{
    STAFString result;

    char zipFile[MAXFILENAME + 1] = "";

    STAFZipFile *zf;
    STAFZipUtil util = STAFZipUtil(pData->fHandlePtr);

    STAFRC_t rc = kSTAFOk;

    FILE *file;       

    // Verify the requester has at least trust level 3

    VALIDATE_TRUST(3, pData->fShortName, "LIST", pData->fLocalMachineName);

    // Parse the request

    STAFCommandParseResultPtr parsedResult =
        pData->fListParser->parse(pInfo->request);

    if (parsedResult->rc != kSTAFOk)
    {
        return STAFResultPtr(new STAFResult(kSTAFInvalidRequestString,
                             parsedResult->errorBuffer), STAFResultPtr::INIT);
    }

    // get "ZIPFILE" parameter value

    STAFResultPtr resultPtr = resolveOp(pInfo, pData, parsedResult, "ZIPFILE");
    if (resultPtr->rc != 0) return resultPtr;

    if (resultPtr->result.length() <= MAXFILENAME)
    {
        util.convertSTAFStringBuffer(resultPtr->result.toCurrentCodePage(), zipFile);
    }
    else
    {
        return STAFResultPtr(new STAFResult(kSTAFInvalidValue,
                                            STAFString("STAFZip::handleList:"
                                            " ZIPFILE [")
                                            + zipFile
                                            +"] file name length exceeds "
                                            + MAXFILENAME
                                            + " charactors.\n"),
                                            STAFResultPtr::INIT);
    }

    resultPtr->result = resultPtr->result.replace(kUTF8_BSLASH, kUTF8_SLASH);

    // use zip file name to construct the mutex name

    *mutexName += resultPtr->result;    
    *mutexName = (*mutexName).toUpperCase();

    // obtain mutex semphore on the zipfile

    zipLock.request(*mutexName);

    STAFTrace::trace(kSTAFTraceServiceResult,
                     STAFString("handleList_CP1")
                     + " zipFile ["
                     + zipFile
                     + "]");

    // open the ZIP archive for reading

    if ((file = fopen(zipFile, "rb")) == NULL) 
    {
        result = STAFString("STAFZip::handleList_CP1.1: ")
                 + "ZipFile [" + zipFile + "] does not exist.\n";
        rc = kSTAFDoesNotExist;

        return STAFResultPtr(new STAFResult(rc, result),
                             STAFResultPtr::INIT);
    }

    // open the Zip archive, don't create a new one

    zf = new STAFZipFile(pData->fHandlePtr, file, &rc, &result, 1);

    if (rc != kSTAFOk)
    {
        delete zf;
        fclose(file);

        return STAFResultPtr(new STAFResult(rc, result),
                         STAFResultPtr::INIT);
    }

    STAFTrace::trace(kSTAFTraceServiceResult,
                     STAFString("handleList_CP2"));

    STAFString buffer;

    rc = zf->listFile(&buffer, &result);

    delete zf;

    // file operation is done, let's close the file handle

    if (file != NULL)
    {
        fclose(file);
    }

    if (rc == kSTAFOk)
    {
        result = buffer;
    }

    return STAFResultPtr(new STAFResult(rc, result),
                         STAFResultPtr::INIT);
}


STAFResultPtr handleHelp(STAFServiceRequestLevel30 *pInfo,
                         STAFZipServiceData *pData)
{
    // Verify the requester has at least trust level 1

    VALIDATE_TRUST(1, pData->fShortName, "HELP", pData->fLocalMachineName);

    // Return help text for the service

    // Note: The ADD request syntax is preferred over the ZIP ADD request
    // syntax (so only documented the ADD request in the help).
    
    STAFString help("ZIP Service Help" + sLineSep + sLineSep);

    help += "UNZIP  ZIPFILE <Name> TODIRECTORY <Name>" + sLineSep;
    help += "       [FILE <Name>]... [RESTOREPERMISSION] [REPLACE]";
    help += sLineSep + sLineSep;
    help += "ADD    ZIPFILE <Name> < FILE <Name> | DIRECTORY <Name> [RECURSE] >";
    help += sLineSep;
    help += "       [RELATIVETO <Directory>]";
    help += sLineSep + sLineSep;
    help += "DELETE ZIPFILE <Name> FILE <Name> [FILE <Name>]... CONFIRM";
    help += sLineSep + sLineSep;
    help += "LIST   ZIPFILE <Name>";
    help += sLineSep + sLineSep;
    help += "VERSION" + sLineSep + sLineSep;
    help += "HELP" + sLineSep;

    return STAFResultPtr(new STAFResult(kSTAFOk, help), STAFResultPtr::INIT);
}


STAFResultPtr handleVersion(STAFServiceRequestLevel30 *pInfo,
                            STAFZipServiceData *pData)
{
    // Verify the requester has at least trust level 1

    VALIDATE_TRUST(1, pData->fShortName, "VERSION", pData->fLocalMachineName);

    return STAFResultPtr(new STAFResult(kSTAFOk, sVersionInfo),
                         STAFResultPtr::INIT);
}


STAFResultPtr resolveOp(STAFServiceRequestLevel30 *pInfo,
                        STAFZipServiceData *pData,
                        STAFCommandParseResultPtr &parsedResult,
                        const STAFString &fOption, unsigned int optionIndex)
{
    STAFString optionValue = parsedResult->optionValue(fOption, optionIndex);

    if (optionValue.find(sLeftCurlyBrace) == STAFString::kNPos)
    {
        return STAFResultPtr(new STAFResult(kSTAFOk, optionValue),
                             STAFResultPtr::INIT);
    }

    return resolveStr(pInfo, pData, optionValue);
}

STAFResultPtr resolveStr(STAFServiceRequestLevel30 *pInfo,
                         STAFZipServiceData *pData,
                         const STAFString &theString)
{
    return pData->fHandlePtr->submit(sLocal, sVar, sResStrResolve +
                                     STAFString(pInfo->requestNumber) +
                                     sString +
                                     pData->fHandlePtr->wrapData(theString));
}


void registerHelpData(STAFZipServiceData *pData, unsigned int errorNumber,
                      const STAFString &shortInfo, const STAFString &longInfo)
{
    static STAFString regString("REGISTER SERVICE %C ERROR %d INFO %C "
                                "DESCRIPTION %C");

    pData->fHandlePtr->submit(sLocal, sHelp, STAFHandle::formatString(
        regString.getImpl(), pData->fShortName.getImpl(), errorNumber,
        shortInfo.getImpl(), longInfo.getImpl()));
}


void unregisterHelpData(STAFZipServiceData *pData, unsigned int errorNumber)
{
    static STAFString regString("UNREGISTER SERVICE %C ERROR %d");

    pData->fHandlePtr->submit(sLocal, STAFString("HELP"), STAFHandle::
    formatString(
        regString.getImpl(), pData->fShortName.getImpl(), errorNumber));
}


























⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -