| 1 | /* | 
|---|
| 2 | Unix SMB/CIFS implementation. | 
|---|
| 3 |  | 
|---|
| 4 | Copyright (C) Andrew Tridgell 2005 | 
|---|
| 5 | Updated for Samba3 64-bit cleanliness (C) Jeremy Allison 2006 | 
|---|
| 6 |  | 
|---|
| 7 | This program is free software; you can redistribute it and/or modify | 
|---|
| 8 | it under the terms of the GNU General Public License as published by | 
|---|
| 9 | the Free Software Foundation; either version 2 of the License, or | 
|---|
| 10 | (at your option) any later version. | 
|---|
| 11 |  | 
|---|
| 12 | This program is distributed in the hope that it will be useful, | 
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 15 | GNU General Public License for more details. | 
|---|
| 16 |  | 
|---|
| 17 | You should have received a copy of the GNU General Public License | 
|---|
| 18 | along with this program; if not, write to the Free Software | 
|---|
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 20 | */ | 
|---|
| 21 | /* | 
|---|
| 22 | a replacement for opendir/readdir/telldir/seekdir/closedir for BSD systems | 
|---|
| 23 |  | 
|---|
| 24 | This is needed because the existing directory handling in FreeBSD | 
|---|
| 25 | and OpenBSD (and possibly NetBSD) doesn't correctly handle unlink() | 
|---|
| 26 | on files in a directory where telldir() has been used. On a block | 
|---|
| 27 | boundary it will occasionally miss a file when seekdir() is used to | 
|---|
| 28 | return to a position previously recorded with telldir(). | 
|---|
| 29 |  | 
|---|
| 30 | This also fixes a severe performance and memory usage problem with | 
|---|
| 31 | telldir() on BSD systems. Each call to telldir() in BSD adds an | 
|---|
| 32 | entry to a linked list, and those entries are cleaned up on | 
|---|
| 33 | closedir(). This means with a large directory closedir() can take an | 
|---|
| 34 | arbitrary amount of time, causing network timeouts as millions of | 
|---|
| 35 | telldir() entries are freed | 
|---|
| 36 |  | 
|---|
| 37 | Note! This replacement code is not portable. It relies on getdents() | 
|---|
| 38 | always leaving the file descriptor at a seek offset that is a | 
|---|
| 39 | multiple of DIR_BUF_SIZE. If the code detects that this doesn't | 
|---|
| 40 | happen then it will abort(). It also does not handle directories | 
|---|
| 41 | with offsets larger than can be stored in a long, | 
|---|
| 42 |  | 
|---|
| 43 | This code is available under other free software licenses as | 
|---|
| 44 | well. Contact the author. | 
|---|
| 45 | */ | 
|---|
| 46 |  | 
|---|
| 47 | #include <include/includes.h> | 
|---|
| 48 |  | 
|---|
| 49 | void replace_readdir_dummy(void); | 
|---|
| 50 | void replace_readdir_dummy(void) {} | 
|---|
| 51 |  | 
|---|
| 52 | #if defined(REPLACE_READDIR) | 
|---|
| 53 |  | 
|---|
| 54 | #if defined(PARANOID_MALLOC_CHECKER) | 
|---|
| 55 | #ifdef malloc | 
|---|
| 56 | #undef malloc | 
|---|
| 57 | #endif | 
|---|
| 58 | #endif | 
|---|
| 59 |  | 
|---|
| 60 | #define DIR_BUF_BITS 9 | 
|---|
| 61 | #define DIR_BUF_SIZE (1<<DIR_BUF_BITS) | 
|---|
| 62 |  | 
|---|
| 63 | struct dir_buf { | 
|---|
| 64 | int fd; | 
|---|
| 65 | int nbytes, ofs; | 
|---|
| 66 | SMB_OFF_T seekpos; | 
|---|
| 67 | char buf[DIR_BUF_SIZE]; | 
|---|
| 68 | }; | 
|---|
| 69 |  | 
|---|
| 70 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64) | 
|---|
| 71 | SMB_STRUCT_DIR *opendir64(const char *dname) | 
|---|
| 72 | #else | 
|---|
| 73 | SMB_STRUCT_DIR *opendir(const char *dname) | 
|---|
| 74 | #endif | 
|---|
| 75 | { | 
|---|
| 76 | struct dir_buf *d; | 
|---|
| 77 | d = malloc(sizeof(*d)); | 
|---|
| 78 | if (d == NULL) { | 
|---|
| 79 | errno = ENOMEM; | 
|---|
| 80 | return NULL; | 
|---|
| 81 | } | 
|---|
| 82 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64) | 
|---|
| 83 | d->fd = open64(dname, O_RDONLY); | 
|---|
| 84 | #else | 
|---|
| 85 | d->fd = open(dname, O_RDONLY); | 
|---|
| 86 | #endif | 
|---|
| 87 |  | 
|---|
| 88 | if (d->fd == -1) { | 
|---|
| 89 | free(d); | 
|---|
| 90 | return NULL; | 
|---|
| 91 | } | 
|---|
| 92 | d->ofs = 0; | 
|---|
| 93 | d->seekpos = 0; | 
|---|
| 94 | d->nbytes = 0; | 
|---|
| 95 | return (SMB_STRUCT_DIR *)d; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64) | 
|---|
| 99 | SMB_STRUCT_DIRENT *readdir64(SMB_STRUCT_DIR *dir) | 
|---|
| 100 | #else | 
|---|
| 101 | SMB_STRUCT_DIRENT *readdir(SMB_STRUCT_DIR *dir) | 
|---|
| 102 | #endif | 
|---|
| 103 | { | 
|---|
| 104 | struct dir_buf *d = (struct dir_buf *)dir; | 
|---|
| 105 | SMB_STRUCT_DIRENT *de; | 
|---|
| 106 |  | 
|---|
| 107 | if (d->ofs >= d->nbytes) { | 
|---|
| 108 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_LSEEK64) | 
|---|
| 109 | d->seekpos = lseek64(d->fd, 0, SEEK_CUR); | 
|---|
| 110 | #else | 
|---|
| 111 | d->seekpos = lseek(d->fd, 0, SEEK_CUR); | 
|---|
| 112 | #endif | 
|---|
| 113 |  | 
|---|
| 114 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_GETDENTS64) | 
|---|
| 115 | d->nbytes = getdents64(d->fd, d->buf, DIR_BUF_SIZE); | 
|---|
| 116 | #else | 
|---|
| 117 | d->nbytes = getdents(d->fd, d->buf, DIR_BUF_SIZE); | 
|---|
| 118 | #endif | 
|---|
| 119 | d->ofs = 0; | 
|---|
| 120 | } | 
|---|
| 121 | if (d->ofs >= d->nbytes) { | 
|---|
| 122 | return NULL; | 
|---|
| 123 | } | 
|---|
| 124 | de = (SMB_STRUCT_DIRENT *)&d->buf[d->ofs]; | 
|---|
| 125 | d->ofs += de->d_reclen; | 
|---|
| 126 | return de; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64) | 
|---|
| 130 | long telldir64(SMB_STRUCT_DIR *dir) | 
|---|
| 131 | #else | 
|---|
| 132 | long telldir(SMB_STRUCT_DIR *dir) | 
|---|
| 133 | #endif | 
|---|
| 134 | { | 
|---|
| 135 | struct dir_buf *d = (struct dir_buf *)dir; | 
|---|
| 136 | if (d->ofs >= d->nbytes) { | 
|---|
| 137 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_LSEEK64) | 
|---|
| 138 | d->seekpos = lseek64(d->fd, 0, SEEK_CUR); | 
|---|
| 139 | #else | 
|---|
| 140 | d->seekpos = lseek(d->fd, 0, SEEK_CUR); | 
|---|
| 141 | #endif | 
|---|
| 142 | d->ofs = 0; | 
|---|
| 143 | d->nbytes = 0; | 
|---|
| 144 | } | 
|---|
| 145 | /* this relies on seekpos always being a multiple of | 
|---|
| 146 | DIR_BUF_SIZE. Is that always true on BSD systems? */ | 
|---|
| 147 | if (d->seekpos & (DIR_BUF_SIZE-1)) { | 
|---|
| 148 | abort(); | 
|---|
| 149 | } | 
|---|
| 150 | return d->seekpos + d->ofs; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64) | 
|---|
| 154 | void seekdir64(SMB_STRUCT_DIR *dir, long ofs) | 
|---|
| 155 | #else | 
|---|
| 156 | void seekdir(SMB_STRUCT_DIR *dir, long ofs) | 
|---|
| 157 | #endif | 
|---|
| 158 | { | 
|---|
| 159 | struct dir_buf *d = (struct dir_buf *)dir; | 
|---|
| 160 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_LSEEK64) | 
|---|
| 161 | d->seekpos = lseek64(d->fd, ofs & ~(DIR_BUF_SIZE-1), SEEK_SET); | 
|---|
| 162 | #else | 
|---|
| 163 | d->seekpos = lseek(d->fd, ofs & ~(DIR_BUF_SIZE-1), SEEK_SET); | 
|---|
| 164 | #endif | 
|---|
| 165 |  | 
|---|
| 166 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_GETDENTS64) | 
|---|
| 167 | d->nbytes = getdents64(d->fd, d->buf, DIR_BUF_SIZE); | 
|---|
| 168 | #else | 
|---|
| 169 | d->nbytes = getdents(d->fd, d->buf, DIR_BUF_SIZE); | 
|---|
| 170 | #endif | 
|---|
| 171 |  | 
|---|
| 172 | d->ofs = 0; | 
|---|
| 173 | while (d->ofs < (ofs & (DIR_BUF_SIZE-1))) { | 
|---|
| 174 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64) | 
|---|
| 175 | if (readdir64(dir) == NULL) break; | 
|---|
| 176 | #else | 
|---|
| 177 | if (readdir(dir) == NULL) break; | 
|---|
| 178 | #endif | 
|---|
| 179 | } | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64) | 
|---|
| 183 | void rewinddir64(SMB_STRUCT_DIR *dir) | 
|---|
| 184 | #else | 
|---|
| 185 | void rewinddir(SMB_STRUCT_DIR *dir) | 
|---|
| 186 | #endif | 
|---|
| 187 | { | 
|---|
| 188 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64) | 
|---|
| 189 | seekdir64(dir, 0); | 
|---|
| 190 | #else | 
|---|
| 191 | seekdir(dir, 0); | 
|---|
| 192 | #endif | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64) | 
|---|
| 196 | int closedir64(SMB_STRUCT_DIR *dir) | 
|---|
| 197 | #else | 
|---|
| 198 | int closedir(SMB_STRUCT_DIR *dir) | 
|---|
| 199 | #endif | 
|---|
| 200 | { | 
|---|
| 201 | struct dir_buf *d = (struct dir_buf *)dir; | 
|---|
| 202 | int r = close(d->fd); | 
|---|
| 203 | if (r != 0) { | 
|---|
| 204 | return r; | 
|---|
| 205 | } | 
|---|
| 206 | free(d); | 
|---|
| 207 | return 0; | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | #ifndef dirfd | 
|---|
| 211 | /* darn, this is a macro on some systems. */ | 
|---|
| 212 | int dirfd(SMB_STRUCT_DIR *dir) | 
|---|
| 213 | { | 
|---|
| 214 | struct dir_buf *d = (struct dir_buf *)dir; | 
|---|
| 215 | return d->fd; | 
|---|
| 216 | } | 
|---|
| 217 | #endif | 
|---|
| 218 | #endif /* REPLACE_READDIR */ | 
|---|