bool TRI_PrintJson (int fd, TRI_json_t const* object) { TRI_string_buffer_t buffer; char const* p; size_t n; TRI_InitStringBuffer(&buffer, TRI_UNKNOWN_MEM_ZONE); StringifyJson(buffer._memoryZone, &buffer, object, true); p = TRI_BeginStringBuffer(&buffer); n = TRI_LengthStringBuffer(&buffer); while (0 < n) { ssize_t m = TRI_WRITE(fd, p, n); if (m <= 0) { TRI_AnnihilateStringBuffer(&buffer); return false; } n -= m; p += m; } TRI_AnnihilateStringBuffer(&buffer); return true; }
void spit (string const& filename, StringBuffer const& content) { int fd = TRI_CREATE(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP); if (fd == -1) { LOG_TRACE("open failed for '%s' with %s", filename.c_str(), strerror(errno)); THROW_FILE_OPEN_ERROR("open", filename, "O_WRONLY | O_CREAT | O_TRUNC", errno); } char const* ptr = content.c_str(); size_t len = content.length(); while (0 < len) { ssize_t n = TRI_WRITE(fd, ptr, len); if (n < 1) { TRI_CLOSE(fd); LOG_TRACE("write failed for '%s' with %s and result %d on %d", filename.c_str(), strerror(errno), (int) n, fd); THROW_FILE_FUNC_ERROR("write", "", errno); } ptr += n; len -= n; } TRI_CLOSE(fd); return; }
static bool PrintVelocyPack(int fd, VPackSlice const& slice, bool appendNewline) { if (slice.isNone()) { // sanity check return false; } TRI_string_buffer_t buffer; TRI_InitStringBuffer(&buffer, TRI_UNKNOWN_MEM_ZONE); arangodb::basics::VPackStringBufferAdapter bufferAdapter(&buffer); try { VPackDumper dumper(&bufferAdapter); dumper.dump(slice); } catch (...) { // Writing failed TRI_AnnihilateStringBuffer(&buffer); return false; } if (TRI_LengthStringBuffer(&buffer) == 0) { // should not happen return false; } if (appendNewline) { // add the newline here so we only need one write operation in the ideal // case TRI_AppendCharStringBuffer(&buffer, '\n'); } char const* p = TRI_BeginStringBuffer(&buffer); size_t n = TRI_LengthStringBuffer(&buffer); while (0 < n) { ssize_t m = TRI_WRITE(fd, p, (TRI_write_t)n); if (m <= 0) { TRI_AnnihilateStringBuffer(&buffer); return false; } n -= m; p += m; } TRI_AnnihilateStringBuffer(&buffer); return true; }
bool TRI_PrintJson (int fd, TRI_json_t const* object, bool appendNewline) { TRI_string_buffer_t buffer; char const* p; size_t n; if (object == NULL) { // sanity check return false; } TRI_InitStringBuffer(&buffer, TRI_UNKNOWN_MEM_ZONE); if (StringifyJson(buffer._memoryZone, &buffer, object, true) != TRI_ERROR_NO_ERROR) { TRI_AnnihilateStringBuffer(&buffer); return false; } if (TRI_LengthStringBuffer(&buffer) == 0) { // should not happen return false; } if (appendNewline) { // add the newline here so we only need one write operation in the ideal case TRI_AppendCharStringBuffer(&buffer, '\n'); } p = TRI_BeginStringBuffer(&buffer); n = TRI_LengthStringBuffer(&buffer); while (0 < n) { ssize_t m = TRI_WRITE(fd, p, (TRI_write_t) n); if (m <= 0) { TRI_AnnihilateStringBuffer(&buffer); return false; } n -= m; p += m; } TRI_AnnihilateStringBuffer(&buffer); return true; }
bool TRI_SaveJson (char const* filename, TRI_json_t const* object) { bool ok; char* tmp; int fd; int res; ssize_t m; tmp = TRI_Concatenate2String(filename, ".tmp"); if (tmp == NULL) { return false; } fd = TRI_CREATE(tmp, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR); if (fd < 0) { TRI_set_errno(TRI_ERROR_SYS_ERROR); LOG_ERROR("cannot create json file '%s': '%s'", tmp, TRI_LAST_ERROR_STR); TRI_FreeString(TRI_CORE_MEM_ZONE, tmp); return false; } ok = TRI_PrintJson(fd, object); if (! ok) { TRI_set_errno(TRI_ERROR_SYS_ERROR); LOG_ERROR("cannot write to json file '%s': '%s'", tmp, TRI_LAST_ERROR_STR); TRI_UnlinkFile(tmp); TRI_FreeString(TRI_CORE_MEM_ZONE, tmp); return false; } m = TRI_WRITE(fd, "\n", 1); if (m <= 0) { TRI_set_errno(TRI_ERROR_SYS_ERROR); LOG_ERROR("cannot write to json file '%s': '%s'", tmp, TRI_LAST_ERROR_STR); TRI_UnlinkFile(tmp); TRI_FreeString(TRI_CORE_MEM_ZONE, tmp); return false; } ok = TRI_fsync(fd); if (! ok) { TRI_set_errno(TRI_ERROR_SYS_ERROR); LOG_ERROR("cannot sync saved json '%s': '%s'", tmp, TRI_LAST_ERROR_STR); TRI_UnlinkFile(tmp); TRI_FreeString(TRI_CORE_MEM_ZONE, tmp); return false; } res = TRI_CLOSE(fd); if (res < 0) { TRI_set_errno(TRI_ERROR_SYS_ERROR); LOG_ERROR("cannot close saved file '%s': '%s'", tmp, TRI_LAST_ERROR_STR); TRI_UnlinkFile(tmp); TRI_FreeString(TRI_CORE_MEM_ZONE, tmp); return false; } res = TRI_RenameFile(tmp, filename); if (res != TRI_ERROR_NO_ERROR) { LOG_ERROR("cannot rename saved file '%s' to '%s': '%s'", tmp, filename, TRI_LAST_ERROR_STR); TRI_UnlinkFile(tmp); TRI_FreeString(TRI_CORE_MEM_ZONE, tmp); return res; } TRI_FreeString(TRI_CORE_MEM_ZONE, tmp); return ok; }