privfile: add forgotten sources

This commit is contained in:
Mirek Kratochvil 2017-10-31 21:25:36 +01:00
parent 8a55068d89
commit 66d5488133
2 changed files with 91 additions and 0 deletions

61
src/privfile.cpp Normal file
View File

@ -0,0 +1,61 @@
/*
* This file is part of Codecrypt.
*
* Copyright (C) 2013-2016 Mirek Kratochvil <exa.exa@gmail.com>
*
* Codecrypt is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Codecrypt is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Codecrypt. If not, see <http://www.gnu.org/licenses/>.
*/
#include "privfile.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/file.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
bool put_private_file (const std::string&fn,
const std::string&contents, bool force_perms)
{
struct stat st;
if (stat (fn.c_str(), &st)) {
if (errno != ENOENT)
return false;
//if it simply doesn't exist, create it
int fd;
fd = creat (fn.c_str(), S_IRUSR | S_IWUSR);
if (fd < 0) return false;
ssize_t res = write (fd, contents.c_str(),
contents.length());
if (close (fd)) return false;
if ( (size_t) res != contents.length()) return false;
} else {
if (!S_ISREG (st.st_mode))
return false;
//remove others' read/write. group r/w is untouched.
if (force_perms && (st.st_mode & 07)) {
if (chmod (fn.c_str(), st.st_mode & ~07))
return false;
}
}
if (access (fn.c_str(), R_OK | W_OK)) return false;
return true;
}

30
src/privfile.h Normal file
View File

@ -0,0 +1,30 @@
/*
* This file is part of Codecrypt.
*
* Copyright (C) 2013-2016 Mirek Kratochvil <exa.exa@gmail.com>
*
* Codecrypt is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Codecrypt is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Codecrypt. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _ccr_privfile_h_
#define _ccr_privfile_h_
#include <string>
bool put_private_file (const std::string&fn,
const std::string&contents,
bool force_permissions);
#endif