Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/java/io/dirent_md.c
32287 views
/*1* Copyright (c) 1995, 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Posix-compatible directory access routines27*/2829#include <windows.h>30#include <direct.h> /* For _getdrive() */31#include <errno.h>32#include <assert.h>3334#include "dirent_md.h"353637/* Caller must have already run dirname through JVM_NativePath, which removes38duplicate slashes and converts all instances of '/' into '\\'. */3940DIR *41opendir(const char *dirname)42{43DIR *dirp = (DIR *)malloc(sizeof(DIR));44DWORD fattr;45char alt_dirname[4] = { 0, 0, 0, 0 };4647if (dirp == 0) {48errno = ENOMEM;49return 0;50}5152/*53* Win32 accepts "\" in its POSIX stat(), but refuses to treat it54* as a directory in FindFirstFile(). We detect this case here and55* prepend the current drive name.56*/57if (dirname[1] == '\0' && dirname[0] == '\\') {58alt_dirname[0] = _getdrive() + 'A' - 1;59alt_dirname[1] = ':';60alt_dirname[2] = '\\';61alt_dirname[3] = '\0';62dirname = alt_dirname;63}6465dirp->path = (char *)malloc(strlen(dirname) + 5);66if (dirp->path == 0) {67free(dirp);68errno = ENOMEM;69return 0;70}71strcpy(dirp->path, dirname);7273fattr = GetFileAttributes(dirp->path);74if (fattr == ((DWORD)-1)) {75free(dirp->path);76free(dirp);77errno = ENOENT;78return 0;79} else if ((fattr & FILE_ATTRIBUTE_DIRECTORY) == 0) {80free(dirp->path);81free(dirp);82errno = ENOTDIR;83return 0;84}8586/* Append "*.*", or possibly "\\*.*", to path */87if (dirp->path[1] == ':'88&& (dirp->path[2] == '\0'89|| (dirp->path[2] == '\\' && dirp->path[3] == '\0'))) {90/* No '\\' needed for cases like "Z:" or "Z:\" */91strcat(dirp->path, "*.*");92} else {93strcat(dirp->path, "\\*.*");94}9596dirp->handle = FindFirstFile(dirp->path, &dirp->find_data);97if (dirp->handle == INVALID_HANDLE_VALUE) {98if (GetLastError() != ERROR_FILE_NOT_FOUND) {99free(dirp->path);100free(dirp);101errno = EACCES;102return 0;103}104}105return dirp;106}107108struct dirent *109readdir(DIR *dirp)110{111if (dirp->handle == INVALID_HANDLE_VALUE) {112return 0;113}114115strcpy(dirp->dirent.d_name, dirp->find_data.cFileName);116117if (!FindNextFile(dirp->handle, &dirp->find_data)) {118if (GetLastError() == ERROR_INVALID_HANDLE) {119errno = EBADF;120return 0;121}122FindClose(dirp->handle);123dirp->handle = INVALID_HANDLE_VALUE;124}125126return &dirp->dirent;127}128129int130closedir(DIR *dirp)131{132if (dirp->handle != INVALID_HANDLE_VALUE) {133if (!FindClose(dirp->handle)) {134errno = EBADF;135return -1;136}137dirp->handle = INVALID_HANDLE_VALUE;138}139free(dirp->path);140free(dirp);141return 0;142}143144void145rewinddir(DIR *dirp)146{147if (dirp->handle != INVALID_HANDLE_VALUE) {148FindClose(dirp->handle);149}150dirp->handle = FindFirstFile(dirp->path, &dirp->find_data);151}152153154