openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/rsyslog/8.1903.0-r0/rsyslog-8.1903.0/runtime/stream.c
static rsRetVal strmCloseFile(strm_t* pThis)
{
    off64_t currOffs;
    DEFiRet;

    assert(pThis != NULL);
    DBGOPRINT((obj_t*)pThis, "file %d(%s) closing, bDeleteOnClose %d\n",
              pThis->fd, getFileDebugName(pThis), pThis->bDeleteOnClose);

    if (pThis->tOperationsMode != STREAMMODE_READ)
    {
        strmFlushInternal(pThis, 0);
        if (pThis->iZipLevel)
        {
            doZipFinish(pThis);
        }
        if (pThis->bAsyncWrite)
        {
            strmWaitAsyncWriterDone(pThis);
        }
    }

    /* if we have a signature provider, we must make sure that the crypto
     * state files are opened and proper close processing happens. */
    if (pThis->cryprov != NULL && pThis->fd == -1)
    {
        const rsRetVal localRet = strmOpenFile(pThis);
        if (localRet != RS_RET_OK)
        {
            LogError(0, localRet,
                     "could not open file %s, this "
                     "may result in problems with encryption - "
                     "unfortunately, we cannot do anything against "
                     "this.",
                     pThis->pszCurrFName);
        }
    }

    /* the file may already be closed (or never have opened), so guard
     * against this. -- rgerhards, 2010-03-19
     */
    if (pThis->fd != -1)
    {
        currOffs = lseek64(pThis->fd, 0, SEEK_CUR);
        close(pThis->fd);
        pThis->fd = -1;
        pThis->inode = 0;
        if (pThis->cryprov != NULL)
        {
            pThis->cryprov->OnFileClose(pThis->cryprovFileData, currOffs);
            pThis->cryprovFileData = NULL;
        }
    }

    if (pThis->fdDir != -1)
    {
        /* close associated directory handle, if it is open */
        close(pThis->fdDir);
        pThis->fdDir = -1;
    }

    if (pThis->bDeleteOnClose)
    {
        if (pThis->pszCurrFName == NULL)
        {
            CHKiRet(genFileName(&pThis->pszCurrFName, pThis->pszDir,
                                pThis->lenDir, pThis->pszFName, pThis->lenFName,
                                pThis->iCurrFNum, pThis->iFileNumDigits));
        }
        DBGPRINTF("strmCloseFile: deleting '%s'\n", pThis->pszCurrFName);
        if (unlink((char*)pThis->pszCurrFName) == -1)
        {
            char errStr[1024];
            int err = errno;
            rs_strerror_r(err, errStr, sizeof(errStr));
            DBGPRINTF("error %d unlinking '%s' - ignored: %s\n", errno,
                      pThis->pszCurrFName, errStr);
        }
    }

    pThis->iCurrOffs = 0; /* we are back at begin of file */

finalize_it:
    free(pThis->pszCurrFName);
    pThis->pszCurrFName = NULL;
    RETiRet;
}
/* close a strm file
 * Note that the bDeleteOnClose flag is honored. If it is set, the file will be
 * deleted after close. This is in support for the qRead thread.
 * Note: it is valid to call this function when the physical file is closed. If
 * so, strmCloseFile() will still check if there is any unwritten data inside
 * buffers (this may be the case) and, if so, will open the file, write the
 * data, and then close it again (this is done via strmFlushInternal and
 * friends).
 */
close a strm file
Note that the bDeleteOnClose flag is honored. If it is set, the file will be
deleted after close. This is in support for the qRead thread.
Note: it is valid to call this function when the physical file is closed. If
so, strmCloseFile() will still check if there is any unwritten data inside
buffers (this may be the case) and, if so, will open the file, write the
data, and then close it again (this is done via strmFlushInternal and
friends).
close a strm file Note that the bDeleteOnClose flag is honored . If it is set the file will be deleted after close . This is in support for the qRead thread . Note : it is valid to call this function when the physical file is closed . If so strmCloseFile () will still check if there is any unwritten data inside buffers ( this may be the case ) and if so will open the file write the data and then close it again ( this is done via strmFlushInternal and friends ) .
static rsRetVal strmCloseFile(strm_t* pThis)
{
    off64_t currOffs;
    DEFiRet;
    assert(pThis != NULL);
    DBGOPRINT((obj_t*)pThis, "file %d(%s) closing, bDeleteOnClose %d\n",
              pThis->fd, getFileDebugName(pThis), pThis->bDeleteOnClose);
    if (pThis->tOperationsMode != STREAMMODE_READ)
    {
        strmFlushInternal(pThis, 0);
        if (pThis->iZipLevel)
        {
            doZipFinish(pThis);
        }
        if (pThis->bAsyncWrite)
        {
            strmWaitAsyncWriterDone(pThis);
        }
    }
    if (pThis->cryprov != NULL && pThis->fd == -1)
    {
        const rsRetVal localRet = strmOpenFile(pThis);
        if (localRet != RS_RET_OK)
        {
            LogError(0, localRet,
                     "could not open file %s, this "
                     "may result in problems with encryption - "
                     "unfortunately, we cannot do anything against "
                     "this.",
                     pThis->pszCurrFName);
        }
    }
    if (pThis->fd != -1)
    {
        currOffs = lseek64(pThis->fd, 0, SEEK_CUR);
        close(pThis->fd);
        pThis->fd = -1;
        pThis->inode = 0;
        if (pThis->cryprov != NULL)
        {
            pThis->cryprov->OnFileClose(pThis->cryprovFileData, currOffs);
            pThis->cryprovFileData = NULL;
        }
    }
    if (pThis->fdDir != -1)
    {
        close(pThis->fdDir);
        pThis->fdDir = -1;
    }
    if (pThis->bDeleteOnClose)
    {
        if (pThis->pszCurrFName == NULL)
        {
            CHKiRet(genFileName(&pThis->pszCurrFName, pThis->pszDir,
                                pThis->lenDir, pThis->pszFName, pThis->lenFName,
                                pThis->iCurrFNum, pThis->iFileNumDigits));
        }
        DBGPRINTF("strmCloseFile: deleting '%s'\n", pThis->pszCurrFName);
        if (unlink((char*)pThis->pszCurrFName) == -1)
        {
            char errStr[1024];
            int err = errno;
            rs_strerror_r(err, errStr, sizeof(errStr));
            DBGPRINTF("error %d unlinking '%s' - ignored: %s\n", errno,
                      pThis->pszCurrFName, errStr);
        }
    }
    pThis->iCurrOffs = 0; 
finalize_it:
    free(pThis->pszCurrFName);
    pThis->pszCurrFName = NULL;
    RETiRet;
}
static rsRetVal strmCloseFile ( strm_t * pThis ) { off64_t currOffs ; DEFiRet ; assert ( pThis != NULL ) ; DBGOPRINT ( ( obj_t * ) pThis , " \n " , pThis -> fd , getFileDebugName ( pThis ) , pThis -> bDeleteOnClose ) ; if ( pThis -> tOperationsMode != STREAMMODE_READ ) { strmFlushInternal ( pThis , 0 ) ; if ( pThis -> iZipLevel ) { doZipFinish ( pThis ) ; } if ( pThis -> bAsyncWrite ) { strmWaitAsyncWriterDone ( pThis ) ; } } if ( pThis -> cryprov != NULL && pThis -> fd == -1 ) { const rsRetVal localRet = strmOpenFile ( pThis ) ; if ( localRet != RS_RET_OK ) { LogError ( 0 , localRet , " " " " " " " " , pThis -> pszCurrFName ) ; } } if ( pThis -> fd != -1 ) { currOffs = lseek64 ( pThis -> fd , 0 , SEEK_CUR ) ; close ( pThis -> fd ) ; pThis -> fd = -1 ; pThis -> inode = 0 ; if ( pThis -> cryprov != NULL ) { pThis -> cryprov -> OnFileClose ( pThis -> cryprovFileData , currOffs ) ; pThis -> cryprovFileData = NULL ; } } if ( pThis -> fdDir != -1 ) { close ( pThis -> fdDir ) ; pThis -> fdDir = -1 ; } if ( pThis -> bDeleteOnClose ) { if ( pThis -> pszCurrFName == NULL ) { CHKiRet ( genFileName ( & pThis -> pszCurrFName , pThis -> pszDir , pThis -> lenDir , pThis -> pszFName , pThis -> lenFName , pThis -> iCurrFNum , pThis -> iFileNumDigits ) ) ; } DBGPRINTF ( " \n " , pThis -> pszCurrFName ) ; if ( unlink ( ( char * ) pThis -> pszCurrFName ) == -1 ) { char errStr [ 1024 ] ; int err = errno ; rs_strerror_r ( err , errStr , sizeof ( errStr ) ) ; DBGPRINTF ( " \n " , errno , pThis -> pszCurrFName , errStr ) ; } } pThis -> iCurrOffs = 0 ; finalize_it : free ( pThis -> pszCurrFName ) ; pThis -> pszCurrFName = NULL ; RETiRet ; }
close a strm file
 Note that the bDeleteOnClose flag is honored.
close a strm file Note that the bDeleteOnClose flag is honored .
/* if we have a signature provider, we must make sure that the crypto
     * state files are opened and proper close processing happens. */ /* the file may already be closed (or never have opened), so guard
     * against this. -- rgerhards, 2010-03-19
     */ /* close associated directory handle, if it is open */ /* we are back at begin of file */