]> mj.ucw.cz Git - libucw.git/blob - lib/lfs.h
b9fb487291151febf4d81c53fe6b112d14567894
[libucw.git] / lib / lfs.h
1 /*
2  *      Sherlock Library -- Large File Support
3  *
4  *      (c) 1999--2001 Martin Mares <mj@ucw.cz>
5  */
6
7 #ifndef _SHERLOCK_LFS_H
8 #define _SHERLOCK_LFS_H
9
10 #include <fcntl.h>
11 #include <unistd.h>
12
13 #ifdef SHERLOCK_CONFIG_LFS
14
15 #ifdef SHERLOCK_CONFIG_LFS_LIBC
16
17 /*
18  *  Unfortunately, we need to configure this manually since
19  *  out-of-the-box glibc 2.1 offers the 64-bit calls, but
20  *  converts them to 32-bit syscalls. Damn it!
21  */
22
23 #define sh_open open64
24 #define sh_seek lseek64
25 #define sh_pread pread64
26 #define sh_pwrite pwrite64
27 #define sh_ftruncate ftruncate64
28 #define SHERLOCK_HAVE_PREAD
29
30 #else
31
32 /*
33  *  Talk directly with the kernel. The implementations of LFS in Linux 2.2
34  *  and 2.4 differ, but fortunately for us only in things like stat64 which
35  *  we don't need to use.
36  */
37
38 #ifndef O_LARGEFILE
39 #if defined(__linux__) && defined(__i386__)
40 #define O_LARGEFILE 0100000
41 #else
42 #error O_LARGEFILE unknown
43 #endif
44 #endif
45
46 static inline int
47 sh_open(char *name, int flags, int mode)
48 {
49   return open(name, flags | O_LARGEFILE, mode);
50 }
51
52 #if 0
53
54 /* A "do it yourself" solution */
55
56 #include <asm/unistd.h>
57 #include <errno.h>
58
59 _syscall5(int, _llseek, int, fd, int, hi, int, lo, loff_t *, result, int, whence);
60
61 static inline loff_t sh_seek(int fd, sh_off_t pos, int whence)
62 {
63   loff_t result;
64   int err;
65
66   err = _llseek(fd, pos >> 32, pos, &result, whence);
67   return (err < 0) ? err : result;
68 }
69 #else
70
71 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
72 /* glibc 2.1 or newer -> has lseek64 */
73 #define sh_seek(f,o,w) lseek64(f,o,w)
74 #else
75 /* Touching hidden places in glibc */
76 extern loff_t llseek(int fd, loff_t pos, int whence);
77 #define sh_seek(f,o,w) llseek(f,o,w)
78 #endif
79
80 #endif
81
82 #endif  /* !SHERLOCK_CONFIG_LFS_LIBC */
83
84 #else   /* !SHERLOCK_CONFIG_LFS */
85
86 #define sh_open open
87 #define sh_seek(f,o,w) lseek(f,o,w)
88 #define sh_ftruncate(f,o) ftruncate(f,o)
89
90 #endif  /* !SHERLOCK_CONFIG_LFS */
91
92 /*
93  *  We'd like to use pread/pwrite for our file accesses, but unfortunately it
94  *  isn't simple at all since all libc's until glibc 2.1 don't define it.
95  */
96
97 #ifndef SHERLOCK_HAVE_PREAD
98
99 #ifdef __linux__
100 #define SHERLOCK_HAVE_PREAD
101 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
102 /* glibc 2.1 or newer -> pread/pwrite supported automatically */
103 #ifdef SHERLOCK_CONFIG_LFS
104 /* but have to use the 64-bit versions explicitly */
105 #define sh_pread pread64
106 #define sh_pwrite pwrite64
107 #else
108 #define sh_pread pread
109 #define sh_pwrite pwrite
110 #endif
111 #elif defined(i386) && defined(__GLIBC__)
112 /* glibc 2.0 on i386 -> call syscalls directly */
113 #include <asm/unistd.h>
114 #include <syscall-list.h>
115 #include <sys/types.h>
116 #include <unistd.h>
117 #ifndef SYS_pread
118 #define SYS_pread 180
119 #endif
120 static int sh_pread(unsigned int fd, void *buf, size_t size, loff_t where)
121 { return syscall(SYS_pread, fd, buf, size, where); }
122 #ifndef SYS_pwrite
123 #define SYS_pwrite 181
124 #endif
125 static int sh_pwrite(unsigned int fd, void *buf, size_t size, loff_t where)
126 { return syscall(SYS_pwrite, fd, buf, size, where); }
127 #elif defined(i386)
128 /* old libc on i386 -> call syscalls directly the old way */
129 #include <asm/unistd.h>
130 #include <errno.h>
131 static _syscall4(int, pread, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
132 static _syscall4(int, pwrite, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
133 #define sh_pread pread
134 #define sh_pwrite pwrite
135 #else
136 #undef SHERLOCK_HAVE_PREAD
137 #endif
138 #endif  /* __linux__ */
139
140 #endif  /* !SHERLOCK_HAVE_PREAD */
141
142 #endif  /* !_SHERLOCK_LFS_H */