示例#1
0
文件: isio.cpp 项目: 0x414A/irods
int
isioFileClose( int fileIndex ) {
    openedDataObjInp_t dataObjCloseInp;
    int i, status;

    if ( debug ) {
        printf( "isioFileClose: %d\n", fileIndex );
    }

    /* If the buffer had been used for writing, flush it */
    status = isioFlush( fileIndex );
    if ( status < 0 ) {
        return status;
    }


    memset( &dataObjCloseInp, 0, sizeof( dataObjCloseInp ) );
    dataObjCloseInp.l1descInx = openFiles[fileIndex];

    openFiles[fileIndex] = 0;

    i = fileIndex;
    if ( cacheInfo[i].usingUsersBuffer == 'n' ) {
        if ( debug ) {
            printf( "isioFileClose calling free\n" );
        }
        free( cacheInfo[i].base );
    }

    cacheInfo[i].usingUsersBuffer = ' ';

    return rcDataObjClose( Comm, &dataObjCloseInp );
}
示例#2
0
int
irodsfflush(FILE *fi_stream) {
   int i;
   i = (int)fi_stream;
   if (debug) printf("isiofflush: %d\n", i);
   if (i<ISIO_MAX_OPEN_FILES && i>=ISIO_MIN_OPEN_FD && openFiles[i]>0) {
      return(isioFlush(i));
   }
   else {
      return(fflush(fi_stream));
   }
}
示例#3
0
文件: isio.cpp 项目: 0x414A/irods
int
isioFileWrite( int fileIndex, void *buffer, int countToWrite ) {
    int status;
    int spaceInBuffer;
    int newBufSize;

    openedDataObjInp_t dataObjWriteInp;
    bytesBuf_t dataObjWriteOutBBuf;

    if ( debug ) {
        printf( "isioFileWrite: %d\n", fileIndex );
    }

    if ( cacheInfo[fileIndex].count > 0 ) {
        /* buffer has read data in it, so seek to where the
           the app thinks the pointer is and disgard the buffered
           read data */
        long offset;
        offset = - cacheInfo[fileIndex].count;
        status = isioFileSeek( fileIndex, offset, SEEK_CUR );
        if ( status ) {
            return status;
        }
        cacheInfo[fileIndex].ptr = cacheInfo[fileIndex].base;
        cacheInfo[fileIndex].count = 0;
    }

    spaceInBuffer = cacheInfo[fileIndex].bufferSize -
                    cacheInfo[fileIndex].written;

    if ( debug ) {
        printf( "isioFileWrite: spaceInBuffer %d\n", spaceInBuffer );
    }
    if ( countToWrite < spaceInBuffer ) {
        /* Fits in the buffer, just cache it */
        if ( debug ) printf( "isioFileWrite: caching 1 %x %d\n",
                                 ( int ) cacheInfo[fileIndex].ptr, countToWrite );
        memcpy( cacheInfo[fileIndex].ptr, buffer, countToWrite );
        cacheInfo[fileIndex].ptr += countToWrite;
        cacheInfo[fileIndex].written += countToWrite;
        return countToWrite;
    }

    status = isioFlush( fileIndex ); /* if anything is buffered, flush it */
    if ( status < 0 ) {
        return status;
    }

    if ( countToWrite > ISIO_MAX_BUF_SIZE ) {
        /* Too big to cache, just send it */
        dataObjWriteOutBBuf.buf = buffer;
        dataObjWriteOutBBuf.len = countToWrite;

        memset( &dataObjWriteInp, 0, sizeof( dataObjWriteInp ) );

        dataObjWriteInp.l1descInx = openFiles[fileIndex];
        dataObjWriteInp.len = countToWrite;

        status = rcDataObjWrite( Comm, &dataObjWriteInp,
                                 &dataObjWriteOutBBuf );
        if ( debug ) {
            printf( "isioFileWrite: rcDataWrite 2 %d\n", status );
        }
        if ( status < 0 ) {
            return status;
        }

        return ( status ); /* total bytes written */
    }

    newBufSize = ( 2 * countToWrite ) + 8; /* Possible next size */
    if ( newBufSize > ISIO_MAX_BUF_SIZE ) {
        newBufSize = ISIO_MAX_BUF_SIZE;
    }

    if ( newBufSize > cacheInfo[fileIndex].bufferSize ) {
        /* free old and make new larger buffer */
        int i = fileIndex;
        if ( cacheInfo[i].usingUsersBuffer == 'n' ) {
            if ( debug ) {
                printf( "isioFilewrite calling free\n" );
            }
            free( cacheInfo[i].base );
        }
        if ( debug ) printf( "isioFilewrite calling malloc %d\n",
                                 newBufSize );
        cacheInfo[i].base = ( char * )malloc( newBufSize );
        if ( cacheInfo[i].base == NULL ) {
            fprintf( stderr, "Memory Allocation error\n" );
            return 0;
        }
        cacheInfo[i].bufferSize = newBufSize;
        cacheInfo[i].usingUsersBuffer = 'n';
        cacheInfo[i].ptr = cacheInfo[i].base;
    }

    /* Now it fits in the buffer, so cache it */
    if ( debug ) printf( "isioFileWrite: caching 2 %x %d\n",
                             ( int ) cacheInfo[fileIndex].ptr, countToWrite );
    memcpy( cacheInfo[fileIndex].ptr, buffer, countToWrite );
    cacheInfo[fileIndex].ptr += countToWrite;
    cacheInfo[fileIndex].count -= countToWrite; //??
    cacheInfo[fileIndex].written += countToWrite;
    return countToWrite;

}
示例#4
0
文件: isio.cpp 项目: 0x414A/irods
int
isioFileRead( int fileIndex, void *buffer, int maxToRead ) {
    int status, i;
    int reqSize;
    char *myPtr;
    openedDataObjInp_t dataObjReadInp;
    bytesBuf_t dataObjReadOutBBuf;
    int count;
    int toMove;
    int newBufSize;

    if ( debug ) {
        printf( "isioFileRead: %d\n", fileIndex );
    }

    /* If the buffer had been used for writing, flush it */
    status = isioFlush( fileIndex );
    if ( status < 0 ) {
        return status;
    }

    reqSize = maxToRead;
    myPtr = buffer;
    if ( cacheInfo[fileIndex].count > 0 ) {
        if ( cacheInfo[fileIndex].count >= reqSize ) {
            memcpy( myPtr, cacheInfo[fileIndex].ptr, reqSize );
            cacheInfo[fileIndex].ptr += reqSize;
            cacheInfo[fileIndex].count -= reqSize;
            return maxToRead;
        }
        else {
            memcpy( myPtr, cacheInfo[fileIndex].ptr, cacheInfo[fileIndex].count );
            cacheInfo[fileIndex].ptr += cacheInfo[fileIndex].count;
            reqSize -=  cacheInfo[fileIndex].count;
            myPtr += cacheInfo[fileIndex].count;
            cacheInfo[fileIndex].count = 0;
        }
    }

    newBufSize = ( 2 * maxToRead ) + 8;
    i = fileIndex;

    if ( cacheInfo[i].usingUsersBuffer == 'y' ) {
        /* previous time we used user's buffer, this time either
           allocate a new one or use their's this time too. */
        cacheInfo[i].bufferSize = 0;
    }

    if ( newBufSize > cacheInfo[i].bufferSize ) {
        if ( newBufSize <= ISIO_MAX_BUF_SIZE ) {
            if ( cacheInfo[i].usingUsersBuffer == 'n' ) {
                if ( debug ) {
                    printf( "isioFileRead calling free\n" );
                }
                free( cacheInfo[i].base );
            }
            if ( debug ) {
                printf( "isioFileRead calling malloc\n" );
            }
            cacheInfo[i].base = ( char * )malloc( newBufSize );
            if ( cacheInfo[i].base == NULL ) {
                fprintf( stderr, "Memory Allocation error\n" );
                return 0;
            }
            cacheInfo[i].bufferSize = newBufSize;
            cacheInfo[i].usingUsersBuffer = 'n';
        }
        else {
            /* Use user's buffer */
            cacheInfo[i].base = buffer;
            cacheInfo[i].bufferSize = maxToRead;
            cacheInfo[i].usingUsersBuffer = 'y';
        }
        cacheInfo[i].ptr = cacheInfo[i].base;
        cacheInfo[i].count = 0;
    }

    status = isioFillBuffer( fileIndex );
    if ( status < 0 ) {
        return status;
    }

    if ( cacheInfo[i].usingUsersBuffer == 'y' ) {
        count = cacheInfo[fileIndex].count;
        cacheInfo[fileIndex].count = 0;
        if ( debug ) {
            printf( "isioFileRead return1: %d\n", count );
        }
        return count;
    }
    if ( cacheInfo[fileIndex].count > 0 ) {
        toMove = reqSize;
        if ( cacheInfo[fileIndex].count < reqSize ) {
            toMove = cacheInfo[fileIndex].count;
        }
        memcpy( myPtr, cacheInfo[fileIndex].ptr, toMove );
        cacheInfo[fileIndex].ptr += toMove;
        cacheInfo[fileIndex].count -= toMove;
        myPtr += toMove;
        reqSize -= toMove;
    }
    count = ( void * )myPtr - buffer;
    if ( debug ) {
        printf( "isioFileRead return2: %d\n", count );
    }
    return count;

}