/** * Create a buffered filestream from a portable file descriptor. * * @param[in] portLibrary The port library. * @param[in] fd The file descriptor. * @param[in] flags Open flags * * @return the resulting filestream. On failure, will return NULL. */ OMRFileStream * omrfilestream_fdopen(OMRPortLibrary *portLibrary, intptr_t fd, int32_t flags) { intptr_t fileDescriptor = 0; intptr_t rc = 0; OMRFileStream *fileStream = NULL; const char *realFlags = NULL; Trc_PRT_filestream_fdopen_Entry(fd, flags); /* translate the open flags */ realFlags = EsTranslateOpenFlags(flags); if (NULL == realFlags) { portLibrary->error_set_last_error(portLibrary, EINVAL, findError(EINVAL)); Trc_PRT_filestream_fdopen_invalidArgs(fd, flags); Trc_PRT_filestream_fdopen_Exit(NULL); return NULL; } /* Get the native file descriptor */ fileDescriptor = portLibrary->file_convert_omrfile_fd_to_native_fd(portLibrary, fd); /* Convert the file descriptor to a FILE */ fileStream = fdopen(fileDescriptor, realFlags); if (NULL == fileStream) { rc = portLibrary->error_set_last_error(portLibrary, errno, findError(errno)); Trc_PRT_filestream_fdopen_failed(fd, flags, (int32_t) rc); } Trc_PRT_filestream_fdopen_Exit(fileStream); return fileStream; }
int32_t omrfile_unlinkdir(struct OMRPortLibrary *portLibrary, const char *path) { wchar_t unicodeBuffer[UNICODE_BUFFER_SIZE], *unicodePath; BOOL result; /* Convert the filename from UTF8 to Unicode */ unicodePath = file_get_unicode_path(portLibrary, path, unicodeBuffer, UNICODE_BUFFER_SIZE); if (NULL == unicodePath) { return -1; } /*PR 93036 - should be able to delete read-only dirs, so we set the file attribute back to normal*/ if (0 == SetFileAttributesW(unicodePath, FILE_ATTRIBUTE_NORMAL)) { int32_t error = GetLastError(); portLibrary->error_set_last_error(portLibrary, error, findError(error)); /* continue */ } result = RemoveDirectoryW(unicodePath); if (unicodeBuffer != unicodePath) { portLibrary->mem_free_memory(portLibrary, unicodePath); } if (!result) { int32_t error = GetLastError(); portLibrary->error_set_last_error(portLibrary, error, findError(error)); return -1; } return 0; }
int32_t omrfile_stat(struct OMRPortLibrary *portLibrary, const char *path, uint32_t flags, struct J9FileStat *buf) { struct stat statbuf; #if defined(LINUX) || defined(AIXPPC) || defined(OSX) PlatformStatfs statfsbuf; #endif /* defined(LINUX) || defined(AIXPPC) || defined(OSX) */ memset(buf, 0, sizeof(J9FileStat)); /* Neutrino does not handle NULL for stat */ if (stat(path, &statbuf)) { return portLibrary->error_set_last_error(portLibrary, errno, findError(errno)); } #if defined(LINUX) || defined(OSX) if (statfs(path, &statfsbuf)) { return portLibrary->error_set_last_error(portLibrary, errno, findError(errno)); } updateJ9FileStat(portLibrary, buf, &statbuf, &statfsbuf); #elif defined(AIXPPC) && !defined(J9OS_I5) /* defined(LINUX) || defined(OSX) */ if (statvfs(path, &statfsbuf)) { return portLibrary->error_set_last_error(portLibrary, errno, findError(errno)); } updateJ9FileStat(portLibrary, buf, &statbuf, &statfsbuf); #else /* defined(AIXPPC) && !defined(J9OS_I5) */ updateJ9FileStat(portLibrary, buf, &statbuf); #endif /* defined(LINUX) || defined(OSX) */ return 0; }
static bool isValueStr(const STString &str) { bool ret = false; if( str.empty() ) { return ret; } STString valueTag = getFirstTagName(str); if ("bool" != valueTag && "int" != valueTag && "string" != valueTag) { return ret; } STString startTag("<" + valueTag + ">"); STString endTag("</" + valueTag + ">"); int beginPos = str.find(startTag); int endPos = str.find_last_of(endTag); if (findError(beginPos) || findError(endPos) ) { return ret; } else { ret = true; } return ret; }
/** * Opens a buffered filestream. All filestreams opened this way start fully buffered. * Opening a filestream with EsOpenRead is not supported, and will return an error code. * * @param[in] portLibrary The port library * @param[in] path Name of the file to be opened. * @param[in] flags Portable file read/write attributes. * @param[in] mode Platform file permissions. * * @return The filestream for the newly opened file. NULL on failure. * * @note accesses the "current working directory" property of the process if the path is relative. */ OMRFileStream * omrfilestream_open(struct OMRPortLibrary *portLibrary, const char *path, int32_t flags, int32_t mode) { OMRFileStream *fileStream = NULL; const char *realFlags = NULL; intptr_t file = -1; intptr_t fileDescriptor = -1; Trc_PRT_filestream_open_Entry(path, flags, mode); if (NULL == path) { Trc_PRT_filestream_open_invalidPath(path, flags, mode); portLibrary->error_set_last_error(portLibrary, EINVAL, findError(EINVAL)); Trc_PRT_filestream_open_Exit(NULL); return NULL; } realFlags = EsTranslateOpenFlags(flags); if ((NULL == realFlags) || (EsOpenRead == (flags & EsOpenRead))) { Trc_PRT_filestream_open_invalidOpenFlags(path, flags, mode); portLibrary->error_set_last_error(portLibrary, EINVAL, findError(EINVAL)); Trc_PRT_filestream_open_Exit(NULL); return NULL; } /* Attempt to portably honor the open flags and mode by using the portlibrary */ file = portLibrary->file_open(portLibrary, path, flags, mode); if (-1 == file) { int32_t error = portLibrary->error_last_error_number(portLibrary); Trc_PRT_filestream_open_failedToOpen(path, flags, mode, error); Trc_PRT_filestream_open_Exit(NULL); return NULL; } /* Get the native file descriptor */ fileDescriptor = portLibrary->file_convert_omrfile_fd_to_native_fd(portLibrary, file); /* Convert the file to a fileDescriptor */ fileStream = fdopen(fileDescriptor, realFlags); if (NULL == fileStream) { int savedErrno = errno; portLibrary->file_close(portLibrary, file); Trc_PRT_filestream_open_fdopenFailed(path, flags, mode, findError(savedErrno)); portLibrary->error_set_last_error(portLibrary, savedErrno, findError(savedErrno)); } Trc_PRT_filestream_open_Exit(fileStream); return fileStream; }
int32_t omrfile_chmod(struct OMRPortLibrary *portLibrary, const char *path, int32_t mode) { int32_t attrs; int32_t result; wchar_t unicodeBuffer[UNICODE_BUFFER_SIZE], *unicodePath; int32_t actualMode = J9_FILE_MODE_READABLE; Trc_PRT_file_chmod_Entry(path, mode); /* Convert the filename from UTF8 to Unicode */ unicodePath = file_get_unicode_path(portLibrary, path, unicodeBuffer, UNICODE_BUFFER_SIZE); if (NULL == unicodePath) { Trc_PRT_file_chmod_Exit(-1); return -1; } attrs = GetFileAttributesW(unicodePath); if (attrs == INVALID_FILE_ATTRIBUTES) { int32_t setError; result = GetLastError(); setError = portLibrary->error_set_last_error(portLibrary, result, findError(result)); Trc_PRT_file_chmod_getAttributesFailed(setError); if (unicodeBuffer != unicodePath) { portLibrary->mem_free_memory(portLibrary, unicodePath); } Trc_PRT_file_chmod_Exit(-1); return -1; } if (J9_FILE_MODE_WRITABLE == (J9_FILE_MODE_WRITABLE & mode)) { /* If all of the write-enable bits in mode are set, then turn off READONLY */ attrs = attrs & ~FILE_ATTRIBUTE_READONLY; actualMode |= J9_FILE_MODE_WRITABLE; } else { attrs = attrs | FILE_ATTRIBUTE_READONLY; } result = SetFileAttributesW(unicodePath, attrs); if (0 == result) { int32_t setError; result = GetLastError(); setError = portLibrary->error_set_last_error(portLibrary, result, findError(result)); Trc_PRT_file_chmod_setAttributesFailed(setError); actualMode = -1; } if (unicodeBuffer != unicodePath) { portLibrary->mem_free_memory(portLibrary, unicodePath); } Trc_PRT_file_chmod_Exit(actualMode); return actualMode; }
IDATA hyfile_write (IDATA fd, const void *buf, IDATA nbytes) { IDATA rc = 0; #ifdef ZOS if (fd == HYPORT_TTY_OUT) { rc = fwrite(buf, sizeof(char), nbytes, stdout); } else if (fd == HYPORT_TTY_ERR) { rc = fwrite(buf, sizeof(char), nbytes, stderr); } else if (fd < FD_BIAS) { /* Cannot fsync STDIN, and no other FD's should exist <FD_BIAS */ return -1; } else #endif /* ZOS */ { /* write will just do the right thing for HYPORT_TTY_OUT and HYPORT_TTY_ERR */ rc = write ((int) (fd - FD_BIAS), buf, nbytes); } if (rc == -1) { int rc = errno; return hyerror_set_last_error(rc, findError(rc)); } return rc; }
/** * Set the length of a file to a specified value. * * @param[in] portLibrary The port library * @param[in] fd The file descriptor. * @param[in] newLength Length to be set * * @return 0 on success, negative portable error code on failure */ int32_t omrfile_set_length(struct OMRPortLibrary *portLibrary, intptr_t inFD, int64_t newLength) { int fd = (int)inFD; int32_t rc; off_t length = (off_t)newLength; Trc_PRT_file_setlength_Entry(inFD, newLength); /* If file offsets are 32 bit, truncate the newLength to that range */ if (sizeof(off_t) < sizeof(int64_t)) { if (newLength > 0x7FFFFFFF) { length = 0x7FFFFFFF; } else if (newLength < -0x7FFFFFFF) { length = -0x7FFFFFFF; } } #if (FD_BIAS != 0) if (fd < FD_BIAS) { /* Cannot ftruncate on STD streams, and no other FD's should exist <FD_BIAS */ Trc_PRT_file_setlength_Exit(-1); return -1; } #endif rc = ftruncate(fd - FD_BIAS, length); if (0 != rc) { rc = portLibrary->error_set_last_error(portLibrary, errno, findError(errno)); } Trc_PRT_file_setlength_Exit(rc); return rc; }
int64_t omrfile_lastmod(struct OMRPortLibrary *portLibrary, const char *path) { int64_t result = -1; wchar_t unicodeBuffer[UNICODE_BUFFER_SIZE], *unicodePath; Trc_PRT_file_lastmod_Entry(path); /* Convert the filename from UTF8 to Unicode */ unicodePath = file_get_unicode_path(portLibrary, path, unicodeBuffer, UNICODE_BUFFER_SIZE); if (NULL != unicodePath) { WIN32_FILE_ATTRIBUTE_DATA myStat; if (0 == GetFileAttributesExW(unicodePath, GetFileExInfoStandard, &myStat)) { int32_t error = GetLastError(); result = portLibrary->error_set_last_error(portLibrary, error, findError(error)); } else { /* * Search MSDN for 'Converting a time_t Value to a File Time' for following implementation. */ result = ((int64_t) myStat.ftLastWriteTime.dwHighDateTime << 32) | (int64_t) myStat.ftLastWriteTime.dwLowDateTime; result = (result - 116444736000000000) / 10000; } if (unicodeBuffer != unicodePath) { portLibrary->mem_free_memory(portLibrary, unicodePath); } } Trc_PRT_file_lastmod_Exit(result); return result; }
int64_t omrfile_length(struct OMRPortLibrary *portLibrary, const char *path) { int64_t result = -1; wchar_t unicodeBuffer[UNICODE_BUFFER_SIZE], *unicodePath; Trc_PRT_file_length_Entry(path); /* Convert the filename from UTF8 to Unicode */ unicodePath = file_get_unicode_path(portLibrary, path, unicodeBuffer, UNICODE_BUFFER_SIZE); if (NULL != unicodePath) { WIN32_FILE_ATTRIBUTE_DATA myStat; if (0 == GetFileAttributesExW(unicodePath, GetFileExInfoStandard, &myStat)) { int32_t error = GetLastError(); result = portLibrary->error_set_last_error(portLibrary, error, findError(error)); } else { result = ((int64_t)myStat.nFileSizeHigh) << 32; result += (int64_t)myStat.nFileSizeLow; } if (unicodeBuffer != unicodePath) { portLibrary->mem_free_memory(portLibrary, unicodePath); } } Trc_PRT_file_length_Exit(result); return result; }
int32_t omrfile_mkdir(struct OMRPortLibrary *portLibrary, const char *path) { wchar_t unicodeBuffer[UNICODE_BUFFER_SIZE], *unicodePath; int32_t result = 0; Trc_PRT_file_mkdir_entry2(path); /* Convert the filename from UTF8 to Unicode */ unicodePath = file_get_unicode_path(portLibrary, path, unicodeBuffer, UNICODE_BUFFER_SIZE); if (NULL == unicodePath) { result = -1; } else { result = CreateDirectoryW(unicodePath, 0); if (unicodeBuffer != unicodePath) { portLibrary->mem_free_memory(portLibrary, unicodePath); } if (0 == result) { int32_t error = GetLastError(); result = portLibrary->error_set_last_error(portLibrary, error, findError(error)); } else { result = 0; } } Trc_PRT_file_mkdir_exit2(result); return result; }
static void setPortableError(OMRPortLibrary *portLibrary, const char *funcName, int32_t portlibErrno, int systemErrno) { char *errmsgbuff = NULL; int32_t errmsglen = 0; int32_t portableErrno = portlibErrno + findError(systemErrno); /*Get size of str_printf buffer (it includes null terminator)*/ errmsglen = portLibrary->str_printf(portLibrary, NULL, 0, "%s%s", funcName, strerror(systemErrno)); if (errmsglen <= 0) { /*Set the error with no message*/ portLibrary->error_set_last_error(portLibrary, systemErrno, portableErrno); return; } /*Alloc the buffer*/ errmsgbuff = portLibrary->mem_allocate_memory(portLibrary, errmsglen, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY); if (NULL == errmsgbuff) { /*Set the error with no message*/ portLibrary->error_set_last_error(portLibrary, systemErrno, portableErrno); return; } /*Fill the buffer using str_printf*/ portLibrary->str_printf(portLibrary, errmsgbuff, errmsglen, "%s%s", funcName, strerror(systemErrno)); /*Set the error message*/ portLibrary->error_set_last_error_with_message(portLibrary, portableErrno, errmsgbuff); /*Free the buffer*/ portLibrary->mem_free_memory(portLibrary, errmsgbuff); return; }
int32_t omrfile_move(struct OMRPortLibrary *portLibrary, const char *pathExist, const char *pathNew) { wchar_t unicodeBufferExist[UNICODE_BUFFER_SIZE], *unicodePathExist; wchar_t unicodeBufferNew[UNICODE_BUFFER_SIZE], *unicodePathNew; BOOL result; /* Convert the filenames from UTF8 to Unicode */ unicodePathExist = file_get_unicode_path(portLibrary, pathExist, unicodeBufferExist, UNICODE_BUFFER_SIZE); if (NULL == unicodePathExist) { return -1; } unicodePathNew = file_get_unicode_path(portLibrary, pathNew, unicodeBufferNew, UNICODE_BUFFER_SIZE); if (NULL == unicodePathNew) { if (unicodeBufferExist != unicodePathExist) { portLibrary->mem_free_memory(portLibrary, unicodePathExist); } return -1; } result = MoveFileW(unicodePathExist, unicodePathNew); if (unicodeBufferExist != unicodePathExist) { portLibrary->mem_free_memory(portLibrary, unicodePathExist); } if (unicodeBufferNew != unicodePathNew) { portLibrary->mem_free_memory(portLibrary, unicodePathNew); } if (!result) { int32_t error = GetLastError(); portLibrary->error_set_last_error(portLibrary, error, findError(error)); return -1; } return 0; }
int32_t omrfile_set_length(struct OMRPortLibrary *portLibrary, intptr_t fd, int64_t newLength) { int32_t lastError = 0; int64_t currentFilePtr = 0; Trc_PRT_file_setlength_Entry(fd, newLength); /* Save current file pointer location */ currentFilePtr = omrfile_seek(portLibrary, fd, 0, EsSeekCur); if ((-1 != currentFilePtr) && (-1 != omrfile_seek(portLibrary, fd, newLength, FILE_BEGIN))) { if (0 == SetEndOfFile((HANDLE)fd)) { lastError = GetLastError(); lastError = portLibrary->error_set_last_error(portLibrary, lastError, findError(lastError)); Trc_PRT_file_setlength_Exit(lastError); return lastError; } /* Put pointer back to where it started */ if (-1 != omrfile_seek(portLibrary, fd, currentFilePtr, EsSeekSet)) { Trc_PRT_file_setlength_Exit(0); return 0; } } /* omrfile_seek already set the last error */ lastError = portLibrary->error_last_error_number(portLibrary); Trc_PRT_file_setlength_Exit(lastError); return lastError; }
void errorElementVar(double* err, double* base, double v, double* tA, double* tB, double* RA, double* RB, double* Rs, double* Re, double* t){ double temp[6]; findError(temp,tA,tB,RA,RB,Rs,Re,t); for(size_t i = 0; i < 6; i++){ err[i] += v*(temp[i] - base[i])*(temp[i] - base[i]); } }
IDATA hyfile_open (const char *path, I_32 flags, I_32 mode) { struct stat buffer; I_32 fd; I_32 realFlags = EsTranslateOpenFlags (flags); I_32 fdflags; // Trc_PRT_file_open_Entry (path, flags, mode); if (realFlags == -1) { // Trc_PRT_file_open_Exit1 (flags); hyerror_set_last_error(EINVAL, findError(EINVAL)); return -1; } if ( ( flags&HyOpenRead && !(flags&HyOpenWrite) ) && !stat (path, &buffer)) { if (S_ISDIR (buffer.st_mode)) { hyerror_set_last_error_with_message(findError(EEXIST), "Is a directory"); // Trc_PRT_file_open_Exit4 (); return -1; } } fd = open (path, realFlags, mode); if (-1 == fd) { // Trc_PRT_file_open_Exit2 (errno, findError (errno)); int rc = errno; hyerror_set_last_error(rc, findError(rc)); return -1; } /* Tag this descriptor as being non-inheritable */ fdflags = fcntl (fd, F_GETFD, 0); fcntl (fd, F_SETFD, fdflags | FD_CLOEXEC); fd += FD_BIAS; // Trc_PRT_file_open_Exit (fd); return (IDATA) fd; }
static STString getFirstTagName(const STString &str, int &position) { STString ret; int beginPos = -1; int endPos = -1; beginPos = str.find("<") + 1; endPos = str.find(">"); if (findError(beginPos) || findError(endPos)) { return ret; } else { ret = str.substr(beginPos, endPos - beginPos); position = beginPos -1; } return ret; }
int32_t omrfile_unlink(struct OMRPortLibrary *portLibrary, const char *path) { int32_t rc; rc = unlink(path); if (-1 == rc) { portLibrary->error_set_last_error(portLibrary, errno, findError(errno)); } return rc; }
void loop() { float right_v = 0; float left_v = 0; findError(); /* If Enough Error Send Control Values */ if(error_d > 2 || abs(error_a) > 5) { float v = error_d * p_gain_d; right_v = v; left_v = v; if(error_a > 0) { // Need to turn right right_v += error_d*p_gain_a; left_v -= error_d*p_gain_a; } else { // Need to turn left left_v += error_d*p_gain_a; right_v -= error_d*p_gain_a; } } else { // If error is small go to next node goal_node++; } /* Filter Out Bad Values */ if(left_v > 100) { left_v = 100; } if(left_v < 0) { left_v = 0; } if(right_v > 100) { right_v = 100; } if(right_v < 0) { right_v = 0; } /* Send Motor Speeds */ sparki.motorRotate(MOROT_LEFT,DIR_CCW,left_v); sparki.motorRotate(MOTOR_RIGHT,DIR_CW,right_v); /* Calculate Speeds Of Left And Right Motors */ wheelLeft = (left_v/100) * motorSpeed; wheelRight = (right_v/100) * motorSpeed; delay(50); /* Calculate Odometry */ deltaTime = float((millis() - time)) / 1000.0; xi = xi + cos(theta) * (r * wheelRight / 2 + r * wheelLeft / 2) * deltaTime; yi = yi + sin(theta) * (r * wheelRight / 2 + r * wheelLeft / 2) * deltaTime; theta = theta + (wheelRight * r / d - wheelLeft * r / d) * deltaTime; time = millis(); }
int32_t omrfile_mkdir(struct OMRPortLibrary *portLibrary, const char *path) { int32_t rc = 0; Trc_PRT_file_mkdir_entry2(path); if (-1 == mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO)) { rc = portLibrary->error_set_last_error(portLibrary, errno, findError(errno)); } Trc_PRT_file_mkdir_exit2(rc); return rc; }
void omrfile_findclose(struct OMRPortLibrary *portLibrary, uintptr_t findhandle) { int32_t error = 0; Trc_PRT_file_findclose_Entry(findhandle); if (0 == FindClose((HANDLE)findhandle)) { error = GetLastError(); error = portLibrary->error_set_last_error(portLibrary, error, findError(error)); } Trc_PRT_file_findclose_Exit(error); }
I_32 hysock_read (hysocket_t sock, U_8 * buf, I_32 nbyte, I_32 flags) { I_32 bytesRec = 0; bytesRec = recv (SOCKET_CAST (sock), buf, nbyte, flags); if (-1 == bytesRec) { I_32 err = errno; return hyerror_set_last_error(err, findError(err)); } else { return bytesRec; } }
void findErrVar(double* err, double* verr, double* tA, double* vtA, double* tB, double* vtB, double* RA, double* vRA, double* RB, double* vRB, double* Rs, double* vRs, double* Re, double* vRe, double* t){ findError(err, tA, tB, RA, RB, Rs, Re, t); //add OFFSET to estimate variance for(size_t j = 0; j < 6; j++){ verr[j] = 0; } for(size_t j = 0; j < 3; j++){ tA[j] += OFFSET; errorElementVar(verr,err, vtA[j], tA, tB, RA, RB, Rs, Re, t); tA[j] -= OFFSET; } for(size_t j = 0; j < 3; j++){ tB[j] += OFFSET; errorElementVar(verr,err, vtB[j], tA, tB, RA, RB, Rs, Re, t); tB[j] -= OFFSET; } for(size_t j = 0; j < 3; j++){ RA[j] += OFFSET; errorElementVar(verr,err, vRA[j], tA, tB, RA, RB, Rs, Re, t); RA[j] -= OFFSET; } for(size_t j = 0; j < 3; j++){ RB[j] += OFFSET; errorElementVar(verr,err, vRB[j], tA, tB, RA, RB, Rs, Re, t); RB[j] -= OFFSET; } for(size_t j = 0; j < 3; j++){ Rs[j] += OFFSET; errorElementVar(verr,err, vRs[j], tA, tB, RA, RB, Rs, Re, t); Rs[j] -= OFFSET; } for(size_t j = 0; j < 3; j++){ Re[j] += OFFSET; errorElementVar(verr,err, vRe[j], tA, tB, RA, RB, Rs, Re, t); Re[j] -= OFFSET; } //transform to variance for(size_t j = 0; j < 6; j++){ verr[j] = verr[j]/(OFFSET*OFFSET); } }
int32_t omrfile_chmod(struct OMRPortLibrary *portLibrary, const char *path, int32_t mode) { int32_t result; intptr_t actualMode = -1; struct stat buffer; const int32_t MODEBITS = 07777; /* bits from S_ISUID down: lstat can return mode with the high-order bits set. */ Trc_PRT_file_chmod_Entry(path, mode); result = chmod(path, mode); if (0 != result) { result = portLibrary->error_set_last_error(portLibrary, errno, findError(errno)); Trc_PRT_file_chmod_setAttributesFailed(result); } else if (0 != lstat(path, &buffer)) { result = portLibrary->error_set_last_error(portLibrary, errno, findError(errno)); Trc_PRT_file_chmod_getAttributesFailed(result); } else { actualMode = buffer.st_mode & MODEBITS; } Trc_PRT_file_chmod_Exit(actualMode); return actualMode; }
I_32 hysock_getsockname (hysocket_t handle, hysockaddr_t addrHandle) { socklen_t addrlen = sizeof (addrHandle->addr); if (getsockname (SOCKET_CAST (handle), (struct sockaddr *) &addrHandle->addr, &addrlen) != 0) { I_32 err = errno; return hyerror_set_last_error(err, findError(err)); } return 0; }
intptr_t omrfile_write(struct OMRPortLibrary *portLibrary, intptr_t fd, const void *buf, intptr_t nbytes) { DWORD nCharsWritten; intptr_t toWrite, offset = 0; int32_t errorCode; HANDLE handle; Trc_PRT_file_write_Entry(fd, buf, nbytes); if (OMRPORT_TTY_OUT == fd) { handle = PPG_tty_consoleOutputHd; } else if (OMRPORT_TTY_ERR == fd) { handle = PPG_tty_consoleErrorHd; } else { handle = (HANDLE)fd; } toWrite = nbytes; while (nbytes > 0) { if (toWrite > nbytes) { toWrite = nbytes; } /* The (DWORD)nbytes downcasts can make toWrite smaller than nbytes, however, the while loop * ensures that nbytes of data is still written */ if (FALSE == WriteFile(handle, (char *)buf + offset, (DWORD)toWrite, &nCharsWritten, NULL)) { errorCode = GetLastError(); if (ERROR_NOT_ENOUGH_MEMORY == errorCode) { /*[PR 94924] Use 48K chunks to get around out of memory problem */ if (toWrite > (48 * 1024)) { toWrite = 48 * 1024; } else { toWrite /= 2; } /* If we can't write 128 bytes, just return */ if (toWrite >= 128) { continue; } } errorCode = portLibrary->error_set_last_error(portLibrary, errorCode, findError(errorCode)); Trc_PRT_file_write_Exit(errorCode); return errorCode; } offset += nCharsWritten; nbytes -= nCharsWritten; } Trc_PRT_file_write_Exit(offset); return offset; }
int32_t omrfile_stat_filesystem(struct OMRPortLibrary *portLibrary, const char *path, uint32_t flags, struct J9FileStatFilesystem *buf) { DWORD result; wchar_t unicodeBuffer[UNICODE_BUFFER_SIZE], *unicodePath; wchar_t driveBuffer[UNICODE_BUFFER_SIZE]; if (NULL == path || NULL == buf) { return -1; } /* Convert the filename from UTF8 to Unicode */ unicodePath = file_get_unicode_path(portLibrary, path, unicodeBuffer, UNICODE_BUFFER_SIZE); if (NULL == unicodePath) { return -1; } result = GetFullPathNameW(unicodePath, UNICODE_BUFFER_SIZE, driveBuffer, NULL); if (unicodeBuffer != unicodePath) { portLibrary->mem_free_memory(portLibrary, unicodePath); } if (result == 0 || result > UNICODE_BUFFER_SIZE) { result = GetLastError(); return portLibrary->error_set_last_error(portLibrary, result, findError(result)); } driveBuffer[3] = '\0'; /* Chop off everything after the initial X:\ */ result = GetDiskFreeSpaceExW(driveBuffer, (PULARGE_INTEGER)&buf->freeSizeBytes, (PULARGE_INTEGER)&buf->totalSizeBytes, NULL); if (0 == result) { result = GetLastError(); return portLibrary->error_set_last_error(portLibrary, result, findError(result)); } return 0; }
/** * Get the portable file descriptor backing a filestream. * * @param[in] portLibrary The port library. * @param[in] fileStream The filestream; * @return The portable file descriptor. Will return OMRPORT_INVALID_FD on error. Calling this * function on an invalid filestream will result in undefined behaviour. */ intptr_t omrfilestream_fileno(OMRPortLibrary *portLibrary, OMRFileStream *fileStream) { intptr_t omrfile = OMRPORT_INVALID_FD; Trc_PRT_filestream_fileno_Entry(fileStream); if (NULL == fileStream) { portLibrary->error_set_last_error(portLibrary, EINVAL, findError(EINVAL)); Trc_PRT_filestream_fileno_invalidArgs(fileStream); } else { /* Get the C library file descriptor backing the stream */ intptr_t fileDescriptor = fileno(fileStream); if (-1 == fileDescriptor) { int32_t rc = portLibrary->error_set_last_error(portLibrary, errno, findError(errno)); Trc_PRT_filestream_fileno_failed(fileStream, rc); } else { omrfile = portLibrary->file_convert_native_fd_to_omrfile_fd(portLibrary, fileDescriptor); } } Trc_PRT_filestream_fileno_Exit(omrfile); return omrfile; }
int64_t omrfile_length(struct OMRPortLibrary *portLibrary, const char *path) { struct stat st; Trc_PRT_file_length_Entry(path); /* Neutrino does not handle NULL for stat */ if (0 != stat(path, &st)) { int32_t errorCode = portLibrary->error_set_last_error(portLibrary, errno, findError(errno)); Trc_PRT_file_length_Exit(errorCode); return errorCode; } Trc_PRT_file_length_Exit((int64_t)st.st_size); return (int64_t) st.st_size; }
I_32 hysock_write (hysocket_t sock, U_8 * buf, I_32 nbyte, I_32 flags) { I_32 bytesSent = 0; bytesSent = send (SOCKET_CAST (sock), buf, nbyte, flags); if (-1 == bytesSent) { I_32 err = errno; return hyerror_set_last_error(err, findError(err)); } else { return bytesSent; } }