bool mozilla::ReadSysFile( const char* aFilename, char* aBuf, size_t aBufSize) { int fd = MOZ_TEMP_FAILURE_RETRY(open(aFilename, O_RDONLY)); if (fd < 0) { return false; } ScopedClose autoClose(fd); if (aBufSize == 0) { return true; } ssize_t bytesRead; size_t offset = 0; do { bytesRead = MOZ_TEMP_FAILURE_RETRY( read(fd, aBuf + offset, aBufSize - offset)); if (bytesRead == -1) { return false; } offset += bytesRead; } while (bytesRead > 0 && offset < aBufSize); MOZ_ASSERT(offset <= aBufSize); if (offset > 0 && aBuf[offset - 1] == '\n') { offset--; } if (offset == aBufSize) { MOZ_ASSERT(offset > 0); offset--; } aBuf[offset] = '\0'; return true; }
/** * Create a file with the specified contents. */ static bool WriteFile( const char* aFilename, const void* aContents, size_t aContentsLen) { int fd; ssize_t ret; size_t offt; fd = MOZ_TEMP_FAILURE_RETRY( open(aFilename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); if (fd == -1) { fprintf(stderr, "open(): %s: %s\n", aFilename, strerror(errno)); return false; } offt = 0; do { ret = MOZ_TEMP_FAILURE_RETRY( write(fd, (char*)aContents + offt, aContentsLen - offt)); if (ret == -1) { fprintf(stderr, "write(): %s: %s\n", aFilename, strerror(errno)); close(fd); return false; } offt += ret; } while (offt < aContentsLen); ret = MOZ_TEMP_FAILURE_RETRY(close(fd)); if (ret == -1) { fprintf(stderr, "close(): %s: %s\n", aFilename, strerror(errno)); return false; } return true; }