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