Support tabs as seaprators when parsing fs config
[project/make_ext4fs.git] / canned_fs_config.c
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdlib.h>
22
23 #include "canned_fs_config.h"
24
25 typedef struct {
26         const char* path;
27         unsigned uid;
28         unsigned gid;
29         unsigned mode;
30         uint64_t capabilities;
31 } Path;
32
33 static Path* canned_data = NULL;
34 static int canned_alloc = 0;
35 static int canned_used = 0;
36
37 static int path_compare(const void* a, const void* b) {
38         return strcmp(((Path*)a)->path, ((Path*)b)->path);
39 }
40
41 int load_canned_fs_config(const char* fn) {
42         FILE* f = fopen(fn, "r");
43         if (f == NULL) {
44                 fprintf(stderr, "failed to open %s: %s\n", fn, strerror(errno));
45                 return -1;
46         }
47
48         char line[PATH_MAX + 200];
49         while (fgets(line, sizeof(line), f)) {
50                 while (canned_used >= canned_alloc) {
51                         canned_alloc = (canned_alloc+1) * 2;
52                         canned_data = (Path*) realloc(canned_data, canned_alloc * sizeof(Path));
53                 }
54                 Path* p = canned_data + canned_used;
55                 p->path = strdup(strtok(line, " \t"));
56                 p->uid = atoi(strtok(NULL, " \t"));
57                 p->gid = atoi(strtok(NULL, " \t"));
58                 p->mode = strtol(strtok(NULL, " \t"), NULL, 8);   // mode is in octal
59                 p->capabilities = 0;
60
61                 char* token = NULL;
62                 do {
63                         token = strtok(NULL, " \t");
64                         if (token && strncmp(token, "capabilities=", 13) == 0) {
65                                 p->capabilities = strtoll(token+13, NULL, 0);
66                                 break;
67                         }
68                 } while (token);
69
70                 canned_used++;
71         }
72
73         fclose(f);
74
75         qsort(canned_data, canned_used, sizeof(Path), path_compare);
76         printf("loaded %d fs_config entries\n", canned_used);
77
78         return 0;
79 }
80
81 int canned_fs_config(const char* path, int dir,
82                                           unsigned* uid, unsigned* gid, unsigned* mode, uint64_t* capabilities) {
83         Path key;
84         key.path = path;
85         Path* p = (Path*) bsearch(&key, canned_data, canned_used, sizeof(Path), path_compare);
86         if (p == NULL) {
87                 return 0;
88         }
89         *uid = p->uid;
90         *gid = p->gid;
91         *mode = p->mode;
92         *capabilities = p->capabilities;
93
94 #if 0
95         // for debugging, run the built-in fs_config and compare the results.
96
97         unsigned c_uid, c_gid, c_mode;
98         uint64_t c_capabilities;
99         fs_config(path, dir, &c_uid, &c_gid, &c_mode, &c_capabilities);
100
101         if (c_uid != *uid) printf("%s uid %d %d\n", path, *uid, c_uid);
102         if (c_gid != *gid) printf("%s gid %d %d\n", path, *gid, c_gid);
103         if (c_mode != *mode) printf("%s mode 0%o 0%o\n", path, *mode, c_mode);
104         if (c_capabilities != *capabilities) printf("%s capabilities %llx %llx\n", path, *capabilities, c_capabilities);
105 #endif
106
107         return 1;
108 }