11/*
2- * Copyright (c) 2001, 2021 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2001, 2022 , Oracle and/or its affiliates. All rights reserved.
33 * Copyright (c) 2012, 2021 SAP SE. All rights reserved.
44 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55 *
@@ -89,38 +89,25 @@ static void save_memory_to_file(char* addr, size_t size) {
8989 const char * destfile = PerfMemory::get_perfdata_file_path ();
9090 assert (destfile[0 ] != ' \0 ' , " invalid PerfData file path" );
9191
92- int result ;
92+ int fd ;
9393
94- RESTARTABLE (os::open (destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR),
95- result);
96- if (result == OS_ERR) {
97- if (PrintMiscellaneous && Verbose) {
98- warning (" Could not create Perfdata save file: %s: %s\n " ,
99- destfile, os::strerror (errno));
100- }
94+ RESTARTABLE (os::open (destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR), fd);
95+ if (fd == OS_ERR) {
96+ warning (" Could not create Perfdata save file: %s: %s\n " ,
97+ destfile, os::strerror (errno));
10198 } else {
102- int fd = result;
99+ ssize_t result;
103100
104- for (size_t remaining = size; remaining > 0 ;) {
105-
106- RESTARTABLE (::write (fd, addr, remaining), result);
107- if (result == OS_ERR) {
108- if (PrintMiscellaneous && Verbose) {
109- warning (" Could not write Perfdata save file: %s: %s\n " ,
110- destfile, os::strerror (errno));
111- }
112- break ;
113- }
114-
115- remaining -= (size_t )result;
116- addr += result;
101+ bool successful_write = os::write (fd, addr, size);
102+ if (!successful_write) {
103+ warning (" Could not write Perfdata save file: %s: %s\n " ,
104+ destfile, os::strerror (errno));
117105 }
118106
107+
119108 result = ::close (fd);
120- if (PrintMiscellaneous && Verbose) {
121- if (result == OS_ERR) {
122- warning (" Could not close %s: %s\n " , destfile, os::strerror (errno));
123- }
109+ if (result == OS_ERR) {
110+ warning (" Could not close %s: %s\n " , destfile, os::strerror (errno));
124111 }
125112 }
126113 FREE_C_HEAP_ARRAY (char , destfile);
@@ -880,9 +867,9 @@ static int create_sharedmem_file(const char* dirname, const char* filename, size
880867 // Open the filename in the current directory.
881868 // Cannot use O_TRUNC here; truncation of an existing file has to happen
882869 // after the is_file_secure() check below.
883- int result ;
884- RESTARTABLE (os::open (filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IRUSR|S_IWUSR), result );
885- if (result == OS_ERR) {
870+ int fd ;
871+ RESTARTABLE (os::open (filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IRUSR|S_IWUSR), fd );
872+ if (fd == OS_ERR) {
886873 if (PrintMiscellaneous && Verbose) {
887874 if (errno == ELOOP) {
888875 warning (" file %s is a symlink and is not secure\n " , filename);
@@ -898,9 +885,6 @@ static int create_sharedmem_file(const char* dirname, const char* filename, size
898885 // close the directory and reset the current working directory
899886 close_directory_secure_cwd (dirp, saved_cwd_fd);
900887
901- // save the file descriptor
902- int fd = result;
903-
904888 // check to see if the file is secure
905889 if (!is_file_secure (fd, filename)) {
906890 ::close (fd);
@@ -933,6 +917,8 @@ static int create_sharedmem_file(const char* dirname, const char* filename, size
933917 }
934918#endif
935919
920+ ssize_t result;
921+
936922 // truncate the file to get rid of any existing data
937923 RESTARTABLE (::ftruncate (fd, (off_t )0 ), result);
938924 if (result == OS_ERR) {
@@ -959,19 +945,19 @@ static int create_sharedmem_file(const char* dirname, const char* filename, size
959945 int zero_int = 0 ;
960946 result = (int )os::seek_to_file_offset (fd, (jlong)(seekpos));
961947 if (result == -1 ) break ;
962- RESTARTABLE (::write (fd, &zero_int, 1 ), result);
963- if (result != 1 ) {
948+ if (!os::write (fd, &zero_int, 1 )) {
964949 if (errno == ENOSPC) {
965950 warning (" Insufficient space for shared memory file:\n %s\n Try using the -Djava.io.tmpdir= option to select an alternate temp location.\n " , filename);
966951 }
952+ result = OS_ERR;
967953 break ;
968954 }
969955 }
970956
971957 if (result != -1 ) {
972958 return fd;
973959 } else {
974- ::close (fd);
960+ os ::close (fd);
975961 return -1 ;
976962 }
977963}
0 commit comments