]> mj.ucw.cz Git - libucw.git/blob - ucw/partmap.c
bgets_mp(): Added a non-intuitive warning to documentation.
[libucw.git] / ucw / partmap.c
1 /*
2  *      UCW Library -- Mapping of File Parts
3  *
4  *      (c) 2003--2006 Martin Mares <mj@ucw.cz>
5  *      (c) 2003--2009 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 <ucw/lib.h>
12 #include <ucw/io.h>
13 #include <ucw/partmap.h>
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include <sys/mman.h>
21
22 #ifdef CONFIG_UCW_PARTMAP_IS_MMAP
23 #define PARTMAP_WINDOW ~(size_t)0
24 #else
25 #ifdef TEST
26 #define PARTMAP_WINDOW 4096
27 #else
28 #define PARTMAP_WINDOW 16777216
29 #endif
30 #endif
31
32 struct partmap *
33 partmap_open(char *name, int writeable)
34 {
35   struct partmap *p = xmalloc_zero(sizeof(struct partmap));
36
37   p->fd = ucw_open(name, writeable ? O_RDWR : O_RDONLY);
38   if (p->fd < 0)
39     die("open(%s): %m", name);
40   if ((p->file_size = ucw_seek(p->fd, 0, SEEK_END)) < 0)
41     die("lseek(%s): %m", name);
42   p->writeable = writeable;
43 #ifdef CONFIG_UCW_PARTMAP_IS_MMAP
44   partmap_load(p, 0, p->file_size);
45 #endif
46   return p;
47 }
48
49 ucw_off_t
50 partmap_size(struct partmap *p)
51 {
52   return p->file_size;
53 }
54
55 void
56 partmap_close(struct partmap *p)
57 {
58   if (p->start_map)
59     munmap(p->start_map, p->end_off - p->start_off);
60   close(p->fd);
61   xfree(p);
62 }
63
64 void
65 partmap_load(struct partmap *p, ucw_off_t start, uint size)
66 {
67   if (p->start_map)
68     munmap(p->start_map, p->end_off - p->start_off);
69   ucw_off_t end = start + size;
70   ucw_off_t win_start = start/CPU_PAGE_SIZE * CPU_PAGE_SIZE;
71   size_t win_len = PARTMAP_WINDOW;
72   if (win_len > (size_t) (p->file_size - win_start))
73     win_len = ALIGN_TO(p->file_size - win_start, CPU_PAGE_SIZE);
74   if ((ucw_off_t) (win_start+win_len) < end)
75     die("partmap_map: Window is too small for mapping %d bytes", size);
76   if (win_len)
77     {
78       p->start_map = ucw_mmap(NULL, win_len, p->writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, p->fd, win_start);
79       if (p->start_map == MAP_FAILED)
80         die("mmap failed at position %lld: %m", (long long)win_start);
81     }
82   else
83     p->start_map = NULL;
84   p->start_off = win_start;
85   p->end_off = win_start+win_len;
86   madvise(p->start_map, win_len, MADV_SEQUENTIAL);
87 }
88
89 #ifdef TEST
90 int main(int argc, char **argv)
91 {
92   struct partmap *p = partmap_open(argv[1], 0);
93   uint l = partmap_size(p);
94   uint 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