]>
Commit | Line | Data |
---|---|---|
a63c7fa1 CM |
1 | /* Copyright (C) 1993, 1995-2003, 2004, 2006, 2007, 2011 |
2 | This file is part of the GNU C Library. | |
3 | Simplified from sysdeps/unix/sysv/linux/getdents.c. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
ab84e3ff PE |
16 | License along with the GNU C Library. If not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
a63c7fa1 CM |
18 | |
19 | #include <alloca.h> | |
20 | #include <assert.h> | |
21 | #include <errno.h> | |
22 | #include <dirent.h> | |
23 | #include <stddef.h> | |
24 | #include <stdint.h> | |
25 | #include <string.h> | |
26 | #include <unistd.h> | |
27 | #include <sys/param.h> | |
28 | #include <sys/types.h> | |
29 | ||
30 | #include <sysdep.h> | |
31 | #include <sys/syscall.h> | |
32 | ||
33 | /* Pack the dirent64 struct down into 32-bit offset/inode fields, and | |
34 | ensure that no overflow occurs. */ | |
35 | ssize_t | |
36 | __getdents (int fd, char *buf, size_t nbytes) | |
37 | { | |
38 | union | |
39 | { | |
40 | struct dirent64 k; /* Kernel structure. */ | |
41 | struct dirent u; | |
42 | char b[1]; | |
43 | } *kbuf = (void *) buf, *outp, *inp; | |
44 | size_t kbytes = nbytes; | |
45 | off64_t last_offset = -1; | |
46 | ssize_t retval; | |
47 | ||
48 | const size_t size_diff = (offsetof (struct dirent64, d_name) | |
49 | - offsetof (struct dirent, d_name)); | |
50 | if (nbytes <= sizeof (struct dirent)) | |
51 | { | |
52 | kbytes = nbytes + offsetof (struct dirent64, d_name) | |
53 | - offsetof (struct dirent, d_name); | |
54 | kbuf = __alloca(kbytes); | |
55 | } | |
56 | ||
57 | retval = INLINE_SYSCALL (getdents64, 3, fd, kbuf, kbytes); | |
58 | if (retval == -1) | |
59 | return -1; | |
60 | ||
61 | /* These two pointers might alias the same memory buffer. | |
62 | Standard C requires that we always use the same type for them, | |
63 | so we must use the union type. */ | |
64 | inp = kbuf; | |
65 | outp = (void *) buf; | |
66 | ||
67 | while (&inp->b < &kbuf->b + retval) | |
68 | { | |
69 | const size_t alignment = __alignof__ (struct dirent); | |
70 | /* Since inp->k.d_reclen is already aligned for the kernel | |
71 | structure this may compute a value that is bigger | |
72 | than necessary. */ | |
73 | size_t old_reclen = inp->k.d_reclen; | |
74 | size_t new_reclen = ((old_reclen - size_diff + alignment - 1) | |
75 | & ~(alignment - 1)); | |
76 | ||
77 | /* Copy the data out of the old structure into temporary space. | |
78 | Then copy the name, which may overlap if BUF == KBUF. */ | |
79 | const uint64_t d_ino = inp->k.d_ino; | |
80 | const int64_t d_off = inp->k.d_off; | |
81 | const uint8_t d_type = inp->k.d_type; | |
82 | ||
83 | memmove (outp->u.d_name, inp->k.d_name, | |
84 | old_reclen - offsetof (struct dirent64, d_name)); | |
85 | ||
86 | /* Now we have copied the data from INP and access only OUTP. */ | |
87 | ||
88 | outp->u.d_ino = d_ino; | |
89 | outp->u.d_off = d_off; | |
90 | if ((sizeof (outp->u.d_ino) != sizeof (inp->k.d_ino) | |
91 | && outp->u.d_ino != d_ino) | |
92 | || (sizeof (outp->u.d_off) != sizeof (inp->k.d_off) | |
93 | && outp->u.d_off != d_off)) | |
94 | { | |
95 | /* Overflow. If there was at least one entry before this one, | |
96 | return them without error, otherwise signal overflow. */ | |
97 | if (last_offset != -1) | |
98 | { | |
99 | __lseek64 (fd, last_offset, SEEK_SET); | |
100 | return outp->b - buf; | |
101 | } | |
102 | __set_errno (EOVERFLOW); | |
103 | return -1; | |
104 | } | |
105 | ||
106 | last_offset = d_off; | |
107 | outp->u.d_reclen = new_reclen; | |
108 | outp->u.d_type = d_type; | |
109 | ||
110 | inp = (void *) inp + old_reclen; | |
111 | outp = (void *) outp + new_reclen; | |
112 | } | |
113 | ||
114 | return outp->b - buf; | |
115 | } |