ServerState is a struct and not a class (clang mismatch type warning)
[ntk/apt.git] / apt-pkg / contrib / mmap.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: mmap.cc,v 1.22 2001/05/27 05:19:30 jgg Exp $
4 /* ######################################################################
5
6 MMap Class - Provides 'real' mmap or a faked mmap using read().
7
8 MMap cover class.
9
10 Some broken versions of glibc2 (libc6) have a broken definition
11 of mmap that accepts a char * -- all other systems (and libc5) use
12 void *. We can't safely do anything here that would be portable, so
13 libc6 generates warnings -- which should be errors, g++ isn't properly
14 strict.
15
16 ##################################################################### */
17 /*}}}*/
18 // Include Files /*{{{*/
19 #define _BSD_SOURCE
20 #include <apt-pkg/mmap.h>
21 #include <apt-pkg/error.h>
22
23 #include <apti18n.h>
24
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <stdlib.h>
30 #include <errno.h>
31
32 #include <cstring>
33 /*}}}*/
34
35 // MMap::MMap - Constructor /*{{{*/
36 // ---------------------------------------------------------------------
37 /* */
38 MMap::MMap(FileFd &F,unsigned long Flags) : Flags(Flags), iSize(0),
39 Base(0), SyncToFd(NULL)
40 {
41 if ((Flags & NoImmMap) != NoImmMap)
42 Map(F);
43 }
44 /*}}}*/
45 // MMap::MMap - Constructor /*{{{*/
46 // ---------------------------------------------------------------------
47 /* */
48 MMap::MMap(unsigned long Flags) : Flags(Flags), iSize(0),
49 Base(0), SyncToFd(NULL)
50 {
51 }
52 /*}}}*/
53 // MMap::~MMap - Destructor /*{{{*/
54 // ---------------------------------------------------------------------
55 /* */
56 MMap::~MMap()
57 {
58 Close();
59 }
60 /*}}}*/
61 // MMap::Map - Perform the mapping /*{{{*/
62 // ---------------------------------------------------------------------
63 /* */
64 bool MMap::Map(FileFd &Fd)
65 {
66 iSize = Fd.Size();
67
68 // Set the permissions.
69 int Prot = PROT_READ;
70 int Map = MAP_SHARED;
71 if ((Flags & ReadOnly) != ReadOnly)
72 Prot |= PROT_WRITE;
73 if ((Flags & Public) != Public)
74 Map = MAP_PRIVATE;
75
76 if (iSize == 0)
77 return _error->Error(_("Can't mmap an empty file"));
78
79 // Map it.
80 Base = mmap(0,iSize,Prot,Map,Fd.Fd(),0);
81 if (Base == (void *)-1)
82 {
83 if (errno == ENODEV || errno == EINVAL)
84 {
85 // The filesystem doesn't support this particular kind of mmap.
86 // So we allocate a buffer and read the whole file into it.
87 int const dupped_fd = dup(Fd.Fd());
88 if (dupped_fd == -1)
89 return _error->Errno("mmap", _("Couldn't duplicate file descriptor %i"), Fd.Fd());
90
91 Base = new unsigned char[iSize];
92 SyncToFd = new FileFd (dupped_fd);
93 if (!SyncToFd->Seek(0L) || !SyncToFd->Read(Base, iSize))
94 return false;
95 }
96 else
97 return _error->Errno("mmap",_("Couldn't make mmap of %lu bytes"),
98 iSize);
99 }
100
101 return true;
102 }
103 /*}}}*/
104 // MMap::Close - Close the map /*{{{*/
105 // ---------------------------------------------------------------------
106 /* */
107 bool MMap::Close(bool DoSync)
108 {
109 if ((Flags & UnMapped) == UnMapped || validData() == false || iSize == 0)
110 return true;
111
112 if (DoSync == true)
113 Sync();
114
115 if (SyncToFd != NULL)
116 {
117 delete[] (char *)Base;
118 delete SyncToFd;
119 SyncToFd = NULL;
120 }
121 else
122 {
123 if (munmap((char *)Base, iSize) != 0)
124 _error->WarningE("mmap", _("Unable to close mmap"));
125 }
126
127 iSize = 0;
128 Base = 0;
129 return true;
130 }
131 /*}}}*/
132 // MMap::Sync - Syncronize the map with the disk /*{{{*/
133 // ---------------------------------------------------------------------
134 /* This is done in syncronous mode - the docs indicate that this will
135 not return till all IO is complete */
136 bool MMap::Sync()
137 {
138 if ((Flags & UnMapped) == UnMapped)
139 return true;
140
141 #ifdef _POSIX_SYNCHRONIZED_IO
142 if ((Flags & ReadOnly) != ReadOnly)
143 {
144 if (SyncToFd != NULL)
145 {
146 if (!SyncToFd->Seek(0) || !SyncToFd->Write(Base, iSize))
147 return false;
148 }
149 else
150 {
151 if (msync((char *)Base, iSize, MS_SYNC) < 0)
152 return _error->Errno("msync", _("Unable to synchronize mmap"));
153 }
154 }
155 #endif
156 return true;
157 }
158 /*}}}*/
159 // MMap::Sync - Syncronize a section of the file to disk /*{{{*/
160 // ---------------------------------------------------------------------
161 /* */
162 bool MMap::Sync(unsigned long Start,unsigned long Stop)
163 {
164 if ((Flags & UnMapped) == UnMapped)
165 return true;
166
167 #ifdef _POSIX_SYNCHRONIZED_IO
168 unsigned long PSize = sysconf(_SC_PAGESIZE);
169 if ((Flags & ReadOnly) != ReadOnly)
170 {
171 if (SyncToFd != 0)
172 {
173 if (!SyncToFd->Seek(0) ||
174 !SyncToFd->Write (((char *)Base)+Start, Stop-Start))
175 return false;
176 }
177 else
178 {
179 if (msync((char *)Base+(int)(Start/PSize)*PSize,Stop - Start,MS_SYNC) < 0)
180 return _error->Errno("msync", _("Unable to synchronize mmap"));
181 }
182 }
183 #endif
184 return true;
185 }
186 /*}}}*/
187
188 // DynamicMMap::DynamicMMap - Constructor /*{{{*/
189 // ---------------------------------------------------------------------
190 /* */
191 DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &Workspace,
192 unsigned long const &Grow, unsigned long const &Limit) :
193 MMap(F,Flags | NoImmMap), Fd(&F), WorkSpace(Workspace),
194 GrowFactor(Grow), Limit(Limit)
195 {
196 if (_error->PendingError() == true)
197 return;
198
199 unsigned long EndOfFile = Fd->Size();
200 if (EndOfFile > WorkSpace)
201 WorkSpace = EndOfFile;
202 else if(WorkSpace > 0)
203 {
204 Fd->Seek(WorkSpace - 1);
205 char C = 0;
206 Fd->Write(&C,sizeof(C));
207 }
208
209 Map(F);
210 iSize = EndOfFile;
211 }
212 /*}}}*/
213 // DynamicMMap::DynamicMMap - Constructor for a non-file backed map /*{{{*/
214 // ---------------------------------------------------------------------
215 /* We try here to use mmap to reserve some space - this is much more
216 cooler than the fallback solution to simply allocate a char array
217 and could come in handy later than we are able to grow such an mmap */
218 DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace,
219 unsigned long const &Grow, unsigned long const &Limit) :
220 MMap(Flags | NoImmMap | UnMapped), Fd(0), WorkSpace(WorkSpace),
221 GrowFactor(Grow), Limit(Limit)
222 {
223 if (_error->PendingError() == true)
224 return;
225
226 // disable Moveable if we don't grow
227 if (Grow == 0)
228 this->Flags &= ~Moveable;
229
230 #ifndef __linux__
231 // kfreebsd doesn't have mremap, so we use the fallback
232 if ((this->Flags & Moveable) == Moveable)
233 this->Flags |= Fallback;
234 #endif
235
236 #ifdef _POSIX_MAPPED_FILES
237 if ((this->Flags & Fallback) != Fallback) {
238 // Set the permissions.
239 int Prot = PROT_READ;
240 #ifdef MAP_ANONYMOUS
241 int Map = MAP_PRIVATE | MAP_ANONYMOUS;
242 #else
243 int Map = MAP_PRIVATE | MAP_ANON;
244 #endif
245 if ((this->Flags & ReadOnly) != ReadOnly)
246 Prot |= PROT_WRITE;
247 if ((this->Flags & Public) == Public)
248 #ifdef MAP_ANONYMOUS
249 Map = MAP_SHARED | MAP_ANONYMOUS;
250 #else
251 Map = MAP_SHARED | MAP_ANON;
252 #endif
253
254 // use anonymous mmap() to get the memory
255 Base = (unsigned char*) mmap(0, WorkSpace, Prot, Map, -1, 0);
256
257 if(Base == MAP_FAILED)
258 _error->Errno("DynamicMMap",_("Couldn't make mmap of %lu bytes"),WorkSpace);
259
260 iSize = 0;
261 return;
262 }
263 #endif
264 // fallback to a static allocated space
265 Base = new unsigned char[WorkSpace];
266 memset(Base,0,WorkSpace);
267 iSize = 0;
268 }
269 /*}}}*/
270 // DynamicMMap::~DynamicMMap - Destructor /*{{{*/
271 // ---------------------------------------------------------------------
272 /* We truncate the file to the size of the memory data set */
273 DynamicMMap::~DynamicMMap()
274 {
275 if (Fd == 0)
276 {
277 if (validData() == false)
278 return;
279 #ifdef _POSIX_MAPPED_FILES
280 munmap(Base, WorkSpace);
281 #else
282 delete [] (unsigned char *)Base;
283 #endif
284 return;
285 }
286
287 unsigned long EndOfFile = iSize;
288 iSize = WorkSpace;
289 Close(false);
290 if(ftruncate(Fd->Fd(),EndOfFile) < 0)
291 _error->Errno("ftruncate", _("Failed to truncate file"));
292 }
293 /*}}}*/
294 // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
295 // ---------------------------------------------------------------------
296 /* This allocates a block of memory aligned to the given size */
297 unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln)
298 {
299 unsigned long Result = iSize;
300 if (Aln != 0)
301 Result += Aln - (iSize%Aln);
302
303 iSize = Result + Size;
304
305 // try to grow the buffer
306 while(Result + Size > WorkSpace)
307 {
308 if(!Grow())
309 {
310 _error->Fatal(_("Dynamic MMap ran out of room. Please increase the size "
311 "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace);
312 return 0;
313 }
314 }
315 return Result;
316 }
317 /*}}}*/
318 // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
319 // ---------------------------------------------------------------------
320 /* This allocates an Item of size ItemSize so that it is aligned to its
321 size in the file. */
322 unsigned long DynamicMMap::Allocate(unsigned long ItemSize)
323 {
324 // Look for a matching pool entry
325 Pool *I;
326 Pool *Empty = 0;
327 for (I = Pools; I != Pools + PoolCount; ++I)
328 {
329 if (I->ItemSize == 0)
330 Empty = I;
331 if (I->ItemSize == ItemSize)
332 break;
333 }
334 // No pool is allocated, use an unallocated one
335 if (I == Pools + PoolCount)
336 {
337 // Woops, we ran out, the calling code should allocate more.
338 if (Empty == 0)
339 {
340 _error->Error("Ran out of allocation pools");
341 return 0;
342 }
343
344 I = Empty;
345 I->ItemSize = ItemSize;
346 I->Count = 0;
347 }
348
349 unsigned long Result = 0;
350 // Out of space, allocate some more
351 if (I->Count == 0)
352 {
353 const unsigned long size = 20*1024;
354 I->Count = size/ItemSize;
355 Pool* oldPools = Pools;
356 Result = RawAllocate(size,ItemSize);
357 if (Pools != oldPools)
358 I += Pools - oldPools;
359
360 // Does the allocation failed ?
361 if (Result == 0 && _error->PendingError())
362 return 0;
363 I->Start = Result;
364 }
365 else
366 Result = I->Start;
367
368 I->Count--;
369 I->Start += ItemSize;
370 return Result/ItemSize;
371 }
372 /*}}}*/
373 // DynamicMMap::WriteString - Write a string to the file /*{{{*/
374 // ---------------------------------------------------------------------
375 /* Strings are not aligned to anything */
376 unsigned long DynamicMMap::WriteString(const char *String,
377 unsigned long Len)
378 {
379 if (Len == (unsigned long)-1)
380 Len = strlen(String);
381
382 unsigned long const Result = RawAllocate(Len+1,0);
383
384 if (Result == 0 && _error->PendingError())
385 return 0;
386
387 memcpy((char *)Base + Result,String,Len);
388 ((char *)Base)[Result + Len] = 0;
389 return Result;
390 }
391 /*}}}*/
392 // DynamicMMap::Grow - Grow the mmap /*{{{*/
393 // ---------------------------------------------------------------------
394 /* This method is a wrapper around different methods to (try to) grow
395 a mmap (or our char[]-fallback). Encounterable environments:
396 1. Moveable + !Fallback + linux -> mremap with MREMAP_MAYMOVE
397 2. Moveable + !Fallback + !linux -> not possible (forbidden by constructor)
398 3. Moveable + Fallback -> realloc
399 4. !Moveable + !Fallback + linux -> mremap alone - which will fail in 99,9%
400 5. !Moveable + !Fallback + !linux -> not possible (forbidden by constructor)
401 6. !Moveable + Fallback -> not possible
402 [ While Moveable and Fallback stands for the equally named flags and
403 "linux" indicates a linux kernel instead of a freebsd kernel. ]
404 So what you can see here is, that a MMAP which want to be growable need
405 to be moveable to have a real chance but that this method will at least try
406 the nearly impossible 4 to grow it before it finally give up: Never say never. */
407 bool DynamicMMap::Grow() {
408 if (Limit != 0 && WorkSpace >= Limit)
409 return _error->Error(_("Unable to increase the size of the MMap as the "
410 "limit of %lu bytes is already reached."), Limit);
411 if (GrowFactor <= 0)
412 return _error->Error(_("Unable to increase size of the MMap as automatic growing is disabled by user."));
413
414 unsigned long const newSize = WorkSpace + GrowFactor;
415
416 if(Fd != 0) {
417 Fd->Seek(newSize - 1);
418 char C = 0;
419 Fd->Write(&C,sizeof(C));
420 }
421
422 unsigned long const poolOffset = Pools - ((Pool*) Base);
423
424 if ((Flags & Fallback) != Fallback) {
425 #if defined(_POSIX_MAPPED_FILES) && defined(__linux__)
426 #ifdef MREMAP_MAYMOVE
427
428 if ((Flags & Moveable) == Moveable)
429 Base = mremap(Base, WorkSpace, newSize, MREMAP_MAYMOVE);
430 else
431 #endif
432 Base = mremap(Base, WorkSpace, newSize, 0);
433
434 if(Base == MAP_FAILED)
435 return false;
436 #else
437 return false;
438 #endif
439 } else {
440 if ((Flags & Moveable) != Moveable)
441 return false;
442
443 Base = realloc(Base, newSize);
444 if (Base == NULL)
445 return false;
446 }
447
448 Pools =(Pool*) Base + poolOffset;
449 WorkSpace = newSize;
450 return true;
451 }
452 /*}}}*/