Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes

blk_StdIOWriter Class Reference
[IVD Data format.]

#include <df.h>

Inheritance diagram for blk_StdIOWriter:
Inheritance graph
[legend]
Collaboration diagram for blk_StdIOWriter:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 blk_StdIOWriter (df_BlockManager &a_mgr, const string &a_fName)
virtual ~blk_StdIOWriter ()

Private Member Functions

virtual void Seek (const UInt32_t a_offset)
 Seek forward for the specified number of bytes.
virtual void Write (const UInt8_t *a_data, const UInt32_t a_size)
 Write one block of data to stdout.

Private Attributes

 log_CLASSID_m
int m_fHandle
UInt32_t m_seqNumber

Static Private Attributes

static UInt32_t m_numActive = 0
static cmn_Mutex m_numActive_x

Detailed Description

Definition at line 374 of file df.h.


Constructor & Destructor Documentation

blk_StdIOWriter::blk_StdIOWriter ( df_BlockManager a_mgr,
const string &  a_fName 
)

Definition at line 62 of file blk_stdiowriter.cpp.

References dbg_NORM, errno, log_DBG_m, log_FUNC_A_m, m_fHandle, m_numActive, m_numActive_x, m_seqNumber, and O_BINARY.

    : blk_BufferWriter(a_mgr),
      m_seqNumber(0) {

    log_FUNC_A_m(blk_StdIOWriter, "fName=" << a_fName);

    if (a_fName.empty()) {
        log_DBG_m(dbg_NORM, "Using stdout for DF output");
        m_fHandle = dup(fileno(stdout));
        if (m_fHandle == -1) {
            throw ivd_SysError(errno, "dup()", true);
        }
#if TGT_OS_windows
        setmode(m_fHandle, O_BINARY);
#endif
    }
    else {
#if IVD_POSIX_OS
        m_fHandle = open(a_fName.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
#else
        m_fHandle = open(a_fName.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_BINARY );
#endif
        if (m_fHandle == -1) {
            throw ivd_SysError(errno, "open()", true);
        }
    }

    cmn_MutexLock l(m_numActive_x);
    m_numActive++;
    m_seqNumber = m_numActive;
}

blk_StdIOWriter::~blk_StdIOWriter (  )  [virtual]

Definition at line 96 of file blk_stdiowriter.cpp.

References errno, log_ERR_m, log_FUNC_m, m_fHandle, m_numActive, and m_numActive_x.

                                  {
    log_FUNC_m(~blk_StdIOWriter);

    cmn_MutexLock l(m_numActive_x);
    m_numActive--;

    if (fsync(m_fHandle) != 0) {
        log_ERR_m(
            "fsync() failed. Ignored. Error: " << errno);
    }

/*    if (m_fHandle != _fileno(stdout)) */ {
        if (close(m_fHandle) != 0) {
           log_ERR_m(
                "close() failed. Ignored. Error: " << errno);
        }
    }
}


Member Function Documentation

void blk_StdIOWriter::Seek ( const UInt32_t  a_offset  )  [private, virtual]

Seek forward for the specified number of bytes.

Used mainly to skip file if migration of a file is aborted. Will just write zeros

Reimplemented from blk_BufferWriter.

Definition at line 121 of file blk_stdiowriter.cpp.

References dbg_DETAIL, errno, log_DBG_m, log_FUNC_m, and m_fHandle.

                                                  {
    log_FUNC_m(Seek);

    log_DBG_m(dbg_DETAIL,
            "Seek forward for " << (a_offset/1024) << " kiB.");

    UInt8_t zeros[4096];
    memset(zeros, 0, 4096);

    UInt32_t bytesLeft = a_offset;
    while (bytesLeft > 0) {
        UInt32_t bufSize = bytesLeft < 4096 ? bytesLeft : 4096;
        Int32_t bytesWritten = write(m_fHandle, zeros, bufSize);
        if (bytesWritten == -1) {
            throw ivd_SysError(errno, "write()", true);
        }
        bytesLeft -= bytesWritten;
    }
}

void blk_StdIOWriter::Write ( const UInt8_t a_data,
const UInt32_t  a_size 
) [private, virtual]

Write one block of data to stdout.

Implements blk_BufferWriter.

Definition at line 144 of file blk_stdiowriter.cpp.

References dbg_DETAIL, errno, log_DBG_m, log_FUNC_m, and m_fHandle.

                                                                        {
    log_FUNC_m(Write);

    log_DBG_m(dbg_DETAIL, "About to write " << a_size << " bytes");
    Int32_t bytesWritten = write(m_fHandle, a_data, a_size);
    log_DBG_m(dbg_DETAIL, "Written " << a_size << " bytes");
    if (bytesWritten == -1) {
        throw ivd_SysError(errno, "write()", true);
    }
}


Member Data Documentation

Reimplemented from blk_BufferWriter.

Definition at line 386 of file df.h.

Definition at line 388 of file df.h.

Referenced by blk_StdIOWriter(), Seek(), Write(), and ~blk_StdIOWriter().

UInt32_t blk_StdIOWriter::m_numActive = 0 [static, private]

Definition at line 389 of file df.h.

Referenced by blk_StdIOWriter(), and ~blk_StdIOWriter().

Definition at line 390 of file df.h.

Referenced by blk_StdIOWriter(), and ~blk_StdIOWriter().

Definition at line 392 of file df.h.

Referenced by blk_StdIOWriter().


The documentation for this class was generated from the following files: