1
0
mirror of https://github.com/wfjm/w11.git synced 2026-03-10 04:54:26 +00:00

add and use RfileFd

This commit is contained in:
wfjm
2019-06-22 09:12:33 +02:00
parent c858427cf5
commit fd0a66b388
7 changed files with 200 additions and 104 deletions

View File

@@ -1,7 +1,8 @@
# $Id: Makefile 1161 2019-06-08 11:52:01Z mueller $
# $Id: Makefile 1163 2019-06-15 07:26:57Z mueller $
#
# Revision History:
# Date Rev Version Comment
# 2019-06-15 1163 1.1.6 add Rfilefd
# 2019-06-07 1161 1.1.5 add Rfd
# 2019-03-30 1125 1.1.4 add ReventFd,RtimerFd
# 2019-01-02 1100 1.1.3 drop boost includes and libs
@@ -29,6 +30,7 @@ OBJ_all += RerrMsg.o
OBJ_all += ReventFd.o
OBJ_all += Rexception.o
OBJ_all += Rfd.o
OBJ_all += RfileFd.o
OBJ_all += RiosState.o
OBJ_all += RlogFile.o RlogFileCatalog.o RlogMsg.o
OBJ_all += RosFill.o

View File

@@ -0,0 +1,115 @@
// $Id: RfileFd.cpp 1167 2019-06-20 10:17:11Z mueller $
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2019- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// Revision History:
// Date Rev Version Comment
// 2019-06-15 1163 1.0 Initial version
// ---------------------------------------------------------------------------
/*!
\brief Implemenation of class RfileFd.
*/
#include <errno.h>
#include "RfileFd.hpp"
using namespace std;
/*!
\class Retro::RfileFd
\brief FIXME_docs
*/
// all method definitions in namespace Retro
namespace Retro {
//------------------------------------------+-----------------------------------
//! FIXME_docs
RfileFd::RfileFd()
: RfileFd("RfileFd::")
{}
//------------------------------------------+-----------------------------------
//! FIXME_docs
RfileFd::RfileFd(const char* cnam)
: Rfd(cnam)
{}
//------------------------------------------+-----------------------------------
//! FIXME_docs
bool RfileFd::Open(const char* fname, int flags, RerrMsg& emsg)
{
Close();
if (!SetFd(::open(fname, flags))) {
emsg.InitErrno(fCnam+"Open()",
string("open() for '") + fname + "' failed: ", errno);
}
return IsOpen();
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
bool RfileFd::Stat(struct stat *sbuf, RerrMsg& emsg)
{
if (::fstat(fFd, sbuf) < 0) {
emsg.InitErrno(fCnam+"Stat()", "stat() failed: ", errno);
return false;
}
return true;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
off_t RfileFd::Seek(off_t offset, int whence, RerrMsg& emsg)
{
if (::lseek(fFd, offset, whence) < 0) {
emsg.InitErrno(fCnam+"Seek()", "seek() failed: ", errno);
return false;
}
return true;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
bool RfileFd::Truncate(off_t length, RerrMsg& emsg)
{
if (::ftruncate(fFd, length) < 0) {
emsg.InitErrno(fCnam+"Truncate()", "ftruncate() failed: ", errno);
return false;
}
return true;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
ssize_t RfileFd::Read(void *buf, size_t count, RerrMsg& emsg)
{
ssize_t irc = ::read(fFd, buf, count);
if (irc < 0) {
emsg.InitErrno(fCnam+"Read()", "read() failed: ", errno);
}
return irc;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
bool RfileFd::WriteAll(const void *buf, size_t count, RerrMsg& emsg)
{
ssize_t irc = ::write(fFd, buf, count);
if (irc < ssize_t(count)) {
emsg.InitErrno("WriteAll()", "write() failed: ", errno);
return false;
}
return true;
}
} // end namespace Retro

View File

@@ -0,0 +1,50 @@
// $Id: RfileFd.hpp 1167 2019-06-20 10:17:11Z mueller $
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2019- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// Revision History:
// Date Rev Version Comment
// 2019-06-15 1163 1.0 Initial version
// ---------------------------------------------------------------------------
/*!
\brief Declaration of class \c RfileFd.
*/
#ifndef included_Retro_RfileFd
#define included_Retro_RfileFd 1
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "Rfd.hpp"
#include "RerrMsg.hpp"
namespace Retro {
class RfileFd : public Rfd {
public:
RfileFd();
explicit RfileFd(const char* cnam);
RfileFd(const RfileFd&) = delete; // noncopyable
RfileFd& operator=(const RfileFd&) = delete; // noncopyable
bool Open(const char* fname, int flags, RerrMsg& emsg);
bool Stat(struct stat *sbuf, RerrMsg& emsg);
off_t Seek(off_t offset, int whence, RerrMsg& emsg);
bool Truncate(off_t length, RerrMsg& emsg);
ssize_t Read(void *buf, size_t count, RerrMsg& emsg);
bool WriteAll(const void *buf, size_t count, RerrMsg& emsg);
};
} // end namespace Retro
//#include "RfileFd.ipp"
#endif

View File

@@ -1,6 +1,6 @@
// $Id: Rw11VirtDisk.cpp 1076 2018-12-02 12:45:49Z mueller $
//
// Copyright 2013-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
// $Id: Rw11VirtDisk.cpp 1167 2019-06-20 10:17:11Z mueller $
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2013-2019 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
@@ -13,6 +13,7 @@
//
// Revision History:
// Date Rev Version Comment
// 2019-06-21 1167 1.4.1 remove dtor
// 2018-12-02 1076 1.4 use unique_ptr for New()
// 2018-10-27 1061 1.3 add fNCyl,fNHead,fNSect; add Rw11VirtDiskRam
// 2017-04-07 868 1.2.1 Dump(): add detail arg
@@ -69,12 +70,6 @@ Rw11VirtDisk::Rw11VirtDisk(Rw11Unit* punit)
fStats.Define(kStatNVDWriteBlk,"NVDWriteBlk", "blocks written");
}
//------------------------------------------+-----------------------------------
//! Destructor
Rw11VirtDisk::~Rw11VirtDisk()
{}
//------------------------------------------+-----------------------------------
//! FIXME_docs

View File

@@ -1,18 +1,10 @@
// $Id: Rw11VirtDisk.hpp 1076 2018-12-02 12:45:49Z mueller $
//
// $Id: Rw11VirtDisk.hpp 1167 2019-06-20 10:17:11Z mueller $
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2013-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for complete details.
//
// Revision History:
// Date Rev Version Comment
// 2019-06-21 1167 1.3.1 remove dtor
// 2018-12-02 1076 1.3 use unique_ptr for New()
// 2018-10-27 1061 1.2 add fNCyl,fNHead,fNSect,NCylinder(),...
// 2017-04-07 868 1.1.1 Dump(): add detail arg
@@ -40,7 +32,6 @@ namespace Retro {
class Rw11VirtDisk : public Rw11Virt {
public:
explicit Rw11VirtDisk(Rw11Unit* punit);
~Rw11VirtDisk();
void Setup(size_t blksize, size_t nblock,
size_t ncyl, size_t nhead, size_t nsect);

View File

@@ -1,18 +1,10 @@
// $Id: Rw11VirtDiskFile.cpp 1048 2018-09-22 07:41:46Z mueller $
//
// Copyright 2013-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for complete details.
// $Id: Rw11VirtDiskFile.cpp 1167 2019-06-20 10:17:11Z mueller $
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2013-2019 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// Revision History:
// Date Rev Version Comment
// 2019-06-21 1167 1.2 use RfileFd; remove dtor
// 2018-09-22 1048 1.1.4 BUGFIX: coverity (resource leak)
// 2018-09-16 1047 1.1.3 coverity fixup (uninitialized scalar)
// 2017-04-15 875 1.1.2 Open(): add overload with scheme handling
@@ -51,18 +43,10 @@ namespace Retro {
Rw11VirtDiskFile::Rw11VirtDiskFile(Rw11Unit* punit)
: Rw11VirtDisk(punit),
fFd(0),
fFd("Rw11VirtDiskFile::fFd."),
fSize(0)
{}
//------------------------------------------+-----------------------------------
//! Destructor
Rw11VirtDiskFile::~Rw11VirtDiskFile()
{
if (fFd > 2) ::close(fFd);
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
@@ -80,25 +64,17 @@ bool Rw11VirtDiskFile::Open(const std::string& url, const std::string& scheme,
if (!fUrl.Set(url, "|wpro|", scheme, emsg)) return false;
fWProt = fUrl.FindOpt("wpro");
int fd = ::open(fUrl.Path().c_str(), fWProt ? O_RDONLY : O_RDWR);
if (fd < 0) {
emsg.InitErrno("Rw11VirtDiskFile::Open()",
string("open() for '") + fUrl.Path() + "' failed: ", errno);
return false;
}
if (!fFd.Open(fUrl.Path().c_str(),
fWProt ? O_RDONLY : O_RDWR, emsg)) return false;
struct stat sbuf;
if (::fstat(fd, &sbuf) < 0) {
emsg.InitErrno("Rw11VirtDiskFile::Open()",
string("stat() for '") + fUrl.Path() + "' failed: ", errno);
::close(fd);
if (!fFd.Stat(&sbuf, emsg)) {
fFd.Close();
return false;
}
if ((sbuf.st_mode & S_IWUSR) == 0) fWProt = true;
fFd = fd;
fSize = sbuf.st_size;
return true;
}
@@ -121,13 +97,9 @@ bool Rw11VirtDiskFile::Read(size_t lba, size_t nblk, uint8_t* data,
return true;
}
if (!Seek(seekpos, emsg)) return false;
ssize_t irc = ::read(fFd, data, nbyt);
if (irc < 0) {
emsg.InitErrno("Rw11VirtDiskFile::Read()", "read() failed: ", errno);
return false;
}
if (!fFd.Seek(seekpos, SEEK_SET, emsg)) return false;
ssize_t irc = fFd.Read(data, nbyt, emsg);
if (irc < 0) return false;
if (irc < ssize_t(nbyt)) {
uint8_t* p = data+irc;
@@ -149,14 +121,8 @@ bool Rw11VirtDiskFile::Write(size_t lba, size_t nblk, const uint8_t* data,
size_t seekpos = fBlkSize * lba;
size_t nbyt = fBlkSize * nblk;
if (!Seek(seekpos, emsg)) return false;
ssize_t irc = ::write(fFd, data, nbyt);
if (irc < ssize_t(nbyt)) {
emsg.InitErrno("Rw11VirtDiskFile::Write()", "write() failed: ", errno);
return false;
}
if (!fFd.Seek(seekpos, SEEK_SET, emsg)) return false;
if (!fFd.WriteAll(data, nbyt, emsg)) return false;
if (seekpos+nbyt > fSize) fSize = seekpos+nbyt;
return true;
@@ -171,23 +137,10 @@ void Rw11VirtDiskFile::Dump(std::ostream& os, int ind, const char* text,
RosFill bl(ind);
os << bl << (text?text:"--") << "Rw11VirtDiskFile @ " << this << endl;
os << bl << " fFd: " << fFd << endl;
os << bl << " fFd: " << fFd.Fd() << endl;
os << bl << " fSize: " << fSize << endl;
Rw11VirtDisk::Dump(os, ind, " ^", detail);
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
bool Rw11VirtDiskFile::Seek(size_t seekpos, RerrMsg& emsg)
{
if (::lseek(fFd, seekpos, SEEK_SET) < 0) {
emsg.InitErrno("Rw11VirtDiskFile::Seek()", "seek() failed: ", errno);
return false;
}
return true;
}
} // end namespace Retro

View File

@@ -1,18 +1,10 @@
// $Id: Rw11VirtDiskFile.hpp 983 2018-01-02 20:35:59Z mueller $
//
// Copyright 2013-2017 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for complete details.
// $Id: Rw11VirtDiskFile.hpp 1167 2019-06-20 10:17:11Z mueller $
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2013-2019 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// Revision History:
// Date Rev Version Comment
// 2019-06-21 1167 1.1 use RfileFd; remove dtor
// 2017-04-15 875 1.0.2 Open(): add overload with scheme handling
// 2017-04-07 868 1.0.1 Dump(): add detail arg
// 2013-04-14 506 1.0 Initial version
@@ -28,6 +20,8 @@
#ifndef included_Retro_Rw11VirtDiskFile
#define included_Retro_Rw11VirtDiskFile 1
#include "librtools/RfileFd.hpp"
#include "Rw11VirtDisk.hpp"
namespace Retro {
@@ -36,7 +30,6 @@ namespace Retro {
public:
explicit Rw11VirtDiskFile(Rw11Unit* punit);
~Rw11VirtDiskFile();
virtual bool Open(const std::string& url, RerrMsg& emsg);
bool Open(const std::string& url, const std::string& scheme,
@@ -53,13 +46,10 @@ namespace Retro {
// statistics counter indices (now new)
enum stats {
kDimStat = Rw11VirtDisk::kDimStat
};
};
protected:
bool Seek(size_t seekpos, RerrMsg& emsg);
protected:
int fFd;
RfileFd fFd;
size_t fSize;
};