]> mj.ucw.cz Git - libucw.git/blob - lib/partmap.c
756fdb635aeb826e1b40121af6efe27bfb6fd37a
[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
21 struct partmap {
22   int fd;
23   sh_off_t file_size;
24   sh_off_t start_off, end_off;
25   byte *start_map;
26   int writeable;
27 };
28
29 #ifdef TEST
30 #define PARTMAP_WINDOW 4096
31 #else
32 #define PARTMAP_WINDOW 16777216
33 #endif
34
35 static uns page_size;
36
37 struct partmap *
38 partmap_open(byte *name, int writeable)
39 {
40   if (!page_size)
41     page_size = getpagesize();
42
43   struct partmap *p = xmalloc_zero(sizeof(struct partmap));
44
45   p->fd = sh_open(name, writeable ? O_RDWR : O_RDONLY);
46   if (p->fd < 0)
47     die("open(%s): %m", name);
48   if ((p->file_size = sh_seek(p->fd, 0, SEEK_END)) < 0)
49     die("lseek(%s): %m", name);
50   p->writeable = writeable;
51   return p;
52 }
53
54 sh_off_t
55 partmap_size(struct partmap *p)
56 {
57   return p->file_size;
58 }
59
60 void
61 partmap_close(struct partmap *p)
62 {
63   if (p->start_map)
64     munmap(p->start_map, p->end_off - p->start_off);
65   close(p->fd);
66   xfree(p);
67 }
68
69 void *
70 partmap_map(struct partmap *p, sh_off_t start, uns size)
71 {
72   if (!p->start_map || start < p->start_off || start+size > p->end_off)
73     {
74       if (p->start_map)
75         munmap(p->start_map, p->end_off - p->start_off);
76       uns win = PARTMAP_WINDOW;
77       ASSERT(win >= size);
78       sh_off_t end = start + size;
79       start = start/page_size * page_size;
80       if (start+win > p->file_size)
81         win = p->file_size - start;
82       if (start+win < end)
83         die("cannot mmap, the window is too small");
84       p->start_map = sh_mmap(NULL, win, p->writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, p->fd, start);
85       if (p->start_map == MAP_FAILED)
86         die("mmap failed at position %Ld: %m", (long long)start);
87       p->start_off = start;
88       p->end_off = start+win;
89     }
90   return p->start_map + (start - p->start_off);
91 }
92
93 #ifdef TEST
94 int main(int argc, char **argv)
95 {
96   struct partmap *p = partmap_open(argv[1], 0);
97   uns l = partmap_size(p);
98   uns i;
99   for (i=0; i<l; i++)
100     putchar(*(char *)partmap_map(p, i, 1));
101   partmap_close(p);
102   return 0;
103 }
104 #endif