| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cgpt.h" | 5 #include "cgpt.h" |
| 6 | 6 |
| 7 #include <getopt.h> | 7 #include <getopt.h> |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 | 153 |
| 154 // This returns true if a GPT partition matches the search criteria. If a match | 154 // This returns true if a GPT partition matches the search criteria. If a match |
| 155 // isn't found (or if the file doesn't contain a GPT), it returns false. The | 155 // isn't found (or if the file doesn't contain a GPT), it returns false. The |
| 156 // filename and partition number that matched is left in a global, since we | 156 // filename and partition number that matched is left in a global, since we |
| 157 // could have multiple hits. | 157 // could have multiple hits. |
| 158 static int do_search(char *filename) { | 158 static int do_search(char *filename) { |
| 159 int retval = 0; | 159 int retval = 0; |
| 160 int i; | 160 int i; |
| 161 struct drive drive; | 161 struct drive drive; |
| 162 GptEntry *entry; | 162 GptEntry *entry; |
| 163 char partlabel[sizeof(entry->name) * 3 / 2]; | 163 char partlabel[GPT_PARTNAME_LEN]; |
| 164 | 164 |
| 165 if (CGPT_OK != DriveOpen(filename, &drive)) | 165 if (CGPT_OK != DriveOpen(filename, &drive)) |
| 166 return 0; | 166 return 0; |
| 167 | 167 |
| 168 if (GPT_SUCCESS != GptSanityCheck(&drive.gpt)) { | 168 if (GPT_SUCCESS != GptSanityCheck(&drive.gpt)) { |
| 169 (void) DriveClose(&drive, 0); | 169 (void) DriveClose(&drive, 0); |
| 170 return 0; | 170 return 0; |
| 171 } | 171 } |
| 172 | 172 |
| 173 for (i = 0; i < GetNumberOfEntries(&drive.gpt); ++i) { | 173 for (i = 0; i < GetNumberOfEntries(&drive.gpt); ++i) { |
| 174 entry = GetEntry(&drive.gpt, PRIMARY, i); | 174 entry = GetEntry(&drive.gpt, PRIMARY, i); |
| 175 | 175 |
| 176 if (IsZero(&entry->type)) | 176 if (IsZero(&entry->type)) |
| 177 continue; | 177 continue; |
| 178 | 178 |
| 179 int found = 0; | 179 int found = 0; |
| 180 if ((set_unique && !memcmp(&unique_guid, &entry->unique, sizeof(Guid))) || | 180 if ((set_unique && !memcmp(&unique_guid, &entry->unique, sizeof(Guid))) || |
| 181 (set_type && !memcmp(&type_guid, &entry->type, sizeof(Guid)))) { | 181 (set_type && !memcmp(&type_guid, &entry->type, sizeof(Guid)))) { |
| 182 found = 1; | 182 found = 1; |
| 183 } else if (set_label) { | 183 } else if (set_label) { |
| 184 UTF16ToUTF8(entry->name, (uint8_t *)partlabel); | 184 UTF16ToUTF8(entry->name, sizeof(entry->name) / sizeof(entry->name[0]), |
| 185 (uint8_t *)partlabel, sizeof(partlabel)); |
| 185 if (!strncmp(label, partlabel, sizeof(partlabel))) { | 186 if (!strncmp(label, partlabel, sizeof(partlabel))) { |
| 186 found = 1; | 187 found = 1; |
| 187 } | 188 } |
| 188 } | 189 } |
| 189 if (found && match_content(&drive, entry)) { | 190 if (found && match_content(&drive, entry)) { |
| 190 hits++; | 191 hits++; |
| 191 retval++; | 192 retval++; |
| 192 showmatch(filename, i+1, entry); | 193 showmatch(filename, i+1, entry); |
| 193 if (!match_partnum) { | 194 if (!match_partnum) { |
| 194 match_partnum = i+1; | 195 match_partnum = i+1; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 | 244 |
| 244 return 0; | 245 return 0; |
| 245 } | 246 } |
| 246 | 247 |
| 247 | 248 |
| 248 // This scans all the physical devices it can find, looking for a match. It | 249 // This scans all the physical devices it can find, looking for a match. It |
| 249 // returns true if any matches were found, false otherwise. | 250 // returns true if any matches were found, false otherwise. |
| 250 static int scan_real_devs(void) { | 251 static int scan_real_devs(void) { |
| 251 int found = 0; | 252 int found = 0; |
| 252 char line[BUFSIZE]; | 253 char line[BUFSIZE]; |
| 253 char partname[128]; | 254 char partname[128]; // max size for /proc/partition lines? |
| 254 FILE *fp; | 255 FILE *fp; |
| 255 char *pathname; | 256 char *pathname; |
| 256 | 257 |
| 257 fp = fopen(PROC_PARTITIONS, "r"); | 258 fp = fopen(PROC_PARTITIONS, "r"); |
| 258 if (!fp) { | 259 if (!fp) { |
| 259 perror("can't read " PROC_PARTITIONS); | 260 perror("can't read " PROC_PARTITIONS); |
| 260 return found; | 261 return found; |
| 261 } | 262 } |
| 262 | 263 |
| 263 while (fgets(line, sizeof(line), fp)) { | 264 while (fgets(line, sizeof(line), fp)) { |
| 264 int ma, mi; | 265 int ma, mi; |
| 265 long long unsigned int sz; | 266 long long unsigned int sz; |
| 266 | 267 |
| 267 if (sscanf(line, " %d %d %llu %128[^\n ]", &ma, &mi, &sz, partname) != 4) | 268 if (sscanf(line, " %d %d %llu %127[^\n ]", &ma, &mi, &sz, partname) != 4) |
| 268 continue; | 269 continue; |
| 269 | 270 |
| 270 if ((pathname = is_wholedev(partname))) { | 271 if ((pathname = is_wholedev(partname))) { |
| 271 if (do_search(pathname)) { | 272 if (do_search(pathname)) { |
| 272 found++; | 273 found++; |
| 273 } | 274 } |
| 274 } | 275 } |
| 275 } | 276 } |
| 276 | 277 |
| 277 fclose(fp); | 278 fclose(fp); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 if (oneonly && hits != 1) { | 377 if (oneonly && hits != 1) { |
| 377 return CGPT_FAILED; | 378 return CGPT_FAILED; |
| 378 } | 379 } |
| 379 | 380 |
| 380 if (match_partnum) { | 381 if (match_partnum) { |
| 381 return CGPT_OK; | 382 return CGPT_OK; |
| 382 } | 383 } |
| 383 | 384 |
| 384 return CGPT_FAILED; | 385 return CGPT_FAILED; |
| 385 } | 386 } |
| OLD | NEW |