#pragma once
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include "android-base/macros.h"
#include "android-base/off64_t.h"
#include "android-base/unique_fd.h"
#if !defined(_WIN32) && !defined(O_BINARY)
#define O_BINARY 0
#endif
#if defined(_WIN32) && !defined(O_CLOEXEC)
#define O_CLOEXEC O_NOINHERIT
#endif
class TemporaryFile {
public:
TemporaryFile();
explicit TemporaryFile(const std::string& tmp_dir);
~TemporaryFile();
int release();
void DoNotRemove() { remove_file_ = false; }
int fd;
char path[1024];
private:
void init(const std::string& tmp_dir);
bool remove_file_ = true;
DISALLOW_COPY_AND_ASSIGN(TemporaryFile);
};
class TemporaryDir {
public:
TemporaryDir();
~TemporaryDir();
void DoNotRemove() { remove_dir_and_contents_ = false; }
char path[1024];
private:
bool init(const std::string& tmp_dir);
bool remove_dir_and_contents_ = true;
DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
};
namespace android {
namespace base {
bool ReadFdToString(borrowed_fd fd, std::string* content);
bool ReadFileToString(const std::string& path, std::string* content,
bool follow_symlinks = false);
bool WriteStringToFile(const std::string& content, const std::string& path,
bool follow_symlinks = false);
bool WriteStringToFd(const std::string& content, borrowed_fd fd);
#if !defined(_WIN32)
bool WriteStringToFile(const std::string& content, const std::string& path,
mode_t mode, uid_t owner, gid_t group,
bool follow_symlinks = false);
#endif
bool ReadFully(borrowed_fd fd, void* data, size_t byte_count);
bool ReadFullyAtOffset(borrowed_fd fd, void* data, size_t byte_count, off64_t offset);
bool WriteFully(borrowed_fd fd, const void* data, size_t byte_count);
bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr);
#if !defined(_WIN32)
bool Realpath(const std::string& path, std::string* result);
bool Readlink(const std::string& path, std::string* result);
#endif
std::string GetExecutablePath();
std::string GetExecutableDirectory();
std::string Basename(const std::string& path);
std::string Dirname(const std::string& path);
} }