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