xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XrdOssCsiTagstoreFile.hh
Go to the documentation of this file.
1 #ifndef _XRDOSSCSITAGSTOREFILE_H
2 #define _XRDOSSCSITAGSTOREFILE_H
3 /******************************************************************************/
4 /* */
5 /* X r d O s s C s i T a g s t o r e F i l e . h h */
6 /* */
7 /* (C) Copyright 2020 CERN. */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* In applying this licence, CERN does not waive the privileges and */
17 /* immunities granted to it by virtue of its status as an Intergovernmental */
18 /* Organization or submit itself to any jurisdiction. */
19 /* */
20 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
21 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
22 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
23 /* License for more details. */
24 /* */
25 /* You should have received a copy of the GNU Lesser General Public License */
26 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
27 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
28 /* */
29 /* The copyright holder's institutional names and contributor's names may not */
30 /* be used to endorse or promote products derived from this software without */
31 /* specific prior written permission of the institution or contributor. */
32 /******************************************************************************/
33 
34 #include "XrdOss/XrdOss.hh"
35 #include "XrdOssCsiTagstore.hh"
36 #include "XrdOuc/XrdOucCRC.hh"
37 
38 #include <memory>
39 #include <mutex>
40 
41 #if defined(__GLIBC__) || defined(__BIONIC__) || defined(__CYGWIN__)
42 #include <byteswap.h>
43 #elif defined(__APPLE__)
44 // Make sure that byte swap functions are not already defined.
45 #if !defined(bswap_16)
46 // Mac OS X / Darwin features
47 #include <libkern/OSByteOrder.h>
48 #define bswap_16(x) OSSwapInt16(x)
49 #define bswap_32(x) OSSwapInt32(x)
50 #define bswap_64(x) OSSwapInt64(x)
51 #endif
52 #endif
53 
55 {
56 public:
57  XrdOssCsiTagstoreFile(const std::string &fn, std::unique_ptr<XrdOssDF> fd, const char *tid) : fn_(fn), fd_(std::move(fd)), trackinglen_(0), isOpen(false), tident_(tid), tident(tident_.c_str()) { }
58  virtual ~XrdOssCsiTagstoreFile() { if (isOpen) { (void)Close(); } }
59 
60  virtual int Open(const char *, off_t, int, XrdOucEnv &) /* override */;
61  virtual int Close() /* override */;
62 
63  virtual void Flush() /* override */;
64  virtual int Fsync() /* override */;
65 
66  virtual ssize_t WriteTags(const uint32_t *, off_t, size_t) /* override */;
67  virtual ssize_t ReadTags(uint32_t *, off_t, size_t) /* override */;
68 
69  virtual int Truncate(off_t, bool) /* override */;
70 
71  virtual off_t GetTrackedTagSize() const /* override */
72  {
73  if (!isOpen) return 0;
74  return trackinglen_;
75  }
76 
77  virtual off_t GetTrackedDataSize() const /* override */
78  {
79  if (!isOpen) return 0;
80  return actualsize_;
81  }
82 
83  virtual int ResetSizes(const off_t size) /* override */;
84 
85  virtual int SetTrackedSize(const off_t size) /* override */
86  {
87  if (!isOpen) return -EBADF;
88  if (size > actualsize_)
89  {
90  actualsize_ = size;
91  }
92  if (size != trackinglen_)
93  {
94  const int wtt = WriteTrackedTagSize(size);
95  if (wtt<0) return wtt;
96  }
97  return 0;
98  }
99 
100  virtual bool IsVerified() const /* override */
101  {
102  if (!isOpen) return false;
103  if ((hflags_ & XrdOssCsiTagstore::csVer)) return true;
104  return false;
105  }
106 
107  virtual int SetUnverified()
108  {
109  if (!isOpen) return -EBADF;
111  {
113  return MarshallAndWriteHeader();
114  }
115  return 0;
116  }
117 
118  static ssize_t fullread(XrdOssDF &fd, void *buff, const off_t off , const size_t sz)
119  {
120  size_t toread = sz, nread = 0;
121  uint8_t *p = (uint8_t*)buff;
122  while(toread>0)
123  {
124  const ssize_t rret = fd.Read(&p[nread], off+nread, toread);
125  if (rret<0) return rret;
126  if (rret==0) break;
127  toread -= rret;
128  nread += rret;
129  }
130  if (nread != sz) return -EDOM;
131  return nread;
132  }
133 
134  static ssize_t fullwrite(XrdOssDF &fd, const void *buff, const off_t off , const size_t sz)
135  {
136  size_t towrite = sz, nwritten = 0;
137  const uint8_t *p = (const uint8_t*)buff;
138  while(towrite>0)
139  {
140  const ssize_t wret = fd.Write(&p[nwritten], off+nwritten, towrite);
141  if (wret<0) return wret;
142  towrite -= wret;
143  nwritten += wret;
144  }
145  return nwritten;
146  }
147 
148 private:
149  const std::string fn_;
150  std::unique_ptr<XrdOssDF> fd_;
152  off_t actualsize_;
153  bool isOpen;
154  const std::string tident_;
155  const char *tident;
158  uint8_t header_[20];
159  uint32_t hflags_;
160 
161  ssize_t WriteTags_swap(const uint32_t *, off_t, size_t);
162  ssize_t ReadTags_swap(uint32_t *, off_t, size_t);
163 
164  int WriteTrackedTagSize(const off_t size)
165  {
166  if (!isOpen) return -EBADF;
167  trackinglen_ = size;
168  return MarshallAndWriteHeader();
169  }
170 
172  {
173  if (!isOpen) return -EBADF;
174 
175  uint32_t y = cmagic_;
176  if (fileIsBige_ != machineIsBige_) y = bswap_32(y);
177  memcpy(header_, &y, 4);
178 
179  uint64_t x = trackinglen_;
180  if (fileIsBige_ != machineIsBige_) x = bswap_64(x);
181  memcpy(&header_[4], &x, 8);
182 
183  y = hflags_;
184  if (fileIsBige_ != machineIsBige_) y = bswap_32(y);
185  memcpy(&header_[12], &y, 4);
186 
187  uint32_t cv = XrdOucCRC::Calc32C(header_, 16, 0U);
188  if (machineIsBige_ != fileIsBige_) cv = bswap_32(cv);
189  memcpy(&header_[16], &cv, 4);
190 
191  ssize_t wret = fullwrite(*fd_, header_, 0, 20);
192  if (wret<0) return wret;
193  return 0;
194  }
195 
196  static const uint32_t cmagic_ = 0x30544452U;
197 };
198 
199 #endif
virtual ~XrdOssCsiTagstoreFile()
Definition: XrdOssCsiTagstoreFile.hh:58
static const uint32_t csVer
Definition: XrdOssCsiTagstore.hh:63
off_t actualsize_
Definition: XrdOssCsiTagstoreFile.hh:152
virtual void Flush()
virtual int ResetSizes(const off_t size)
int MarshallAndWriteHeader()
Definition: XrdOssCsiTagstoreFile.hh:171
uint8_t header_[20]
Definition: XrdOssCsiTagstoreFile.hh:158
virtual int Open(const char *, off_t, int, XrdOucEnv &)
virtual bool IsVerified() const
Definition: XrdOssCsiTagstoreFile.hh:100
const std::string fn_
Definition: XrdOssCsiTagstoreFile.hh:149
int WriteTrackedTagSize(const off_t size)
Definition: XrdOssCsiTagstoreFile.hh:164
virtual int SetUnverified()
Definition: XrdOssCsiTagstoreFile.hh:107
bool isOpen
Definition: XrdOssCsiTagstoreFile.hh:153
virtual ssize_t ReadTags(uint32_t *, off_t, size_t)
XrdOssCsiTagstoreFile(const std::string &fn, std::unique_ptr< XrdOssDF > fd, const char *tid)
Definition: XrdOssCsiTagstoreFile.hh:57
Definition: XrdOssCsiTagstoreFile.hh:54
off_t trackinglen_
Definition: XrdOssCsiTagstoreFile.hh:151
Definition: XrdOssCsiTagstore.hh:36
Definition: XrdOucEnv.hh:41
const std::string tident_
Definition: XrdOssCsiTagstoreFile.hh:154
virtual off_t GetTrackedTagSize() const
Definition: XrdOssCsiTagstoreFile.hh:71
j template void())
Definition: XrdOucJson.hh:4121
virtual int Truncate(off_t, bool)
static ssize_t fullwrite(XrdOssDF &fd, const void *buff, const off_t off, const size_t sz)
Definition: XrdOssCsiTagstoreFile.hh:134
static ssize_t fullread(XrdOssDF &fd, void *buff, const off_t off, const size_t sz)
Definition: XrdOssCsiTagstoreFile.hh:118
uint32_t hflags_
Definition: XrdOssCsiTagstoreFile.hh:159
const char * tident
Definition: XrdOssCsiTagstoreFile.hh:155
virtual ssize_t Read(off_t offset, size_t size)
Definition: XrdOss.hh:281
static const uint32_t cmagic_
Definition: XrdOssCsiTagstoreFile.hh:196
virtual ssize_t WriteTags(const uint32_t *, off_t, size_t)
virtual off_t GetTrackedDataSize() const
Definition: XrdOssCsiTagstoreFile.hh:77
bool machineIsBige_
Definition: XrdOssCsiTagstoreFile.hh:156
ssize_t WriteTags_swap(const uint32_t *, off_t, size_t)
bool fileIsBige_
Definition: XrdOssCsiTagstoreFile.hh:157
Definition: XrdOss.hh:62
virtual int SetTrackedSize(const off_t size)
Definition: XrdOssCsiTagstoreFile.hh:85
virtual int Fsync()
static uint32_t Calc32C(const void *data, size_t count, uint32_t prevcs=0)
virtual int Close()
std::unique_ptr< XrdOssDF > fd_
Definition: XrdOssCsiTagstoreFile.hh:150
virtual ssize_t Write(const void *buffer, off_t offset, size_t size)
Definition: XrdOss.hh:345
ssize_t ReadTags_swap(uint32_t *, off_t, size_t)