1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
4377 os::free(pathbuf);
4378 errno = err;
4379 return -1;
4380 }
4381 ret = ::_wopen(wpath, oflag | O_BINARY | O_NOINHERIT, mode);
4382 if (ret == -1) {
4383 errno = ::GetLastError();
4384 }
4385 destroy_unc_path(wpath);
4386 }
4387 os::free(pathbuf);
4388 return ret;
4389 }
4390
4391 FILE* os::open(int fd, const char* mode) {
4392 return ::_fdopen(fd, mode);
4393 }
4394
4395 // Is a (classpath) directory empty?
4396 bool os::dir_is_empty(const char* path) {
4397 WIN32_FIND_DATA fd;
4398 HANDLE f = FindFirstFile(path, &fd);
4399 if (f == INVALID_HANDLE_VALUE) {
4400 return true;
4401 }
4402 FindClose(f);
4403 return false;
4404 }
4405
4406 // create binary file, rewriting existing file if required
4407 int os::create_binary_file(const char* path, bool rewrite_existing) {
4408 int oflags = _O_CREAT | _O_WRONLY | _O_BINARY;
4409 if (!rewrite_existing) {
4410 oflags |= _O_EXCL;
4411 }
4412 return ::open(path, oflags, _S_IREAD | _S_IWRITE);
4413 }
4414
4415 // return current position of file pointer
4416 jlong os::current_file_offset(int fd) {
4417 return (jlong)::_lseeki64(fd, (__int64)0L, SEEK_CUR);
4418 }
4419
4420 // move file pointer to the specified offset
4421 jlong os::seek_to_file_offset(int fd, jlong offset) {
4422 return (jlong)::_lseeki64(fd, (__int64)offset, SEEK_SET);
4423 }
|
1 /*
2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
4377 os::free(pathbuf);
4378 errno = err;
4379 return -1;
4380 }
4381 ret = ::_wopen(wpath, oflag | O_BINARY | O_NOINHERIT, mode);
4382 if (ret == -1) {
4383 errno = ::GetLastError();
4384 }
4385 destroy_unc_path(wpath);
4386 }
4387 os::free(pathbuf);
4388 return ret;
4389 }
4390
4391 FILE* os::open(int fd, const char* mode) {
4392 return ::_fdopen(fd, mode);
4393 }
4394
4395 // Is a (classpath) directory empty?
4396 bool os::dir_is_empty(const char* path) {
4397 bool is_empty = false;
4398 char* search_path = (char*)os::malloc(strlen(path) + 3, mtInternal);
4399 if (search_path == NULL) {
4400 errno = ENOMEM;
4401 return false;
4402 }
4403 strcpy(search_path, path);
4404 // Append "*", or possibly "\\*", to path
4405 if (path[1] == ':' &&
4406 (path[2] == '\0' ||
4407 (path[2] == '\\' && path[3] == '\0'))) {
4408 // No '\\' needed for cases like "Z:" or "Z:\"
4409 strcat(search_path, "*");
4410 }
4411 else {
4412 strcat(search_path, "\\*");
4413 }
4414 errno_t err = ERROR_SUCCESS;
4415 wchar_t* wpath = create_unc_path(search_path, err);
4416 if (err != ERROR_SUCCESS) {
4417 if (wpath != NULL) {
4418 destroy_unc_path(wpath);
4419 }
4420 os::free(search_path);
4421 errno = err;
4422 return false;
4423 }
4424 WIN32_FIND_DATAW fd;
4425 HANDLE f = ::FindFirstFileW(wpath, &fd);
4426 destroy_unc_path(wpath);
4427 if (f == INVALID_HANDLE_VALUE) {
4428 is_empty = true;
4429 } else {
4430 is_empty = true;
4431 while (is_empty && ::FindNextFileW(f, &fd)) {
4432 // An empty directory contains only the current directory file
4433 // and the previous directory file.
4434 if ((wcscmp(fd.cFileName, L".") != 0) &&
4435 (wcscmp(fd.cFileName, L"..") != 0)) {
4436 is_empty = false;
4437 }
4438 }
4439 FindClose(f);
4440 }
4441 os::free(search_path);
4442 return is_empty;
4443 }
4444
4445 // create binary file, rewriting existing file if required
4446 int os::create_binary_file(const char* path, bool rewrite_existing) {
4447 int oflags = _O_CREAT | _O_WRONLY | _O_BINARY;
4448 if (!rewrite_existing) {
4449 oflags |= _O_EXCL;
4450 }
4451 return ::open(path, oflags, _S_IREAD | _S_IWRITE);
4452 }
4453
4454 // return current position of file pointer
4455 jlong os::current_file_offset(int fd) {
4456 return (jlong)::_lseeki64(fd, (__int64)0L, SEEK_CUR);
4457 }
4458
4459 // move file pointer to the specified offset
4460 jlong os::seek_to_file_offset(int fd, jlong offset) {
4461 return (jlong)::_lseeki64(fd, (__int64)offset, SEEK_SET);
4462 }
|