BlockingFile.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 47 Store Directory Routines */
10 
11 #include "squid.h"
12 #include "BlockingFile.h"
13 #include "Debug.h"
14 #include "defines.h"
15 #include "DiskIO/IORequestor.h"
16 #include "DiskIO/ReadRequest.h"
17 #include "DiskIO/WriteRequest.h"
18 #include "fs_io.h"
19 #include "globals.h"
20 
21 #include <cerrno>
22 
24 
25 BlockingFile::BlockingFile(char const *aPath) : fd(-1), closed(true), error_(false)
26 {
27  assert(aPath);
28  debugs(79, 3, "BlockingFile::BlockingFile: " << aPath);
29  path_ = xstrdup(aPath);
30 }
31 
33 {
35  doClose();
36 }
37 
38 void
40 {
41  /* Simulate async calls */
42  fd = file_open(path_, flags);
44 
45  if (fd < 0) {
46  debugs(79, 3, "BlockingFile::open: got failure (" << errno << ")");
47  error(true);
48  } else {
49  closed = false;
51  debugs(79, 3, "BlockingFile::open: opened FD " << fd);
52  }
53 
54  callback->ioCompletedNotification();
55 }
56 
61 void
63 {
64  /* We use the same logic path for open */
65  open(flags, mode, callback);
66 }
67 
69 {
70  if (fd > -1) {
71  closed = true;
72  file_close(fd);
74  fd = -1;
75  }
76 }
77 
78 void
80 {
81  debugs(79, 3, "BlockingFile::close: " << this << " closing for " << ioRequestor.getRaw());
82  doClose();
85 }
86 
87 bool
89 {
90  return fd > -1;
91 }
92 
93 bool
95 {
96  if ((fd < 0 && !closed) || error_)
97  return true;
98 
99  return false;
100 }
101 
102 void BlockingFile::error(bool const &aBool)
103 {
104  error_ = aBool;
105 }
106 
107 void
109 {
110  assert (fd > -1);
112  readRequest = aRequest;
113  debugs(79, 3, HERE << aRequest->len << " for FD " << fd << " at " << aRequest->offset);
114  file_read(fd, aRequest->buf, aRequest->len, aRequest->offset, ReadDone, this);
115 }
116 
117 void
118 BlockingFile::ReadDone(int fd, const char *buf, int len, int errflag, void *my_data)
119 {
120  BlockingFile *myFile = static_cast<BlockingFile *>(my_data);
121  assert (myFile);
122  myFile->readDone (fd, buf, len, errflag);
123 }
124 
125 void
127 {
128  debugs(79, 3, HERE << aRequest->len << " for FD " << fd << " at " << aRequest->offset);
129  writeRequest = aRequest;
130  file_write(fd,
131  aRequest->offset,
132  (char *)aRequest->buf,
133  aRequest->len,
134  WriteDone,
135  this,
136  aRequest->free_func);
137 }
138 
139 bool
141 {
143  return false;
144 }
145 
146 /* === STATIC =========================================================== */
147 
148 void
149 BlockingFile::readDone(int rvfd, const char *buf, int len, int errflag)
150 {
151  debugs(79, 3, "BlockingFile::readDone: FD " << rvfd);
152  assert (fd == rvfd);
153 
154  ssize_t rlen;
155 
156  if (errflag) {
157  debugs(79, 3, "BlockingFile::readDone: got failure (" << errflag << ")");
158  rlen = -1;
159  } else {
160  rlen = (ssize_t) len;
161  }
162 
163  if (errflag == DISK_EOF)
164  errflag = DISK_OK; /* EOF is signalled by len == 0, not errors... */
165 
167 
168  readRequest = NULL;
169 
170  ioRequestor->readCompleted(buf, rlen, errflag, result);
171 }
172 
173 void
174 BlockingFile::WriteDone (int fd, int errflag, size_t len, void *me)
175 {
176  BlockingFile *aFile = static_cast<BlockingFile *>(me);
177  aFile->writeDone (fd, errflag, len);
178 }
179 
180 void
181 BlockingFile::writeDone(int rvfd, int errflag, size_t len)
182 {
183  assert (rvfd == fd);
184  debugs(79, 3, HERE << "FD " << fd << ", len " << len);
185 
187  writeRequest = NULL;
188 
189  if (errflag) {
190  debugs(79, DBG_CRITICAL, "storeUfsWriteDone: got failure (" << errflag << ")");
191  doClose();
193  return;
194  }
195 
196  ioRequestor->writeCompleted(DISK_OK, len, result);
197 }
198 
BlockingFile(char const *path)
Definition: BlockingFile.cc:25
#define assert(EX)
Definition: assert.h:17
FREE * free_func
Definition: WriteRequest.h:28
void const char HLPCB * callback
Definition: stub_helper.cc:16
static DRCB ReadDone
Definition: BlockingFile.h:38
RefCount< IORequestor > ioRequestor
Definition: BlockingFile.h:45
size_t len
Definition: ReadRequest.h:26
#define xstrdup
virtual void write(WriteRequest *)
#define safe_free(x)
Definition: xalloc.h:73
int file_open(const char *path, int mode)
Definition: fs_io.cc:46
char * buf
Definition: ReadRequest.h:24
#define DBG_CRITICAL
Definition: Debug.h:44
virtual void create(int flags, mode_t mode, RefCount< IORequestor > callback)
Definition: BlockingFile.cc:62
virtual bool ioInProgress() const
void readDone(int fd, const char *buf, int len, int errflag)
#define DISK_ERROR
Definition: defines.h:40
virtual void read(ReadRequest *)
CBDATA_CLASS_INIT(BlockingFile)
off_t offset
Definition: ReadRequest.h:25
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
void file_write(int fd, off_t file_offset, void const *ptr_to_buf, int len, DWCB *handle, void *handle_data, FREE *free_func)
Definition: fs_io.cc:344
#define true
Definition: GnuRegex.c:234
virtual void closeCompleted()=0
virtual void open(int flags, mode_t mode, RefCount< IORequestor > callback)
Definition: BlockingFile.cc:39
char const * buf
Definition: WriteRequest.h:25
virtual void close()
Definition: BlockingFile.cc:79
void writeDone(int fd, int errflag, size_t len)
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
#define DISK_EOF
Definition: defines.h:41
unsigned short mode_t
Definition: types.h:150
virtual void readCompleted(const char *buf, int len, int errflag, RefCount< ReadRequest >)=0
char const * path_
Definition: BlockingFile.h:44
virtual bool error() const
Definition: BlockingFile.cc:94
int store_open_disk_fd
static DWCB WriteDone
Definition: BlockingFile.h:39
virtual bool canRead() const
Definition: BlockingFile.cc:88
#define DISK_OK
Definition: defines.h:39
RefCount< WriteRequest > writeRequest
Definition: BlockingFile.h:47
virtual void ioCompletedNotification()=0
void file_close(int fd)
Definition: fs_io.cc:76
void file_read(int fd, char *buf, int req_len, off_t offset, DRCB *handler, void *client_data)
Definition: fs_io.cc:479
virtual void writeCompleted(int errflag, size_t len, RefCount< WriteRequest >)=0
RefCount< ReadRequest > readRequest
Definition: BlockingFile.h:46
C * getRaw() const
Definition: RefCount.h:74
#define NULL
Definition: types.h:166
void doClose()
Definition: BlockingFile.cc:68
#define false
Definition: GnuRegex.c:233

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors