OSDN Git Service

Use default path to put benchmark binaries
[android-x86/system-extras.git] / ext4_utils / 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 <inttypes.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdlib.h>
23
24 #include "private/android_filesystem_config.h"
25
26 #include "canned_fs_config.h"
27
28 typedef struct {
29         const char* path;
30         unsigned uid;
31         unsigned gid;
32         unsigned mode;
33         uint64_t capabilities;
34 } Path;
35
36 static Path* canned_data = NULL;
37 static int canned_alloc = 0;
38 static int canned_used = 0;
39
40 static int path_compare(const void* a, const void* b) {
41         return strcmp(((Path*)a)->path, ((Path*)b)->path);
42 }
43
44 int load_canned_fs_config(const char* fn) {
45         FILE* f = fopen(fn, "r");
46         if (f == NULL) {
47                 fprintf(stderr, "failed to open %s: %s\n", fn, strerror(errno));
48                 return -1;
49         }
50
51         char line[PATH_MAX + 200];
52         while (fgets(line, sizeof(line), f)) {
53                 while (canned_used >= canned_alloc) {
54                         canned_alloc = (canned_alloc+1) * 2;
55                         canned_data = (Path*) realloc(canned_data, canned_alloc * sizeof(Path));
56                 }
57                 Path* p = canned_data + canned_used;
58                 p->path = strdup(strtok(line, " "));
59                 p->uid = atoi(strtok(NULL, " "));
60                 p->gid = atoi(strtok(NULL, " "));
61                 p->mode = strtol(strtok(NULL, " "), NULL, 8);   // mode is in octal
62                 p->capabilities = 0;
63
64                 char* token = NULL;
65                 do {
66                         token = strtok(NULL, " ");
67                         if (token && strncmp(token, "capabilities=", 13) == 0) {
68                                 p->capabilities = strtoll(token+13, NULL, 0);
69                                 break;
70                         }
71                 } while (token);
72
73                 canned_used++;
74         }
75
76         fclose(f);
77
78         qsort(canned_data, canned_used, sizeof(Path), path_compare);
79         printf("loaded %d fs_config entries\n", canned_used);
80
81         return 0;
82 }
83
84 static const int kDebugCannedFsConfig = 0;
85
86 void canned_fs_config(const char* path, int dir, const char* target_out_path,
87                                           unsigned* uid, unsigned* gid, unsigned* mode, uint64_t* capabilities) {
88         Path key;
89         key.path = path+1;   // canned paths lack the leading '/'
90         Path* p = (Path*) bsearch(&key, canned_data, canned_used, sizeof(Path), path_compare);
91         if (p == NULL) {
92                 fprintf(stderr, "failed to find [%s] in canned fs_config\n", path);
93                 exit(1);
94         }
95         *uid = p->uid;
96         *gid = p->gid;
97         *mode = p->mode;
98         *capabilities = p->capabilities;
99
100         if (kDebugCannedFsConfig) {
101                 // for debugging, run the built-in fs_config and compare the results.
102
103                 unsigned c_uid, c_gid, c_mode;
104                 uint64_t c_capabilities;
105                 fs_config(path, dir, target_out_path, &c_uid, &c_gid, &c_mode, &c_capabilities);
106
107                 if (c_uid != *uid) printf("%s uid %d %d\n", path, *uid, c_uid);
108                 if (c_gid != *gid) printf("%s gid %d %d\n", path, *gid, c_gid);
109                 if (c_mode != *mode) printf("%s mode 0%o 0%o\n", path, *mode, c_mode);
110                 if (c_capabilities != *capabilities)
111                         printf("%s capabilities %" PRIx64 " %" PRIx64 "\n",
112                                 path,
113                                 *capabilities,
114                                 c_capabilities);
115         }
116 }