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