Chris Dearman | 645d031 | 2014-02-05 18:51:43 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | /* |
| 30 | * The MIPS64 getdents64() system call is only present in 3.10+ kernels. |
| 31 | * If the getdents64() system call is not available fall back to using |
| 32 | * getdents() and modify the result to be compatible with getdents64(). |
| 33 | */ |
| 34 | |
| 35 | #include <dirent.h> |
| 36 | |
| 37 | #include <errno.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <sys/stat.h> |
| 40 | #include <sys/types.h> |
| 41 | #include <unistd.h> |
| 42 | |
| 43 | |
| 44 | /* The mips_getdents type is 64bit clean */ |
| 45 | struct mips_dirent { |
| 46 | uint64_t d_ino; /* Inode number */ |
| 47 | uint64_t d_off; /* Offset to next mips_dirent */ |
| 48 | uint16_t d_reclen; /* Length of this mips_dirent */ |
| 49 | char d_name[]; /* Filename (null-terminated) */ |
| 50 | /* length is actually (d_reclen - 2 - |
| 51 | offsetof(struct mips_dirent, d_name) */ |
| 52 | // char pad; /* Zero padding byte */ |
| 53 | // char d_type; /* File type (only since Linux 2.6.4; offset is (d_reclen - 1)) */ |
| 54 | }; |
| 55 | |
| 56 | extern "C" int __getdents64(unsigned int fd, struct dirent *dirp, unsigned int count); |
| 57 | extern "C" int __getdents(unsigned int fd, struct mips_dirent *dirp, unsigned int count); |
| 58 | int getdents(unsigned int fd, struct dirent *dirp, unsigned int count) |
| 59 | { |
| 60 | int r; |
| 61 | int oerrno = errno; |
| 62 | |
| 63 | /* Use getdents64() if it is available */ |
| 64 | r = __getdents64(fd, dirp, count); |
| 65 | if (r >= 0 || errno != ENOSYS) |
| 66 | return r; |
| 67 | |
| 68 | /* Fallback to getdents() */ |
| 69 | errno = oerrno; |
| 70 | r = __getdents(fd, (struct mips_dirent *)dirp, count); |
| 71 | if (r > 0) { |
| 72 | char *p; |
| 73 | char type; |
| 74 | union dirents { |
| 75 | struct mips_dirent m; |
| 76 | struct dirent d; |
| 77 | } *u; |
| 78 | |
| 79 | p = (char *)dirp; |
| 80 | do { |
| 81 | u = (union dirents *)p; |
| 82 | |
| 83 | /* This should not happen, but just in case... */ |
| 84 | if (p + u->m.d_reclen > (char *)dirp + r) |
| 85 | break; |
| 86 | |
| 87 | /* shuffle the dirent */ |
| 88 | type = *(p + u->m.d_reclen - 1); |
| 89 | memmove(u->d.d_name, u->m.d_name, |
| 90 | u->m.d_reclen - 2 - offsetof(struct mips_dirent, d_name) + 1); |
| 91 | u->d.d_type = type; |
| 92 | |
| 93 | p += u->m.d_reclen; |
| 94 | } while (p < (char *)dirp + r); |
| 95 | } |
| 96 | return r; |
| 97 | } |