bmsd/futil.c

changeset 0
033898178630
child 745
3addb8cfcc3e
equal deleted inserted replaced
-1:000000000000 0:033898178630
1 /*****************************************************************************
2 * Copyright (C) 2017
3 *
4 * Michiel Broek <mbroek at mbse dot eu>
5 *
6 * This file is part of the bms (Brewery Management System)
7 *
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * bms is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with ThermFerm; see the file COPYING. If not, write to the Free
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *****************************************************************************/
22
23 #include "bms.h"
24 #include "futil.h"
25
26
27 /*
28 * Make directory tree, the name must end with a /
29 */
30 int mkdirs(char *name, mode_t mode)
31 {
32 char buf[PATH_MAX], *p, *q;
33 int last = 0, oldmask;
34
35 memset(&buf, 0, sizeof(buf));
36 strncpy(buf, name, sizeof(buf)-1);
37 buf[sizeof(buf)-1] = '\0';
38
39 p = buf+1;
40
41 oldmask = umask(000);
42 while ((q = strchr(p, '/'))) {
43 *q = '\0';
44 mkdir(buf, mode);
45 last = errno;
46 *q = '/';
47 p = q+1;
48 }
49
50 umask(oldmask);
51
52 if ((last == 0) || (last == EEXIST)) {
53 return TRUE;
54 } else {
55 syslog(LOG_NOTICE, "mkdirs(%s)", name);
56 return FALSE;
57 }
58 }
59
60
61
62 /*
63 * Test if the given file exists. The second option is:
64 * R_OK - test for Read rights
65 * W_OK - test for Write rights
66 * X_OK - test for eXecute rights
67 * F_OK - test file presence only
68 */
69 int file_exist(char *path, int mode)
70 {
71 if (access(path, mode) != 0)
72 return errno;
73
74 return 0;
75 }
76
77
78
79 /*
80 * Return size of file, or -1 if file doesn't exist
81 */
82 int file_size(char *path)
83 {
84 static struct stat sb;
85
86 if (stat(path, &sb) == -1)
87 return -1;
88
89 return sb.st_size;
90 }
91

mercurial