src/java.base/windows/native/libjava/io_util_md.c

Print this page


   1 /*
   2  * Copyright (c) 2001, 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 559     long highPos = 0;
 560     DWORD op = FILE_CURRENT;
 561     HANDLE h = (HANDLE)fd;
 562 
 563     if (whence == SEEK_END) {
 564         op = FILE_END;
 565     }
 566     if (whence == SEEK_CUR) {
 567         op = FILE_CURRENT;
 568     }
 569     if (whence == SEEK_SET) {
 570         op = FILE_BEGIN;
 571     }
 572 
 573     distance.QuadPart = offset;
 574     if (SetFilePointerEx(h, distance, &pos, op) == 0) {
 575         return -1;
 576     }
 577     return long_to_jlong(pos.QuadPart);
 578 }
 579 
 580 size_t
 581 getLastErrorString(char *utf8_jvmErrorMsg, size_t cbErrorMsg)
 582 {
 583     size_t n = 0;
 584     if (cbErrorMsg > 0) {
 585         BOOLEAN noError = FALSE;
 586         WCHAR *utf16_osErrorMsg = (WCHAR *)malloc(cbErrorMsg*sizeof(WCHAR));
 587         if (utf16_osErrorMsg == NULL) {
 588             // OOM accident
 589             strncpy(utf8_jvmErrorMsg, "Out of memory", cbErrorMsg);
 590             // truncate if too long
 591             utf8_jvmErrorMsg[cbErrorMsg - 1] = '\0';
 592             n = strlen(utf8_jvmErrorMsg);
 593         } else {
 594             DWORD errval = GetLastError();
 595             if (errval != 0) {
 596                 // WIN32 error
 597                 n = (size_t)FormatMessageW(
 598                     FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
 599                     NULL,
 600                     errval,
 601                     0,
 602                     utf16_osErrorMsg,
 603                     (DWORD)cbErrorMsg,
 604                     NULL);
 605                 if (n > 3) {
 606                     // Drop final '.', CR, LF
 607                     if (utf16_osErrorMsg[n - 1] == L'\n') --n;
 608                     if (utf16_osErrorMsg[n - 1] == L'\r') --n;
 609                     if (utf16_osErrorMsg[n - 1] == L'.') --n;
 610                     utf16_osErrorMsg[n] = L'\0';
 611                 }
 612             } else if (errno != 0) {
 613                 // C runtime error that has no corresponding WIN32 error code
 614                 const WCHAR *rtError = _wcserror(errno);
 615                 if (rtError != NULL) {
 616                     wcsncpy(utf16_osErrorMsg, rtError, cbErrorMsg);
 617                     // truncate if too long
 618                     utf16_osErrorMsg[cbErrorMsg - 1] = L'\0';
 619                     n = wcslen(utf16_osErrorMsg);
 620                 }
 621             } else
 622                 noError = TRUE; //OS has no error to report
 623 
 624             if (!noError) {
 625                 if (n > 0) {
 626                     n = WideCharToMultiByte(
 627                         CP_UTF8,
 628                         0,
 629                         utf16_osErrorMsg,
 630                         n,
 631                         utf8_jvmErrorMsg,
 632                         cbErrorMsg,
 633                         NULL,
 634                         NULL);
 635 
 636                     // no way to die
 637                     if (n > 0)
 638                         utf8_jvmErrorMsg[min(cbErrorMsg - 1, n)] = '\0';
 639                 }
 640 
 641                 if (n <= 0) {
 642                     strncpy(utf8_jvmErrorMsg, "Secondary error while OS message extraction", cbErrorMsg);
 643                     // truncate if too long
 644                     utf8_jvmErrorMsg[cbErrorMsg - 1] = '\0';
 645                     n = strlen(utf8_jvmErrorMsg);
 646                 }
 647             }
 648             free(utf16_osErrorMsg);
 649         }
 650     }
 651     return n;
 652 }
   1 /*
   2  * Copyright (c) 2001, 2014, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 559     long highPos = 0;
 560     DWORD op = FILE_CURRENT;
 561     HANDLE h = (HANDLE)fd;
 562 
 563     if (whence == SEEK_END) {
 564         op = FILE_END;
 565     }
 566     if (whence == SEEK_CUR) {
 567         op = FILE_CURRENT;
 568     }
 569     if (whence == SEEK_SET) {
 570         op = FILE_BEGIN;
 571     }
 572 
 573     distance.QuadPart = offset;
 574     if (SetFilePointerEx(h, distance, &pos, op) == 0) {
 575         return -1;
 576     }
 577     return long_to_jlong(pos.QuadPart);
 578 }