blob: 2a9c591ebe43b0566958e9cf65887258086bb490 [file] [log] [blame]
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SRC_SYS_PKG_LIB_FAR_CPP_FILE_OPERATIONS_H_
#define SRC_SYS_PKG_LIB_FAR_CPP_FILE_OPERATIONS_H_
#include <vector>
#include "src/lib/files/file_descriptor.h"
namespace archive {
template <typename T>
bool ReadObject(int fd, T* object) {
char buffer[sizeof(T)];
ssize_t actual = fxl::ReadFileDescriptor(fd, buffer, sizeof(T));
if (actual < 0 || static_cast<size_t>(actual) != sizeof(T))
return false;
memcpy(object, buffer, sizeof(T));
return true;
}
template <typename T>
bool WriteObject(int fd, const T& object) {
char buffer[sizeof(T)];
memcpy(buffer, &object, sizeof(T));
return fxl::WriteFileDescriptor(fd, buffer, sizeof(T));
}
template <typename T>
bool ReadVector(int fd, std::vector<T>* vector) {
size_t requested = vector->size() * sizeof(T);
char* buffer = reinterpret_cast<char*>(vector->data());
ssize_t actual = fxl::ReadFileDescriptor(fd, buffer, requested);
return actual >= 0 && static_cast<size_t>(actual) == requested;
}
template <typename T>
bool WriteVector(int fd, const std::vector<T>& vector) {
size_t requested = vector.size() * sizeof(T);
const char* buffer = reinterpret_cast<const char*>(vector.data());
return fxl::WriteFileDescriptor(fd, buffer, requested);
}
bool CopyPathToFile(const char* src_path, int dst_fd, uint64_t length);
bool CopyFileToPath(int src_fd, const char* dst_path, uint64_t length);
bool CopyFileToFile(int src_fd, int dst_fd, uint64_t length);
} // namespace archive
#endif // SRC_SYS_PKG_LIB_FAR_CPP_FILE_OPERATIONS_H_