w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
Rw11VirtDiskFile.cpp
Go to the documentation of this file.
1// $Id: Rw11VirtDiskFile.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2013-2019 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2019-06-21 1167 1.2 use RfileFd; remove dtor
8// 2018-09-22 1048 1.1.4 BUGFIX: coverity (resource leak)
9// 2018-09-16 1047 1.1.3 coverity fixup (uninitialized scalar)
10// 2017-04-15 875 1.1.2 Open(): add overload with scheme handling
11// 2017-04-07 868 1.1.1 Dump(): add detail arg
12// 2017-03-11 859 1.1 use fWProt
13// 2013-04-14 506 1.0 Initial version
14// 2013-02-13 488 0.1 First draft
15// ---------------------------------------------------------------------------
16
21#include "librtools/RosFill.hpp"
22
23#include "Rw11VirtDiskFile.hpp"
24
25using namespace std;
26
32// all method definitions in namespace Retro
33namespace Retro {
34
35//------------------------------------------+-----------------------------------
37
39 : Rw11VirtDisk(punit),
40 fFd("Rw11VirtDiskFile::fFd."),
41 fSize(0)
42{}
43
44//------------------------------------------+-----------------------------------
46
47bool Rw11VirtDiskFile::Open(const std::string& url, RerrMsg& emsg)
48{
49 return Open(url, "file", emsg);
50}
51
52//------------------------------------------+-----------------------------------
54
55bool Rw11VirtDiskFile::Open(const std::string& url, const std::string& scheme,
56 RerrMsg& emsg)
57{
58 if (!fUrl.Set(url, "|wpro|", scheme, emsg)) return false;
59
60 fWProt = fUrl.FindOpt("wpro");
61
62 if (!fFd.Open(fUrl.Path().c_str(),
63 fWProt ? O_RDONLY : O_RDWR, emsg)) return false;
64
65 struct stat sbuf;
66 if (!fFd.Stat(&sbuf, emsg)) {
67 fFd.Close();
68 return false;
69 }
70
71 if ((sbuf.st_mode & S_IWUSR) == 0) fWProt = true;
72 fSize = sbuf.st_size;
73 return true;
74}
75
76//------------------------------------------+-----------------------------------
78
79bool Rw11VirtDiskFile::Read(size_t lba, size_t nblk, uint8_t* data,
80 RerrMsg& emsg)
81{
83 fStats.Inc(kStatNVDReadBlk, double(nblk));
84
85 size_t seekpos = fBlkSize * lba;
86 size_t nbyt = fBlkSize * nblk;
87
88 if (seekpos >= fSize) {
89 uint8_t* p = data;
90 for (size_t i=0; i<nbyt; i++) *p++ = 0;
91 return true;
92 }
93
94 if (!fFd.Seek(seekpos, SEEK_SET, emsg)) return false;
95 ssize_t irc = fFd.Read(data, nbyt, emsg);
96 if (irc < 0) return false;
97
98 if (irc < ssize_t(nbyt)) {
99 uint8_t* p = data+irc;
100 for (size_t i=irc; i<nbyt; i++) *p++ = 0;
101 }
102
103 return true;
104}
105
106//------------------------------------------+-----------------------------------
108
109bool Rw11VirtDiskFile::Write(size_t lba, size_t nblk, const uint8_t* data,
110 RerrMsg& emsg)
111{
113 fStats.Inc(kStatNVDWriteBlk, double(nblk));
114
115 size_t seekpos = fBlkSize * lba;
116 size_t nbyt = fBlkSize * nblk;
117
118 if (!fFd.Seek(seekpos, SEEK_SET, emsg)) return false;
119 if (!fFd.WriteAll(data, nbyt, emsg)) return false;
120 if (seekpos+nbyt > fSize) fSize = seekpos+nbyt;
121
122 return true;
123}
124
125//------------------------------------------+-----------------------------------
127
128void Rw11VirtDiskFile::Dump(std::ostream& os, int ind, const char* text,
129 int detail) const
130{
131 RosFill bl(ind);
132 os << bl << (text?text:"--") << "Rw11VirtDiskFile @ " << this << endl;
133
134 os << bl << " fFd: " << fFd.Fd() << endl;
135 os << bl << " fSize: " << fSize << endl;
136 Rw11VirtDisk::Dump(os, ind, " ^", detail);
137 return;
138}
139
140} // end namespace Retro
FIXME_docs.
Definition: RerrMsg.hpp:25
int Fd() const
FIXME_docs.
Definition: Rfd.ipp:20
void Close()
FIXME_docs.
Definition: Rfd.cpp:82
bool WriteAll(const void *buf, size_t count, RerrMsg &emsg)
FIXME_docs.
Definition: RfileFd.cpp:119
ssize_t Read(void *buf, size_t count, RerrMsg &emsg)
FIXME_docs.
Definition: RfileFd.cpp:107
bool Stat(struct stat *sbuf, RerrMsg &emsg)
FIXME_docs.
Definition: RfileFd.cpp:71
off_t Seek(off_t offset, int whence, RerrMsg &emsg)
FIXME_docs.
Definition: RfileFd.cpp:83
bool Open(const char *fname, int flags, RerrMsg &emsg)
FIXME_docs.
Definition: RfileFd.cpp:45
I/O appicator to generate fill characters.
Definition: RosFill.hpp:24
bool FindOpt(const std::string &name) const
FIXME_docs.
Definition: RparseUrl.cpp:196
bool Set(const std::string &url, const std::string &optlist, RerrMsg &emsg)
FIXME_docs.
Definition: RparseUrl.cpp:55
const std::string & Path() const
FIXME_docs.
Definition: RparseUrl.ipp:45
void Inc(size_t ind, double val=1.)
FIXME_docs.
Definition: Rstats.ipp:29
FIXME_docs.
Definition: Rw11Unit.hpp:39
Rw11VirtDiskFile(Rw11Unit *punit)
Default constructor.
virtual bool Write(size_t lba, size_t nblk, const uint8_t *data, RerrMsg &emsg)
FIXME_docs.
virtual bool Read(size_t lba, size_t nblk, uint8_t *data, RerrMsg &emsg)
FIXME_docs.
virtual bool Open(const std::string &url, RerrMsg &emsg)
FIXME_docs.
virtual void Dump(std::ostream &os, int ind=0, const char *text=0, int detail=0) const
FIXME_docs.
virtual void Dump(std::ostream &os, int ind=0, const char *text=0, int detail=0) const
FIXME_docs.
size_t fBlkSize
block size in byte
RparseUrl fUrl
Definition: Rw11Virt.hpp:66
Rstats fStats
statistics
Definition: Rw11Virt.hpp:68
bool fWProt
write protected
Definition: Rw11Virt.hpp:67
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47