chiark / gitweb /
vol_id: fix language in manpage
[elogind.git] / udev / lib / libudev-device-db-write.c
1 /*
2  * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <stddef.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <sys/stat.h>
26
27 #include "udev.h"
28
29 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
30 {
31         size_t start;
32
33         /* translate to location of db file */
34         util_strlcpy(filename, udev_get_dev_path(udev), len);
35         start = util_strlcat(filename, "/.udev/db/", len);
36         util_strlcat(filename, devpath, len);
37         return util_path_encode(&filename[start], len - start);
38 }
39
40 int udev_device_update_db(struct udev_device *udev_device)
41 {
42         struct udev *udev = udev_device_get_udev(udev_device);
43         char filename[UTIL_PATH_SIZE];
44         FILE *f;
45         char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */
46         size_t devlen = strlen(udev_get_dev_path(udev))+1;
47         struct udev_list_entry *list_entry;
48         int ret;
49
50         devpath_to_db_path(udev,
51                            udev_device_get_devpath(udev_device),
52                            filename, sizeof(filename));
53         util_create_path(udev, filename);
54         unlink(filename);
55
56         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
57                 if (udev_list_entry_get_flag(list_entry))
58                         goto file;
59         if (udev_device_get_num_fake_partitions(udev_device) != 0)
60                 goto file;
61         if (udev_device_get_ignore_remove(udev_device))
62                 goto file;
63         if (udev_device_get_devlink_priority(udev_device) != 0)
64                 goto file;
65         if (udev_device_get_event_timeout(udev_device) >= 0)
66                 goto file;
67         if (udev_device_get_watch_handle(udev_device) >= 0)
68                 goto file;
69         if (udev_device_get_devnode(udev_device) == NULL)
70                 goto out;
71
72         /*
73          * if we have only the node and symlinks to store, try not to waste
74          * tmpfs memory -- store values, if they fit, in a symlink target
75          */
76         util_strlcpy(target, &udev_device_get_devnode(udev_device)[devlen], sizeof(target));
77         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
78                 size_t len;
79
80                 util_strlcat(target, " ", sizeof(target));
81                 len = util_strlcat(target, &udev_list_entry_get_name(list_entry)[devlen], sizeof(target));
82                 if (len >= sizeof(target)) {
83                         info(udev, "size of links too large, create file\n");
84                         goto file;
85                 }
86         }
87         info(udev, "create db link (%s)\n", target);
88         udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
89         ret = symlink(target, filename);
90         udev_selinux_resetfscreatecon(udev);
91         if (ret == 0)
92                 goto out;
93 file:
94         f = fopen(filename, "w");
95         if (f == NULL) {
96                 err(udev, "unable to create db file '%s': %m\n", filename);
97                 return -1;
98                 }
99         info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
100
101         if (udev_device_get_devnode(udev_device) != NULL) {
102                 fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
103                 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
104                         fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
105         }
106         if (udev_device_get_devlink_priority(udev_device) != 0)
107                 fprintf(f, "L:%u\n", udev_device_get_devlink_priority(udev_device));
108         if (udev_device_get_event_timeout(udev_device) >= 0)
109                 fprintf(f, "T:%u\n", udev_device_get_event_timeout(udev_device));
110         if (udev_device_get_num_fake_partitions(udev_device) != 0)
111                 fprintf(f, "A:%u\n", udev_device_get_num_fake_partitions(udev_device));
112         if (udev_device_get_ignore_remove(udev_device))
113                 fprintf(f, "R:%u\n", udev_device_get_ignore_remove(udev_device));
114         if (udev_device_get_watch_handle(udev_device) >= 0)
115                 fprintf(f, "W:%u\n", udev_device_get_watch_handle(udev_device));
116         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
117                 if (!udev_list_entry_get_flag(list_entry))
118                         continue;
119                 fprintf(f, "E:%s=%s\n",
120                         udev_list_entry_get_name(list_entry),
121                         udev_list_entry_get_value(list_entry));
122         }
123         fclose(f);
124 out:
125         return 0;
126 }
127
128 int udev_device_delete_db(struct udev_device *udev_device)
129 {
130         char filename[UTIL_PATH_SIZE];
131
132         devpath_to_db_path(udev_device_get_udev(udev_device),
133                            udev_device_get_devpath(udev_device),
134                            filename, sizeof(filename));
135         unlink(filename);
136         return 0;
137 }
138
139 int udev_device_rename_db(struct udev_device *udev_device, const char *devpath_old)
140 {
141         char filename_old[UTIL_PATH_SIZE];
142         char filename[UTIL_PATH_SIZE];
143
144         devpath_to_db_path(udev_device_get_udev(udev_device),
145                            devpath_old,
146                            filename_old, sizeof(filename_old));
147         devpath_to_db_path(udev_device_get_udev(udev_device),
148                            udev_device_get_devpath(udev_device),
149                            filename, sizeof(filename));
150         return rename(filename_old, filename);
151 }