diff --git a/src/keyring.cpp b/src/keyring.cpp index eb11e97..de0d30c 100644 --- a/src/keyring.cpp +++ b/src/keyring.cpp @@ -283,6 +283,8 @@ static std::string get_user_dir() return "." CCR_CONFDIR; //fallback for absolutely desolate systems } +#include "privfile.h" +#include #include #include #include @@ -290,8 +292,6 @@ static std::string get_user_dir() #include #include -#include - /* * prepares the user directory with empty files and similar stuff. * @@ -302,33 +302,12 @@ static std::string get_user_dir() static bool ensure_empty_sencode_file (const std::string&fn, const std::string&ident) { - struct stat st; - if (stat (fn.c_str(), &st)) { - if (errno != ENOENT) - return false; + sencode_list l; + sencode_bytes b (ident); + l.items.push_back (&b); + std::string emptyfile = l.encode(); - //if it simply doesn't exist, create it - sencode_list l; - sencode_bytes b (ident); - l.items.push_back (&b); - std::string emptyfile = l.encode(); - - int fd; - fd = creat (fn.c_str(), S_IRUSR | S_IWUSR); - if (fd < 0) return false; - ssize_t res = write (fd, emptyfile.c_str(), - emptyfile.length()); - if (close (fd)) return false; - if ( (size_t) res != emptyfile.length()) return false; - - } else { - if (!S_ISREG (st.st_mode)) - return false; - } - - if (access (fn.c_str(), R_OK | W_OK)) return false; - - return true; + return put_private_file (fn, emptyfile, true); } static bool prepare_user_dir (const std::string&dir) diff --git a/src/symkey.cpp b/src/symkey.cpp index 57f6922..3675c9e 100644 --- a/src/symkey.cpp +++ b/src/symkey.cpp @@ -20,10 +20,11 @@ #include "symkey.h" -#include "sc.h" #include "hash.h" -#include "str_match.h" #include "iohelpers.h" +#include "privfile.h" +#include "sc.h" +#include "str_match.h" #include @@ -176,14 +177,6 @@ bool symkey::save (const std::string&fn, const std::string&withlock, data = tmp; } - std::ofstream sk_out; - sk_out.open (fn == "-" ? "/dev/stdout" : fn.c_str(), - std::ios::out | std::ios::binary); - if (!sk_out) { - err ("error: can't open symkey file for writing"); - return false; - } - if (armor) { std::vector parts; parts.resize (1); @@ -191,18 +184,13 @@ bool symkey::save (const std::string&fn, const std::string&withlock, data = envelope_format (ENVELOPE_SYMKEY, parts, r); } - sk_out << data; - if (!sk_out.good()) { + bool to_stdout = (fn == "-"); + if (!put_private_file (to_stdout ? "/dev/stdout" : fn, + data, !to_stdout)) { err ("error: can't write to symkey file"); return false; } - sk_out.close(); - if (!sk_out.good()) { - err ("error: couldn't close symkey file"); - return false; - } - return true; }