Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
rlimit.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2016-2017 Peter Budai.
4 Copyright (C) 2017 Roman Lebedev.
5 Copyright (C) 2020 Heiko Bauke.
6 Copyright (C) 2020 Pascal Obry.
7 Copyright (C) 2022 Martin Baƙinka.
8
9 darktable is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 darktable is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with darktable. If not, see <http://www.gnu.org/licenses/>.
21*/
22
24//
25// Sample implementation of getrlimit() and setrlimit() for Win32.
26//
27// Includes wrappers around fwrite() and _write() where the wrappers
28// are resource limit aware.
29//
31
32#include <windows.h>
33#include "rlimit.h"
34#include <io.h>
35#include <errno.h>
36#include <inttypes.h>
37
38static BOOL rInitialized = FALSE; // Indicates if the rlimit structure has been initialized
39
40static rlimit_t rlimits[RLIM_NLIMITS]; // Resource limits array on element for each limit we
41 // keep track of.
42
44//
45// InitializeRlimits()
46//
47// Sets the initial values in the rlimits arrar for the process.
48//
51{
52 //
53 // Initialize the rlimits structure with 0 for the current value,
54 // and 2^32 - 1 for the max. This function could be modified
55 // to read the initial values from...
56 // ...the registry...
57 // ...an environment variable...
58 // ...a disk file...
59 // ...other...
60 // which would then be used to populate the rlimits structure.
61 //
62 for(int i = 0; i < RLIM_NLIMITS; i++)
63 {
65 rlimits[i].rlim_max = 0xffffffff;
66 }
68}
69
71// getrlimit()
72//
73// NOTE: Posix spec states function returns 0 on success and -1
74// when an error occurs and sets errno to the error code.
75// Currently, if an error occurs, the errno value is returned
76// rather than -1. errno is not set.
77//
79int getrlimit(int resource, struct rlimit *rlp)
80{
81 //
82 // If we have not initialized the limits yet, do so now
83 //
85
86 //
87 // Check to make sure the resource value is within range
88 //
89 if(resource < 0 || resource >= RLIM_NLIMITS)
90 return EINVAL;
91
92 //
93 // Return both rlim_cur and rlim_max
94 //
95 *rlp = rlimits[resource];
96
97 return 0; // success
98}
99
101// setrlimit()
102//
103// NOTE: Posix spec states function returns 0 on success and -1
104// when an error occurs and sets errno to the error code.
105// Currently, if an error occurs, the errno value is returned
106// rather than -1. errno is not set.
107//
109int setrlimit(int resource, const struct rlimit *rlp)
110{
112 //
113 // Check to make sure the resource value is within range
114 //
115 if(resource < 0 || resource >= RLIM_NLIMITS)
116 return EINVAL;
117 //
118 // Only change the current limit - do not change the max limit.
119 // We could pick some NT privilege, which if the user held, we
120 // would allow the changing of rlim_max.
121 //
122 if(rlp->rlim_cur < rlimits[resource].rlim_max)
123 rlimits[resource].rlim_cur = rlp->rlim_cur;
124 else
125 return EINVAL;
126 //
127 // We should not let the user set the max value. However,
128 // since currently there is no defined source for initial
129 // values, we will let the user change the max value.
130 //
131 rlimits[resource].rlim_max = rlp->rlim_max;
132
133 return 0; // success
134}
135
137// Wrap the real fwrite() with this rfwrite() function, which is
138// resource limit aware.
139//
141size_t rfwrite(const void *buffer, size_t size, size_t count, FILE *stream)
142{
143 //
144 // Convert the count to a large integer (64 bit integer)
145 //
146 const __int64 liByteCount = (__int64)count;
147
148 //
149 // Get the current file position
150 //
151 const __int64 liPosition = (__int64)ftell(stream);
152
153 //
154 // Check to make sure the write will not exceed the RLIMIT_FSIZE limit.
155 //
156 if(liPosition + liByteCount > rlimits[RLIMIT_FSIZE].rlim_cur)
157 {
158 //
159 // report an error
160 //
161 return 0;
162 }
163 //
164 // Do the actual write the user requested
165 //
166 return fwrite(buffer, size, count, stream);
167}
168
170// Wrap the real _write() function with the _rwrite() function
171// which is resource aware.
172//
174int _rwrite(int handle, const void *buffer, unsigned int count)
175{
176 //
177 // Convert the count to a large integer
178 //
179 const int64_t liByteCount = (__int64)count;
180
181 //
182 // Get the Current file position
183 //
184 const int64_t liPosition = (__int64)_tell(handle);
185
186 //
187 // Check to make sure the write will not exceed the RLIMIT_FSIZE limit.
188 //
189 if(liPosition + liByteCount > rlimits[RLIMIT_FSIZE].rlim_cur)
190 {
191 //
192 // report an error
193 //
194 return 0;
195 }
196
197 //
198 // Do the actual write the user requested
199 //
200 return _write(handle, buffer, count);
201}
202
203// clang-format off
204// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
205// vim: shiftwidth=2 expandtab tabstop=2 cindent
206// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
207// clang-format on
208
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
TIFF sidecar I/O API for drawlayer layers.
size_t size
Definition mipmap_cache.c:3
void InitializeRlimits()
Definition rlimit.c:50
size_t rfwrite(const void *buffer, size_t size, size_t count, FILE *stream)
Definition rlimit.c:141
static BOOL rInitialized
Definition rlimit.c:38
static rlimit_t rlimits[7]
Definition rlimit.c:40
int getrlimit(int resource, struct rlimit *rlp)
Definition rlimit.c:79
int _rwrite(int handle, const void *buffer, unsigned int count)
Definition rlimit.c:174
int setrlimit(int resource, const struct rlimit *rlp)
Definition rlimit.c:109
#define RLIM_INFINITY
Definition rlimit.h:34
#define RLIMIT_FSIZE
Definition rlimit.h:26
#define RLIM_NLIMITS
Definition rlimit.h:33
__int64 rlim_cur
Definition rlimit.h:44
__int64 rlim_max
Definition rlimit.h:45