Changeset 2424 for trunk/src/kash/shfile.c
- Timestamp:
- Oct 18, 2010, 10:52:03 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/shfile.c
r2423 r2424 65 65 # define SHFILE_IN_USE 66 66 #endif 67 # define SHFILE_IN_USE 67 68 /** The max file table size. */ 68 69 #define SHFILE_MAX 1024 … … 176 177 177 178 /** 179 * Grows the descriptor table, making sure that it can hold @a fdMin, 180 * 181 * @returns The max(fdMin, fdFirstNew) on success, -1 on failure. 182 * @param pfdtab The table to grow. 183 * @param fdMin Grow to include this index. 184 */ 185 static int shfile_grow_tab_locked(shfdtab *pfdtab, int fdMin) 186 { 187 /* 188 * Grow the descriptor table. 189 */ 190 int fdRet = -1; 191 shfile *new_tab; 192 int new_size = pfdtab->size + SHFILE_GROW; 193 while (new_size < fdMin) 194 new_size += SHFILE_GROW; 195 new_tab = sh_realloc(shthread_get_shell(), pfdtab->tab, new_size * sizeof(shfile)); 196 if (new_tab) 197 { 198 int i; 199 for (i = pfdtab->size; i < new_size; i++) 200 { 201 new_tab[i].fd = -1; 202 new_tab[i].oflags = 0; 203 new_tab[i].shflags = 0; 204 new_tab[i].native = -1; 205 } 206 207 fdRet = pfdtab->size; 208 if (fdRet < fdMin) 209 fdRet = fdMin; 210 211 pfdtab->tab = new_tab; 212 pfdtab->size = new_size; 213 } 214 215 return fdRet; 216 } 217 218 /** 178 219 * Inserts the file into the descriptor table. 179 220 * … … 219 260 */ 220 261 fd = -1; 221 for (i = 0; (unsigned)i < pfdtab->size; i++) 222 if ( i >= fdMin 223 && pfdtab->tab[i].fd == -1) 262 for (i = fdMin >= 0 ? fdMin : 0; (unsigned)i < pfdtab->size; i++) 263 if (pfdtab->tab[i].fd == -1) 224 264 { 225 265 fd = i; … … 227 267 } 228 268 if (fd == -1) 229 { 230 /* 231 * Grow the descriptor table. 232 */ 233 shfile *new_tab; 234 int new_size = pfdtab->size + SHFILE_GROW; 235 while (new_size < fdMin) 236 new_size += SHFILE_GROW; 237 new_tab = sh_realloc(shthread_get_shell(), pfdtab->tab, new_size * sizeof(shfile)); 238 if (new_tab) 239 { 240 for (i = pfdtab->size; i < new_size; i++) 241 { 242 new_tab[i].fd = -1; 243 new_tab[i].oflags = 0; 244 new_tab[i].shflags = 0; 245 new_tab[i].native = -1; 246 } 247 248 fd = pfdtab->size; 249 if (fd < fdMin) 250 fd = fdMin; 251 252 pfdtab->tab = new_tab; 253 pfdtab->size = new_size; 254 } 255 } 269 fd = shfile_grow_tab_locked(pfdtab, fdMin); 256 270 257 271 /* … … 1134 1148 1135 1149 /** 1150 * Move the file descriptor, closing any existing descriptor at @a fdto. 1151 * 1152 * @returns fdto on success, -1 and errno on failure. 1153 * @param pfdtab The file descriptor table. 1154 * @param fdfrom The descriptor to move. 1155 * @param fdto Where to move it. 1156 */ 1157 int shfile_movefd(shfdtab *pfdtab, int fdfrom, int fdto) 1158 { 1159 #ifdef SHFILE_IN_USE 1160 int rc; 1161 shmtxtmp tmp; 1162 shfile *file = shfile_get(pfdtab, fdfrom, &tmp); 1163 if (file) 1164 { 1165 /* prepare the new entry */ 1166 if (fdto >= (int)pfdtab->size) 1167 shfile_grow_tab_locked(pfdtab, fdto); 1168 if (fdto < (int)pfdtab->size) 1169 { 1170 if (pfdtab->tab[fdto].fd != -1) 1171 shfile_native_close(pfdtab->tab[fdto].native, pfdtab->tab[fdto].oflags); 1172 1173 /* setup the target. */ 1174 pfdtab->tab[fdto].fd = fdto; 1175 pfdtab->tab[fdto].oflags = file->oflags; 1176 pfdtab->tab[fdto].shflags = file->shflags; 1177 pfdtab->tab[fdto].native = file->native; 1178 1179 /* close the source. */ 1180 file->fd = -1; 1181 file->oflags = 0; 1182 file->shflags = 0; 1183 file->native = -1; 1184 1185 rc = fdto; 1186 } 1187 else 1188 { 1189 errno = EMFILE; 1190 rc = -1; 1191 } 1192 1193 shfile_put(pfdtab, file, &tmp); 1194 } 1195 else 1196 rc = -1; 1197 return rc; 1198 1199 #else 1200 return dup2(fdfrom, fdto); 1201 #endif 1202 } 1203 1204 /** 1205 * Move the file descriptor to somewhere at @a fdMin or above. 1206 * 1207 * @returns the new file descriptor success, -1 and errno on failure. 1208 * @param pfdtab The file descriptor table. 1209 * @param fdfrom The descriptor to move. 1210 * @param fdMin The minimum descriptor. 1211 */ 1212 int shfile_movefd_above(shfdtab *pfdtab, int fdfrom, int fdMin) 1213 { 1214 #ifdef SHFILE_IN_USE 1215 int fdto; 1216 shmtxtmp tmp; 1217 shfile *file = shfile_get(pfdtab, fdfrom, &tmp); 1218 if (file) 1219 { 1220 /* find a new place */ 1221 int i; 1222 fdto = -1; 1223 for (i = fdMin; (unsigned)i < pfdtab->size; i++) 1224 if (pfdtab->tab[i].fd == -1) 1225 { 1226 fdto = i; 1227 break; 1228 } 1229 if (fdto == -1) 1230 fdto = shfile_grow_tab_locked(pfdtab, fdMin); 1231 if (fdto != -1) 1232 { 1233 /* setup the target. */ 1234 pfdtab->tab[fdto].fd = fdto; 1235 pfdtab->tab[fdto].oflags = file->oflags; 1236 pfdtab->tab[fdto].shflags = file->shflags; 1237 pfdtab->tab[fdto].native = file->native; 1238 1239 /* close the source. */ 1240 file->fd = -1; 1241 file->oflags = 0; 1242 file->shflags = 0; 1243 file->native = -1; 1244 } 1245 else 1246 { 1247 errno = EMFILE; 1248 fdto = -1; 1249 } 1250 1251 shfile_put(pfdtab, file, &tmp); 1252 } 1253 else 1254 fdto = -1; 1255 return fdto; 1256 1257 #else 1258 int fdnew = fcntl(fdfrom, F_DUPFD, fdMin); 1259 if (fdnew >= 0) 1260 close(fdfrom); 1261 return fdnew; 1262 #endif 1263 } 1264 1265 /** 1136 1266 * close(). 1137 1267 */ … … 1158 1288 1159 1289 #else 1160 fsync(fd);1161 {1162 struct stat s;1163 fstat(fd, &s);1164 TRACE2((NULL, "shfile_close(%d) - %lu bytes\n", fd, (long)s.st_size));1165 }1166 1290 rc = close(fd); 1167 1291 #endif … … 1430 1554 int shfile_chdir(shfdtab *pfdtab, const char *path) 1431 1555 { 1432 shinstance *psh = shthread_get_shell();1433 1556 int rc; 1434 1557 #ifdef SHFILE_IN_USE 1558 shinstance *psh = shthread_get_shell(); 1435 1559 char abspath[SHFILE_MAX_PATH]; 1436 1560 … … 1459 1583 #endif 1460 1584 1461 TRACE2(( psh, "shfile_chdir(,%s) -> %d [%d]\n", path, rc, errno));1585 TRACE2((NULL, "shfile_chdir(,%s) -> %d [%d]\n", path, rc, errno)); 1462 1586 return rc; 1463 1587 }
Note:
See TracChangeset
for help on using the changeset viewer.