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