Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame^] | 1 | /* $Id$ */
|
| 2 | /*
|
| 3 | * PJLIB - PJ Foundation Library
|
| 4 | * (C)2003-2005 Benny Prijono <bennylp@bulukucing.org>
|
| 5 | *
|
| 6 | * Author:
|
| 7 | * Benny Prijono <bennylp@bulukucing.org>
|
| 8 | *
|
| 9 | * This library is free software; you can redistribute it and/or
|
| 10 | * modify it under the terms of the GNU Lesser General Public
|
| 11 | * License as published by the Free Software Foundation; either
|
| 12 | * version 2.1 of the License, or (at your option) any later version.
|
| 13 | *
|
| 14 | * This library is distributed in the hope that it will be useful,
|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 17 | * Lesser General Public License for more details.
|
| 18 | *
|
| 19 | * You should have received a copy of the GNU Lesser General Public
|
| 20 | * License along with this library; if not, write to the Free Software
|
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 22 | */
|
| 23 | #include <pj/file_access.h>
|
| 24 | #include <pj/assert.h>
|
| 25 | #include <pj/errno.h>
|
| 26 |
|
| 27 | #include <sys/types.h>
|
| 28 | #include <sys/stat.h>
|
| 29 | #include <unistd.h>
|
| 30 | #include <stdio.h> /* rename() */
|
| 31 | #include <errno.h>
|
| 32 |
|
| 33 | /*
|
| 34 | * pj_file_exists()
|
| 35 | */
|
| 36 | PJ_DEF(pj_bool_t) pj_file_exists(const char *filename)
|
| 37 | {
|
| 38 | struct stat buf;
|
| 39 |
|
| 40 | PJ_ASSERT_RETURN(filename, 0);
|
| 41 |
|
| 42 | if (stat(filename, &buf) != 0)
|
| 43 | return 0;
|
| 44 |
|
| 45 | return PJ_TRUE;
|
| 46 | }
|
| 47 |
|
| 48 |
|
| 49 | /*
|
| 50 | * pj_file_size()
|
| 51 | */
|
| 52 | PJ_DEF(pj_off_t) pj_file_size(const char *filename)
|
| 53 | {
|
| 54 | struct stat buf;
|
| 55 |
|
| 56 | PJ_ASSERT_RETURN(filename, -1);
|
| 57 |
|
| 58 | if (stat(filename, &buf) != 0)
|
| 59 | return -1;
|
| 60 |
|
| 61 | return buf.st_size;
|
| 62 | }
|
| 63 |
|
| 64 |
|
| 65 | /*
|
| 66 | * pj_file_delete()
|
| 67 | */
|
| 68 | PJ_DEF(pj_status_t) pj_file_delete(const char *filename)
|
| 69 | {
|
| 70 | PJ_ASSERT_RETURN(filename, PJ_EINVAL);
|
| 71 |
|
| 72 | if (unlink(filename)!=0) {
|
| 73 | return PJ_RETURN_OS_ERROR(errno);
|
| 74 | }
|
| 75 | return PJ_SUCCESS;
|
| 76 | }
|
| 77 |
|
| 78 |
|
| 79 | /*
|
| 80 | * pj_file_move()
|
| 81 | */
|
| 82 | PJ_DEF(pj_status_t) pj_file_move( const char *oldname, const char *newname)
|
| 83 | {
|
| 84 | PJ_ASSERT_RETURN(oldname && newname, PJ_EINVAL);
|
| 85 |
|
| 86 | if (rename(oldname, newname) != 0) {
|
| 87 | return PJ_RETURN_OS_ERROR(errno);
|
| 88 | }
|
| 89 | return PJ_SUCCESS;
|
| 90 | }
|
| 91 |
|
| 92 |
|
| 93 | /*
|
| 94 | * pj_file_getstat()
|
| 95 | */
|
| 96 | PJ_DEF(pj_status_t) pj_file_getstat(const char *filename,
|
| 97 | pj_file_stat *statbuf)
|
| 98 | {
|
| 99 | struct stat buf;
|
| 100 |
|
| 101 | PJ_ASSERT_RETURN(filename && statbuf, PJ_EINVAL);
|
| 102 |
|
| 103 | if (stat(filename, &buf) != 0) {
|
| 104 | return PJ_RETURN_OS_ERROR(errno);
|
| 105 | }
|
| 106 |
|
| 107 | statbuf->size = buf.st_size;
|
| 108 | statbuf->ctime.sec = buf.st_ctime;
|
| 109 | statbuf->ctime.msec = 0;
|
| 110 | statbuf->mtime.sec = buf.st_mtime;
|
| 111 | statbuf->mtime.msec = 0;
|
| 112 | statbuf->atime.sec = buf.st_atime;
|
| 113 | statbuf->atime.msec = 0;
|
| 114 |
|
| 115 | return PJ_SUCCESS;
|
| 116 | }
|
| 117 |
|