X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcfileutil.c;h=ea48479bde029971bfc15d6e37d695f73545031c;hb=e7b6c157b80152bf9fb9266e6bdd93f9fb0db776;hp=30b2a5e5ce2001df916e0a0ebb7b8f38a309af49;hpb=40f8443d8d3a6577336ee66d18e04d9ac4d956bb;p=silc.git diff --git a/lib/silcutil/silcfileutil.c b/lib/silcutil/silcfileutil.c index 30b2a5e5..ea48479b 100644 --- a/lib/silcutil/silcfileutil.c +++ b/lib/silcutil/silcfileutil.c @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 1997 - 2005 Pekka Riikonen + Copyright (C) 1997 - 2007 Pekka Riikonen 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 @@ -34,6 +34,8 @@ int silc_file_open(const char *filename, int flags) int silc_file_open_mode(const char *filename, int flags, int mode) { int fd = open(filename, flags, mode); + if (fd < 0) + silc_set_errno_posix(errno); return fd; } @@ -41,21 +43,30 @@ int silc_file_open_mode(const char *filename, int flags, int mode) int silc_file_read(int fd, unsigned char *buf, SilcUInt32 buf_len) { - return read(fd, (void *)buf, buf_len); + int ret = read(fd, (void *)buf, buf_len); + if (ret < 0) + silc_set_errno_posix(errno); + return ret; } /* Writes `buffer' of length of `len' to file descriptor `fd'. */ int silc_file_write(int fd, const char *buffer, SilcUInt32 len) { - return write(fd, (const void *)buffer, len); + int ret = write(fd, (const void *)buffer, len); + if (ret < 0) + silc_set_errno_posix(errno); + return ret; } /* Closes file descriptor */ int silc_file_close(int fd) { - return close(fd); + int ret = close(fd); + if (ret < 0) + silc_set_errno_posix(errno); + return ret; } /* Writes a buffer to the file. */ @@ -72,19 +83,22 @@ int silc_file_writefile(const char *filename, const char *buffer, if ((fd = open(filename, flags, 0644)) == -1) { SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename, - strerror(errno))); + silc_errno_string(silc_errno))); return -1; } if (silc_file_write(fd, buffer, len) == -1) { - SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno))); + SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, + silc_errno_string(silc_errno))); silc_file_close(fd); return -1; } - silc_file_close(fd); +#ifdef SILC_UNIX + fsync(fd); +#endif /* SILC_UNIX */ - return 0; + return silc_file_close(fd); } /* Writes a buffer to the file. If the file is created specific mode is @@ -102,61 +116,62 @@ int silc_file_writefile_mode(const char *filename, const char *buffer, if ((fd = open(filename, flags, mode)) == -1) { SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename, - strerror(errno))); + silc_errno_string(silc_errno))); return -1; } if ((silc_file_write(fd, buffer, len)) == -1) { - SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno))); + SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, + silc_errno_string(silc_errno))); silc_file_close(fd); return -1; } - silc_file_close(fd); +#ifdef SILC_UNIX + fsync(fd); +#endif /* SILC_UNIX */ - return 0; + return silc_file_close(fd); } /* Reads a file to a buffer. The allocated buffer is returned. Length of the file read is returned to the return_len argument. */ -char *silc_file_readfile(const char *filename, SilcUInt32 *return_len) +char *silc_file_readfile(const char *filename, SilcUInt32 *return_len, + SilcStack stack) { int fd; - char *buffer; + unsigned char *buffer; int filelen; fd = silc_file_open(filename, O_RDONLY); if (fd < 0) { - if (errno == ENOENT) + if (silc_errno == SILC_ERR_NO_SUCH_FILE) return NULL; - SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno))); + SILC_LOG_ERROR(("Cannot open file %s: %s", filename, + silc_errno_string(silc_errno))); return NULL; } filelen = lseek(fd, (off_t)0L, SEEK_END); if (filelen < 0) { + silc_set_errno_posix(errno); silc_file_close(fd); return NULL; } if (lseek(fd, (off_t)0L, SEEK_SET) < 0) { + silc_set_errno_posix(errno); silc_file_close(fd); return NULL; } - if (filelen < 0) { - SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno))); - silc_file_close(fd); - return NULL; - } - - buffer = silc_calloc(filelen + 1, sizeof(char)); + buffer = silc_calloc(filelen + 1, sizeof(*buffer)); if ((silc_file_read(fd, buffer, filelen)) == -1) { memset(buffer, 0, sizeof(buffer)); silc_file_close(fd); SILC_LOG_ERROR(("Cannot read from file %s: %s", filename, - strerror(errno))); + silc_errno_string(silc_errno))); return NULL; } @@ -166,7 +181,7 @@ char *silc_file_readfile(const char *filename, SilcUInt32 *return_len) if (return_len) *return_len = filelen; - return buffer; + return (char *)buffer; } /* Returns the size of `filename'. Returns 0 on error. */ @@ -176,13 +191,19 @@ SilcUInt64 silc_file_size(const char *filename) int ret; struct stat stats; -#ifndef SILC_WIN32 +#ifdef SILC_WIN32 + ret = stat(filename, &stats); +#endif /* SILC_WIN32 */ +#ifdef SILC_UNIX ret = lstat(filename, &stats); -#else +#endif /* SILC_UNIX */ +#ifdef SILC_SYMBIAN ret = stat(filename, &stats); -#endif - if (ret < 0) +#endif /* SILC_SYMBIAN */ + if (ret < 0) { + silc_set_errno_posix(errno); return 0; + } return (SilcUInt64)stats.st_size; }