BlockingFile.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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/Stream.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
25BlockingFile::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
38void
40{
41 /* Simulate async calls */
42 fd = file_open(path_, flags);
43 ioRequestor = callback;
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
61void
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;
74 fd = -1;
75 }
76}
77
78void
80{
81 debugs(79, 3, "BlockingFile::close: " << this << " closing for " << ioRequestor.getRaw());
82 doClose();
85}
86
87bool
89{
90 return fd > -1;
91}
92
93bool
95{
96 if ((fd < 0 && !closed) || error_)
97 return true;
98
99 return false;
100}
101
102void BlockingFile::error(bool const &aBool)
103{
104 error_ = aBool;
105}
106
107void
109{
110 assert (fd > -1);
112 readRequest = aRequest;
113 debugs(79, 3, aRequest->len << " for FD " << fd << " at " << aRequest->offset);
114 file_read(fd, aRequest->buf, aRequest->len, aRequest->offset, ReadDone, this);
115}
116
117void
118BlockingFile::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
125void
127{
128 debugs(79, 3, aRequest->len << " for FD " << fd << " at " << aRequest->offset);
129 writeRequest = aRequest;
131 aRequest->offset,
132 (char *)aRequest->buf,
133 aRequest->len,
134 WriteDone,
135 this,
136 aRequest->free_func);
137}
138
139bool
141{
143 return false;
144}
145
146/* === STATIC =========================================================== */
147
148void
149BlockingFile::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 = nullptr;
169
170 ioRequestor->readCompleted(buf, rlen, errflag, result);
171}
172
173void
174BlockingFile::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
180void
181BlockingFile::writeDone(int rvfd, int errflag, size_t len)
182{
183 assert (rvfd == fd);
184 debugs(79, 3, "FD " << fd << ", len " << len);
185
187 writeRequest = nullptr;
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
CBDATA_CLASS_INIT(BlockingFile)
#define true
Definition: GnuRegex.c:241
#define false
Definition: GnuRegex.c:240
#define assert(EX)
Definition: assert.h:19
virtual void read(ReadRequest *)
virtual bool error() const
Definition: BlockingFile.cc:94
virtual void open(int flags, mode_t mode, RefCount< IORequestor > callback)
Definition: BlockingFile.cc:39
void readDone(int fd, const char *buf, int len, int errflag)
RefCount< ReadRequest > readRequest
Definition: BlockingFile.h:46
char const * path_
Definition: BlockingFile.h:44
RefCount< WriteRequest > writeRequest
Definition: BlockingFile.h:47
RefCount< IORequestor > ioRequestor
Definition: BlockingFile.h:45
BlockingFile(char const *path)
Definition: BlockingFile.cc:25
static DWCB WriteDone
Definition: BlockingFile.h:39
virtual bool canRead() const
Definition: BlockingFile.cc:88
virtual void close()
Definition: BlockingFile.cc:79
void doClose()
Definition: BlockingFile.cc:68
void writeDone(int fd, int errflag, size_t len)
virtual void create(int flags, mode_t mode, RefCount< IORequestor > callback)
Definition: BlockingFile.cc:62
virtual bool ioInProgress() const
static DRCB ReadDone
Definition: BlockingFile.h:38
virtual void write(WriteRequest *)
virtual void closeCompleted()=0
virtual void readCompleted(const char *buf, int len, int errflag, RefCount< ReadRequest >)=0
virtual void ioCompletedNotification()=0
virtual void writeCompleted(int errflag, size_t len, RefCount< WriteRequest >)=0
size_t len
Definition: ReadRequest.h:26
off_t offset
Definition: ReadRequest.h:25
char * buf
Definition: ReadRequest.h:24
C * getRaw() const
Definition: RefCount.h:80
FREE * free_func
Definition: WriteRequest.h:28
char const * buf
Definition: WriteRequest.h:25
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:196
#define DBG_CRITICAL
Definition: Stream.h:40
#define DISK_EOF
Definition: defines.h:29
#define DISK_ERROR
Definition: defines.h:28
#define DISK_OK
Definition: defines.h:27
int file_open(const char *path, int mode)
Definition: fs_io.cc:45
void file_read(int fd, char *buf, int req_len, off_t offset, DRCB *handler, void *client_data)
Definition: fs_io.cc:453
void file_close(int fd)
Definition: fs_io.cc:73
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:326
int store_open_disk_fd
#define xstrdup
unsigned short mode_t
Definition: types.h:150
#define safe_free(x)
Definition: xalloc.h:73

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors