tzpfms/src/fd.hpp
2023-11-25 17:44:40 +01:00

40 lines
1.0 KiB
C++

/* SPDX-License-Identifier: MIT */
#pragma once
#include "common.hpp"
#include <stdint.h>
#include <unistd.h>
template <class F>
int with_stdin_at(int fd, F && what) {
auto stdin_saved = dup(0);
quickscope_wrapper stdin_saved_deleter{[=] { close(stdin_saved); }};
dup2(fd, 0);
clearerr(stdin);
if(int ret = what()) {
dup2(stdin_saved, 0);
return ret;
}
dup2(stdin_saved, 0);
return 0;
}
/// with_len may not exceed pipe capacity (64k by default)
extern int filled_fd(int & fd, const void * with, size_t with_len);
/// Write exactly len bytes from data into path, or error
extern int write_exact(const char * path, const void * data, size_t len, mode_t mode);
/// Prompt for passphrase for whom the user knows, up to max_len bytes
extern int read_known_passphrase(const char * whom, uint8_t *& buf, size_t & len_out, size_t max_len = SIZE_MAX);
/// Prompt twice for passphrase for whom the user is setting
extern int read_new_passphrase(const char * whom, uint8_t *& buf, size_t & len_out, size_t max_len = SIZE_MAX);