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
|
|
|
|
*/
|
|
|
|
|
2012-03-06 17:20:08 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windef.h>
|
|
|
|
#include <winbase.h>
|
|
|
|
#endif
|
|
|
|
|
2008-02-27 20:56:47 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2013-03-22 13:23:17 +04:00
|
|
|
#include <string.h>
|
2008-02-27 20:56:47 +03:00
|
|
|
|
|
|
|
#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) {
|
2012-03-07 19:30:55 +04:00
|
|
|
csync_stat_t sb;
|
2012-12-07 16:02:46 +04:00
|
|
|
_TCHAR *wpath = c_multibyte(path);
|
2012-05-09 18:55:56 +04:00
|
|
|
int re = _tstat(wpath, &sb);
|
|
|
|
c_free_multibyte(wpath);
|
|
|
|
|
|
|
|
if (re< 0) {
|
2008-02-27 20:56:47 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-19 06:07:51 +04:00
|
|
|
#ifdef __unix__
|
2008-02-27 20:56:47 +03:00
|
|
|
if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode)) {
|
2012-02-19 06:07:51 +04:00
|
|
|
#else
|
|
|
|
if (S_ISREG(sb.st_mode)) {
|
|
|
|
#endif
|
2008-02-27 20:56:47 +03:00
|
|
|
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;
|
2012-03-07 19:30:55 +04:00
|
|
|
csync_stat_t sb;
|
2012-10-19 22:27:51 +04:00
|
|
|
char buf[4096];
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-03-06 17:20:08 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
if(src && dst) {
|
2012-12-07 16:02:46 +04:00
|
|
|
_TCHAR *wsrc = c_multibyte(src);
|
|
|
|
_TCHAR *wdst = c_multibyte(dst);
|
2012-06-13 19:32:13 +04:00
|
|
|
if (CopyFileW(wsrc, wdst, FALSE)) {
|
|
|
|
rc = 0;
|
2012-03-06 17:20:08 +04:00
|
|
|
}
|
2012-06-13 19:32:13 +04:00
|
|
|
c_free_multibyte(wsrc);
|
|
|
|
c_free_multibyte(wdst);
|
2012-03-06 17:20:08 +04:00
|
|
|
|
2012-06-13 19:32:13 +04:00
|
|
|
if( rc < 0 ) {
|
|
|
|
errno = GetLastError();
|
|
|
|
}
|
2012-03-06 17:20:08 +04:00
|
|
|
}
|
2012-11-07 20:03:01 +04:00
|
|
|
return rc;
|
2012-05-09 18:55:56 +04:00
|
|
|
#else
|
2012-03-06 17:20:08 +04:00
|
|
|
|
2012-05-09 18:55:56 +04:00
|
|
|
/* Win32 does not come here. */
|
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 (;;) {
|
2012-10-19 22:27:51 +04:00
|
|
|
bread = read(srcfd, buf, sizeof(buf));
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 17:20:08 +04:00
|
|
|
#ifdef __unix__
|
|
|
|
fsync(dstfd);
|
|
|
|
#endif
|
|
|
|
|
2008-02-27 20:56:47 +03:00
|
|
|
rc = 0;
|
|
|
|
out:
|
2012-10-19 21:42:04 +04:00
|
|
|
if (srcfd > 0) {
|
|
|
|
close(srcfd);
|
|
|
|
}
|
|
|
|
if (dstfd > 0) {
|
|
|
|
close(dstfd);
|
|
|
|
}
|
2008-02-27 20:56:47 +03:00
|
|
|
if (rc < 0) {
|
|
|
|
unlink(dst);
|
|
|
|
}
|
|
|
|
return rc;
|
2012-05-09 18:55:56 +04:00
|
|
|
#endif
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2013-03-22 13:23:17 +04:00
|
|
|
int c_compare_file( const char *f1, const char *f2 ) {
|
|
|
|
_TCHAR *wf1, *wf2;
|
|
|
|
int fd1 = -1, fd2 = -1;
|
2013-03-22 13:23:17 +04:00
|
|
|
size_t size1, size2;
|
2013-03-22 13:23:17 +04:00
|
|
|
char buffer1[BUFFER_SIZE];
|
|
|
|
char buffer2[BUFFER_SIZE];
|
|
|
|
csync_stat_t stat1;
|
|
|
|
csync_stat_t stat2;
|
|
|
|
|
2013-03-22 13:23:17 +04:00
|
|
|
int rc = -1;
|
2013-03-22 13:23:17 +04:00
|
|
|
|
2013-03-22 13:23:17 +04:00
|
|
|
if(f1 == NULL || f2 == NULL) return -1;
|
2013-03-22 13:23:17 +04:00
|
|
|
|
|
|
|
wf1 = c_multibyte(f1);
|
2013-03-22 13:23:17 +04:00
|
|
|
if(wf1 == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-03-22 13:23:17 +04:00
|
|
|
wf2 = c_multibyte(f2);
|
2013-03-22 13:23:17 +04:00
|
|
|
if(wf2 == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-03-22 13:23:17 +04:00
|
|
|
|
|
|
|
/* compare size first. */
|
2013-03-22 13:23:17 +04:00
|
|
|
rc = _tstat(wf1, &stat1);
|
|
|
|
if(rc< 0) {
|
2013-03-22 13:23:17 +04:00
|
|
|
goto out;
|
|
|
|
}
|
2013-03-22 13:23:17 +04:00
|
|
|
rc = _tstat(wf2, &stat2);
|
|
|
|
if(rc < 0) {
|
2013-03-22 13:23:17 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if sizes are different, the files can not be equal. */
|
|
|
|
if( stat1.st_size != stat2.st_size ) {
|
2013-03-22 13:23:17 +04:00
|
|
|
rc = 0;
|
2013-03-22 13:23:17 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
_fmode = _O_BINARY;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fd1 = _topen(wf1, O_RDONLY);
|
2013-03-22 13:23:17 +04:00
|
|
|
if(fd1 < 0) {
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-03-22 13:23:17 +04:00
|
|
|
fd2 = _topen(wf2, O_RDONLY);
|
2013-03-22 13:23:17 +04:00
|
|
|
if(fd2 < 0) {
|
|
|
|
rc = -1;
|
2013-03-22 13:23:17 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while( (size1 = read(fd1, buffer1, BUFFER_SIZE)) > 0 ) {
|
|
|
|
size2 = read( fd2, buffer2, BUFFER_SIZE );
|
|
|
|
|
|
|
|
if( size1 != size2 ) {
|
2013-03-22 13:23:17 +04:00
|
|
|
rc = 0;
|
2013-03-22 13:23:17 +04:00
|
|
|
goto out;
|
|
|
|
}
|
2013-03-22 13:23:17 +04:00
|
|
|
if(memcmp(buffer1, buffer2, size1) != 0) {
|
|
|
|
/* buffers are different */
|
|
|
|
rc = 0;
|
|
|
|
goto out;
|
2013-03-22 13:23:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 13:23:17 +04:00
|
|
|
rc = 1;
|
2013-03-22 13:23:17 +04:00
|
|
|
|
|
|
|
out:
|
|
|
|
|
2013-03-22 13:23:17 +04:00
|
|
|
if(fd1 > -1) close(fd1);
|
|
|
|
if(fd2 > -1) close(fd2);
|
2013-03-22 13:23:17 +04:00
|
|
|
|
2013-03-22 13:23:17 +04:00
|
|
|
c_free_multibyte(wf1);
|
|
|
|
c_free_multibyte(wf2);
|
|
|
|
return rc;
|
2013-03-22 13:23:17 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|