]> mj.ucw.cz Git - libucw.git/blob - lib/partmap.c
One more deadly testcase.
[libucw.git] / lib / partmap.c
1 /*
2  *      Sherlock Library -- Mapping of File Parts
3  *
4  *      (c) 2003 Martin Mares <mj@ucw.cz>
5  *      (c) 2003 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include "lib/lib.h"
12 #include "lib/lfs.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <sys/stat.h>
19 #include <sys/mman.h>
20 #include <sys/user.h>
21
22 struct partmap {
23   int fd;
24   sh_off_t file_size;
25   sh_off_t start_off, end_off;
26   byte *start_map;
27   int writeable;
28 };
29
30 #ifdef TEST
31 #define PARTMAP_WINDOW 4096
32 #else
33 #define PARTMAP_WINDOW 16777216
34 #endif
35
36 struct partmap *
37 partmap_open(byte *name, int writeable)
38 {
39   struct partmap *p = xmalloc_zero(sizeof(struct partmap));
40
41   p->fd = sh_open(name, writeable ? O_RDWR : O_RDONLY);
42   if (p->fd < 0)
43     die("open(%s): %m", name);
44   if ((p->file_size = sh_seek(p->fd, 0, SEEK_END)) < 0)
45     die("lseek(%s): %m", name);
46   p->writeable = writeable;
47   return p;
48 }
49
50 sh_off_t
51 partmap_size(struct partmap *p)
52 {
53   return p->file_size;
54 }
55
56 void
57 partmap_close(struct partmap *p)
58 {
59   if (p->start_map)
60     munmap(p->start_map, p->end_off - p->start_off);
61   close(p->fd);
62   xfree(p);
63 }
64
65 void *
66 partmap_map(struct partmap *p, sh_off_t start, uns size)
67 {
68   if (!p->start_map || start < p->start_off || start+size > p->end_off)
69     {
70       if (p->start_map)
71         munmap(p->start_map, p->end_off - p->start_off);
72       uns win = PARTMAP_WINDOW;
73       ASSERT(win >= size);
74       sh_off_t end = start + size;
75       start = start/PAGE_SIZE * PAGE_SIZE;
76       if (start+win > p->file_size)
77         win = p->file_size - start;
78       if (start+win < end)
79         die("cannot mmap, the window is too small");
80       p->start_map = sh_mmap(NULL, win, p->writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, p->fd, start);
81       if (p->start_map == MAP_FAILED)
82         die("mmap failed at position %Ld: %m", (long long)start);
83       p->start_off = start;
84       p->end_off = start+win;
85     }
86   return p->start_map + (start - p->start_off);
87 }
88
89 #ifdef TEST
90 int main(int argc, char **argv)
91 {
92   struct partmap *p = partmap_open(argv[1], 0);
93   uns l = partmap_size(p);
94   uns i;
95   for (i=0; i<l; i++)
96     putchar(*(char *)partmap_map(p, i, 1));
97   partmap_close(p);
98   return 0;
99 }
100 #endif