Example #1
0
int main(int argc, char * * argv)
{
    CryptedVolumeParms parms;
    CoreResult cr;

    SuperBlock * pSuperBlock;
    CryptedVolume * pVolume;

    CryptedFileInfo info;
    CryptedFileID idFile;
    CryptedFilePos cbWritten, cbRead;

    octet buf[100000];
    int i;

    sysInitPRNG();

    coreSetDefVolumeParms(&parms);
    parms.csMaxCached = 10;
    parms.csIOGranularity = 5;
   
    cr = coreReadSuperBlock(TESTVOL "/", TESTPW,
        cipherTable, &parms, &pSuperBlock);
    assert(cr == CORERC_OK);

    pVolume = pSuperBlock->pVolume;

    memset(&info, 0, sizeof(info));
    info.flFlags = CFF_IFREG;
    info.cRefs = 1;
    info.cbFileSize = 0;
    info.idParent = 0;
    cr = coreCreateBaseFile(pVolume, &info, &idFile);
    assert(cr == CORERC_OK);

    memset(buf, 0xaa, sizeof(buf));
    cr = coreWriteToFile(pVolume, idFile, 0, sizeof(buf), buf, &cbWritten);
    assert(cr == CORERC_OK && cbWritten == sizeof(buf));

    /* Regression test: a bug in basefile.c caused writes that were
       entirely in the initialised area to zero out the remainder of
       the last written sector unless it was in the cache. */
    cr = coreWriteToFile(pVolume, idFile, 0, 1000, buf, &cbWritten);
    assert(cr == CORERC_OK && cbWritten == 1000);

    memset(buf, 0xff, sizeof(buf));
    cr = coreReadFromFile(pVolume, idFile, 0, sizeof(buf), buf, &cbRead);
    assert(cr == CORERC_OK && cbRead == sizeof(buf));
    for (i = 0; i < sizeof(buf); i++)
        assert(buf[i] == 0xaa);
    
    cr = coreDropSuperBlock(pSuperBlock);
    assert(cr == CORERC_OK);

    return 0;
}
Example #2
0
CoreResult coreSetDirEntries(CryptedVolume * pVolume,
   CryptedFileID id, CryptedDirEntry * pEntries)
{
   CoreResult cr;
   CryptedFilePos cbDirSize;
   CryptedFilePos cbWritten;
   CryptedDirEntry * pEntry;
   octet * pabBuffer, * pabPos;

   if (!pEntries) return coreSetFileSize(pVolume, id, 0);
   
   /* How big will the directory be? */
   cbDirSize = 1;
   for (pEntry = pEntries;
        pEntry;
        pEntry = pEntry->pNext)
      cbDirSize += 9 + pEntry->cbName;

   /* Allocate memory. */
   pabBuffer = sysAllocSecureMem(cbDirSize);
   if (!pabBuffer) return CORERC_NOT_ENOUGH_MEMORY;

   /* Build the directory contents. */
   pabPos = pabBuffer;
   for (pEntry = pEntries;
        pEntry;
        pEntry = pEntry->pNext)
   {
      *pabPos = pEntry->flFlags | CDF_NOT_EOL;
      int32ToBytes(pEntry->idFile, pabPos + 1);
      int32ToBytes(pEntry->cbName, pabPos + 5);
      memcpy(pabPos + 9, pEntry->pabName, pEntry->cbName);
      pabPos += 9 + pEntry->cbName;
   }

   *pabPos = 0;
   
   /* Write the directory contents. */
   cr = coreWriteToFile(pVolume, id, 0,
      cbDirSize, pabBuffer, &cbWritten);
   sysFreeSecureMem(pabBuffer);
   if (cr) return cr;
   assert(cbWritten == cbDirSize);
   
   /* Shrink the directory file to cbDirSize. */
   cr = coreSetFileSize(pVolume, id, cbDirSize);
   if (cr) return cr;

   return CORERC_OK;
}