예제 #1
0
/** A helper function for midp_file_cache_flush(). */
static
void midp_file_cache_flush_using_buffer(char** ppszError, int handle,
                                        char* buf, long bufsize) {
    MidpFileCacheBlock *b; /* current cache block */
    MidpFileCacheBlock *n; /* next cache block */
    MidpFileCacheBlock *q; /* first block not (yet) copied to the buffer,
                              finally, the value to be stored
                              in mFileCache->blocks */
    char *bufPos;          /* first not yet used byte in the write buffer */
    long startPos, endPos; /* positions in the file cached in a series
                              of buffers, from...upto */
    long len;
    *ppszError = NULL;

    while ((b = mFileCache->blocks) != NULL) {

        if (  NULL != buf
                && NULL != (n = b->next)
                && n->position == b->position + b->length /* adjacent? */
                && b->length + n->length < bufsize
           ) {  /* begin merging blocks */

            /* position in the buffer */
            bufPos = buf;
            /* position in the file */
            startPos = endPos = b->position;

            b = mFileCache->blocks;
            /* now copy a series of adjacent blocks to the write buffer */
            do {
                len = b->length;
                memcpy(bufPos,DATA(b),len);
                endPos += len;
                bufPos += len;
                b = b->next;
            } while (  NULL != b
                       && b->position == endPos
                       && endPos - startPos + b->length < bufsize
                    );

            q = b; /* q is next to the last copied block */
            /* the cache state has not been modified yet */

            /* now write from the write buffer */
            storagePosition(ppszError, handle, startPos);
            CHECK_ERROR(*ppszError);
            storageWrite(ppszError, handle, buf, endPos-startPos);
            CHECK_ERROR(*ppszError);

            /* write successful, now free the cache blocks */
            while ( q != (b = mFileCache->blocks)) {
                mFileCache->size -= b->length + sizeof(MidpFileCacheBlock);
                mFileCache->blocks = b->next;
                midpFree(b);
            }
        } else {
            storagePosition(ppszError, handle, b->position);
            CHECK_ERROR(*ppszError);
            storageWrite(ppszError, handle, DATA(b), b->length);
            CHECK_ERROR(*ppszError);

            mFileCache->size -= b->length + sizeof(MidpFileCacheBlock);
            mFileCache->blocks = b->next;
            midpFree(b);
        }
    }
    storageCommitWrite(ppszError, handle);
    CHECK_ERROR(*ppszError);

    /* ASSERT (mFileCache->size == 0) */
    if (mFileCache->size != 0) {
        REPORT_ERROR(LC_RMS, "File cache out of sync");
    }
}
예제 #2
0
/**
 * Writes integer to the file.
 *
 * @param data integer to be written
 * @return number of bytes written
 */
static int write_int(int data) {
  storageWrite(&io_error_message, table_file, (char *)&data, sizeof(int));
  return sizeof(int);
}