| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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> |
| 27 |
#include <errno.h> |
| 28 |
|
| 29 |
|
| 30 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|