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