chiark / gitweb /
add and use name_list_cleanup() for cleaning up the string lists
[elogind.git] / udev_utils_file.c
1 /*
2  * udev_utils_file.c - files operations
3  *
4  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include <sys/stat.h>
31 #include <sys/mman.h>
32
33 #include "udev_libc_wrapper.h"
34 #include "udev.h"
35 #include "logging.h"
36 #include "udev_utils.h"
37 #include "list.h"
38
39 int create_path(const char *path)
40 {
41         char p[PATH_SIZE];
42         char *pos;
43         struct stat stats;
44
45         strcpy (p, path);
46         pos = strrchr(p, '/');
47         if (pos == p || pos == NULL)
48                 return 0;
49
50         while (pos[-1] == '/')
51                 pos--;
52
53         pos[0] = '\0';
54
55         dbg("stat '%s'\n", p);
56         if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
57                 return 0;
58
59         if (create_path (p) != 0)
60                 return -1;
61
62         dbg("mkdir '%s'\n", p);
63         return mkdir(p, 0755);
64 }
65
66 /* Reset permissions on the device node, before unlinking it to make sure,
67  * that permisions of possible hard links will be removed too.
68  */
69 int unlink_secure(const char *filename)
70 {
71         int retval;
72
73         retval = chown(filename, 0, 0);
74         if (retval)
75                 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
76
77         retval = chmod(filename, 0000);
78         if (retval)
79                 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
80
81         retval = unlink(filename);
82         if (errno == ENOENT)
83                 retval = 0;
84
85         if (retval)
86                 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
87
88         return retval;
89 }
90
91 int file_map(const char *filename, char **buf, size_t *bufsize)
92 {
93         struct stat stats;
94         int fd;
95
96         fd = open(filename, O_RDONLY);
97         if (fd < 0) {
98                 return -1;
99         }
100
101         if (fstat(fd, &stats) < 0) {
102                 close(fd);
103                 return -1;
104         }
105
106         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
107         if (*buf == MAP_FAILED) {
108                 close(fd);
109                 return -1;
110         }
111         *bufsize = stats.st_size;
112
113         close(fd);
114
115         return 0;
116 }
117
118 void file_unmap(void *buf, size_t bufsize)
119 {
120         munmap(buf, bufsize);
121 }
122
123 /* return number of chars until the next newline, skip escaped newline */
124 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
125 {
126         int escape = 0;
127         size_t count;
128
129         for (count = cur; count < buflen; count++) {
130                 if (!escape && buf[count] == '\n')
131                         break;
132
133                 if (buf[count] == '\\')
134                         escape = 1;
135                 else
136                         escape = 0;
137         }
138
139         return count - cur;
140 }