File.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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#ifndef SQUID_BASE_FILE_H
10#define SQUID_BASE_FILE_H
11
12#include "sbuf/SBuf.h"
13
14#if HAVE_SYS_FILE_H
15#include <sys/file.h>
16#endif
17
20{
21public:
22 static FileOpeningConfig ReadOnly(); // shared reading
23 static FileOpeningConfig ReadWrite(); // exclusive creation and/or reading/writing
24
25 /* adjustment methods; named to work well with the File::Be::X shorthand */
26
28 FileOpeningConfig &locked(unsigned int attempts = 5);
29
32
34 FileOpeningConfig &openedByRoot() { openByRoot = true; return *this; }
35
36 /* add more mode adjustment methods as needed */
37
38private:
39 friend class File;
40
41 /* file opening parameters */
42#if _SQUID_WINDOWS_
43 DWORD desiredAccess = 0;
44 DWORD shareMode = 0;
45 DWORD creationDisposition = OPEN_EXISTING;
46#else
48 int openFlags = 0;
50#endif
51
52 /* file locking (disabled unless lock(n) sets positive lockAttempts) */
53#if _SQUID_WINDOWS_
54 DWORD lockFlags = 0;
55#elif _SQUID_SOLARIS_
56 int lockType = F_UNLCK;
57#else
58 int flockMode = LOCK_UN;
59#endif
60 const unsigned int retryGapUsec = 500000;
61 unsigned int lockAttempts = 0;
62 bool openByRoot = false;
63};
64
66class File
67{
68public:
70
72 static File *Optional(const SBuf &aName, const FileOpeningConfig &cfg);
73
74 File(const SBuf &aFilename, const FileOpeningConfig &cfg);
75 ~File();
76
77 /* can move but cannot copy */
78 File(const File &) = delete;
79 File &operator = (const File &) = delete;
80 File(File &&other);
81 File &operator = (File &&other);
82
83 const SBuf &name() const { return name_; }
84
85 /* system call wrappers */
86
88 void truncate();
89 SBuf readSmall(SBuf::size_type minBytes, SBuf::size_type maxBytes);
90 void writeAll(const SBuf &data);
91 void synchronize();
92
93protected:
94 bool isOpen() const {
95#if _SQUID_WINDOWS_
96 return fd_ != InvalidHandle;
97#else
98 return fd_ >= 0;
99#endif
100 }
101
102 void open(const FileOpeningConfig &cfg);
103 void lock(const FileOpeningConfig &cfg);
104 void lockOnce(const FileOpeningConfig &cfg);
105 void close();
106
108 SBuf sysCallFailure(const char *callName, const SBuf &error) const;
110 SBuf sysCallError(const char *callName, const int savedErrno) const;
111
112private:
114
115 // Windows-specific HANDLE is needed because LockFileEx() does not take POSIX FDs.
116#if _SQUID_WINDOWS_
117 typedef HANDLE Handle;
118 static const Handle InvalidHandle;
119#else
120 typedef int Handle;
121 static const Handle InvalidHandle = -1;
122#endif
124};
125
126#endif
127
void error(char *format,...)
How should a file be opened/created? Should it be locked?
Definition: File.h:20
const unsigned int retryGapUsec
pause before each lock retry
Definition: File.h:60
static FileOpeningConfig ReadWrite()
Definition: File.cc:57
unsigned int lockAttempts
how many times to try locking
Definition: File.h:61
mode_t openMode
access mode; 3rd open(2) parameter
Definition: File.h:49
bool openByRoot
Definition: File.h:62
int openFlags
opening flags; 2nd open(2) parameter
Definition: File.h:48
static FileOpeningConfig ReadOnly()
Definition: File.cc:32
int flockMode
2nd flock(2) parameter
Definition: File.h:58
mode_t creationMask
umask() parameter; the default is S_IWGRP|S_IWOTH
Definition: File.h:47
FileOpeningConfig & locked(unsigned int attempts=5)
protect concurrent accesses by attempting to obtain an appropriate lock
Definition: File.cc:82
FileOpeningConfig & createdIfMissing()
when opening a file for writing, create it if it does not exist
Definition: File.cc:90
FileOpeningConfig & openedByRoot()
enter_suid() to open the file; leaves suid ASAP after that
Definition: File.h:34
a portable locking-aware exception-friendly file (with RAII API)
Definition: File.h:67
const SBuf & name() const
Definition: File.h:83
File(const File &)=delete
File(const SBuf &aFilename, const FileOpeningConfig &cfg)
opens
Definition: File.cc:135
Handle fd_
OS-specific file handle.
Definition: File.h:123
void lock(const FileOpeningConfig &cfg)
calls lockOnce() as many times as necessary (including zero)
Definition: File.cc:321
void writeAll(const SBuf &data)
write(2) with a "wrote everything" check
Definition: File.cc:280
FileOpeningConfig Be
convenient shorthand for File() callers
Definition: File.h:69
void open(const FileOpeningConfig &cfg)
opens (or creates) the file
Definition: File.cc:171
static File * Optional(const SBuf &aName, const FileOpeningConfig &cfg)
Definition: File.cc:124
void close()
Definition: File.cc:196
File & operator=(const File &)=delete
void lockOnce(const FileOpeningConfig &cfg)
locks, blocking or returning immediately depending on the lock waiting mode
Definition: File.cc:342
SBuf sysCallError(const char *callName, const int savedErrno) const
Definition: File.cc:372
SBuf sysCallFailure(const char *callName, const SBuf &error) const
Definition: File.cc:365
static const Handle InvalidHandle
Definition: File.h:121
bool isOpen() const
Definition: File.h:94
void truncate()
makes the file size (and the current I/O offset) zero
Definition: File.cc:215
int Handle
Definition: File.h:120
~File()
closes
Definition: File.cc:151
void synchronize()
fsync(2)
Definition: File.cc:304
SBuf readSmall(SBuf::size_type minBytes, SBuf::size_type maxBytes)
read(2) for small files
Definition: File.cc:241
SBuf name_
location on disk
Definition: File.h:113
Definition: SBuf.h:94
MemBlob::size_type size_type
Definition: SBuf.h:96
unsigned short mode_t
Definition: types.h:129

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors