chiark / gitweb /
rules: fix typo in ide cd rule
[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_devnode(udev_device) == NULL)
68                 goto out;
69
70         /*
71          * if we have only the node and symlinks to store, try not to waste
72          * tmpfs memory -- store values, if they fit, in a symlink target
73          */
74         util_strlcpy(target, &udev_device_get_devnode(udev_device)[devlen], sizeof(target));
75         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
76                 size_t len;
77
78                 util_strlcat(target, " ", sizeof(target));
79                 len = util_strlcat(target, &udev_list_entry_get_name(list_entry)[devlen], sizeof(target));
80                 if (len >= sizeof(target)) {
81                         info(udev, "size of links too large, create file\n");
82                         goto file;
83                 }
84         }
85         info(udev, "create db link (%s)\n", target);
86         udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
87         ret = symlink(target, filename);
88         udev_selinux_resetfscreatecon(udev);
89         if (ret == 0)
90                 goto out;
91 file:
92         f = fopen(filename, "w");
93         if (f == NULL) {
94                 err(udev, "unable to create db file '%s': %m\n", filename);
95                 return -1;
96                 }
97         info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
98
99         if (udev_device_get_devnode(udev_device) != NULL) {
100                 fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
101                 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
102                         fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
103         }
104         if (udev_device_get_devlink_priority(udev_device) != 0)
105                 fprintf(f, "L:%u\n", udev_device_get_devlink_priority(udev_device));
106         if (udev_device_get_event_timeout(udev_device) >= 0)
107                 fprintf(f, "T:%u\n", udev_device_get_event_timeout(udev_device));
108         if (udev_device_get_num_fake_partitions(udev_device) != 0)
109                 fprintf(f, "A:%u\n", udev_device_get_num_fake_partitions(udev_device));
110         if (udev_device_get_ignore_remove(udev_device))
111                 fprintf(f, "R:%u\n", udev_device_get_ignore_remove(udev_device));
112         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
113                 if (!udev_list_entry_get_flag(list_entry))
114                         continue;
115                 fprintf(f, "E:%s=%s\n",
116                         udev_list_entry_get_name(list_entry),
117                         udev_list_entry_get_value(list_entry));
118         }
119         fclose(f);
120 out:
121         return 0;
122 }
123
124 int udev_device_delete_db(struct udev_device *udev_device)
125 {
126         char filename[UTIL_PATH_SIZE];
127
128         devpath_to_db_path(udev_device_get_udev(udev_device),
129                            udev_device_get_devpath(udev_device),
130                            filename, sizeof(filename));
131         unlink(filename);
132         return 0;
133 }
134
135 int udev_device_rename_db(struct udev_device *udev_device, const char *devpath_old)
136 {
137         char filename_old[UTIL_PATH_SIZE];
138         char filename[UTIL_PATH_SIZE];
139
140         devpath_to_db_path(udev_device_get_udev(udev_device),
141                            devpath_old,
142                            filename_old, sizeof(filename_old));
143         devpath_to_db_path(udev_device_get_udev(udev_device),
144                            udev_device_get_devpath(udev_device),
145                            filename, sizeof(filename));
146         return rename(filename_old, filename);
147 }