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