2008-02-27 20:56:47 +03:00
|
|
|
/*
|
|
|
|
* cynapses libc functions
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 by Andreas Schneider <mail@cynapses.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* vim: ts=2 sw=2 et cindent
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "c_file.h"
|
|
|
|
#include "c_string.h"
|
|
|
|
|
2012-02-20 21:21:22 +04:00
|
|
|
#include "c_private.h"
|
|
|
|
|
2008-02-27 20:56:47 +03:00
|
|
|
/* check if path is a file */
|
|
|
|
int c_isfile(const char *path) {
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (lstat (path, &sb) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy file from src to dst, overwrites dst */
|
|
|
|
int c_copy(const char* src, const char *dst, mode_t mode) {
|
2008-05-08 18:00:05 +04:00
|
|
|
int srcfd = -1;
|
|
|
|
int dstfd = -1;
|
|
|
|
int rc = -1;
|
2008-02-27 20:56:47 +03:00
|
|
|
ssize_t bread, bwritten;
|
|
|
|
struct stat sb;
|
2008-06-02 18:05:40 +04:00
|
|
|
char buf[BUFFER_SIZE];
|
2008-02-27 20:56:47 +03:00
|
|
|
|
|
|
|
if (c_streq(src, dst)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lstat(src, &sb) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(sb.st_mode)) {
|
|
|
|
errno = EISDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == 0) {
|
|
|
|
mode = sb.st_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lstat(dst, &sb) == 0) {
|
|
|
|
if (S_ISDIR(sb.st_mode)) {
|
|
|
|
errno = EISDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((srcfd = open(src, O_RDONLY, 0)) < 0) {
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dstfd = open(dst, O_CREAT|O_WRONLY|O_TRUNC, mode)) < 0) {
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2008-06-02 18:05:40 +04:00
|
|
|
bread = read(srcfd, buf, BUFFER_SIZE);
|
2008-02-27 20:56:47 +03:00
|
|
|
if (bread == 0) {
|
|
|
|
/* done */
|
|
|
|
break;
|
|
|
|
} else if (bread < 0) {
|
|
|
|
errno = ENODATA;
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bwritten = write(dstfd, buf, bread);
|
|
|
|
if (bwritten < 0) {
|
|
|
|
errno = ENODATA;
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bread != bwritten) {
|
|
|
|
errno = EFAULT;
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
out:
|
|
|
|
close(srcfd);
|
|
|
|
close(dstfd);
|
|
|
|
if (rc < 0) {
|
|
|
|
unlink(dst);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|