blob: 8e46e0edef1f04b6929496102aa471e3c5afcfa8 [file] [log] [blame]
Benny Prijono40ce3fb2005-11-07 18:14:08 +00001/* $Id$ */
2#include <pj/file_access.h>
3#include <pj/assert.h>
4#include <pj/errno.h>
5
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <unistd.h>
9#include <stdio.h> /* rename() */
10#include <errno.h>
11
12/*
13 * pj_file_exists()
14 */
15PJ_DEF(pj_bool_t) pj_file_exists(const char *filename)
16{
17 struct stat buf;
18
19 PJ_ASSERT_RETURN(filename, 0);
20
21 if (stat(filename, &buf) != 0)
22 return 0;
23
24 return PJ_TRUE;
25}
26
27
28/*
29 * pj_file_size()
30 */
31PJ_DEF(pj_off_t) pj_file_size(const char *filename)
32{
33 struct stat buf;
34
35 PJ_ASSERT_RETURN(filename, -1);
36
37 if (stat(filename, &buf) != 0)
38 return -1;
39
40 return buf.st_size;
41}
42
43
44/*
45 * pj_file_delete()
46 */
47PJ_DEF(pj_status_t) pj_file_delete(const char *filename)
48{
49 PJ_ASSERT_RETURN(filename, PJ_EINVAL);
50
51 if (unlink(filename)!=0) {
52 return PJ_RETURN_OS_ERROR(errno);
53 }
54 return PJ_SUCCESS;
55}
56
57
58/*
59 * pj_file_move()
60 */
61PJ_DEF(pj_status_t) pj_file_move( const char *oldname, const char *newname)
62{
63 PJ_ASSERT_RETURN(oldname && newname, PJ_EINVAL);
64
65 if (rename(oldname, newname) != 0) {
66 return PJ_RETURN_OS_ERROR(errno);
67 }
68 return PJ_SUCCESS;
69}
70
71
72/*
73 * pj_file_getstat()
74 */
75PJ_DEF(pj_status_t) pj_file_getstat(const char *filename,
76 pj_file_stat *statbuf)
77{
78 struct stat buf;
79
80 PJ_ASSERT_RETURN(filename && statbuf, PJ_EINVAL);
81
82 if (stat(filename, &buf) != 0) {
83 return PJ_RETURN_OS_ERROR(errno);
84 }
85
86 statbuf->size = buf.st_size;
87 statbuf->ctime.sec = buf.st_ctime;
88 statbuf->ctime.msec = 0;
89 statbuf->mtime.sec = buf.st_mtime;
90 statbuf->mtime.msec = 0;
91 statbuf->atime.sec = buf.st_atime;
92 statbuf->atime.msec = 0;
93
94 return PJ_SUCCESS;
95}
96