bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkOSFile.h" |
| 9 | |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 10 | #include "SkTFitsIn.h" |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 11 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <sys/mman.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <sys/types.h> |
| 16 | |
| 17 | typedef struct { |
| 18 | dev_t dev; |
| 19 | ino_t ino; |
| 20 | } SkFILEID; |
| 21 | |
| 22 | static bool sk_ino(SkFILE* a, SkFILEID* id) { |
| 23 | int fd = fileno((FILE*)a); |
| 24 | if (fd < 0) { |
| 25 | return 0; |
| 26 | } |
| 27 | struct stat status; |
| 28 | if (0 != fstat(fd, &status)) { |
| 29 | return 0; |
| 30 | } |
| 31 | id->dev = status.st_dev; |
| 32 | id->ino = status.st_ino; |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | bool sk_fidentical(SkFILE* a, SkFILE* b) { |
| 37 | SkFILEID aID, bID; |
| 38 | return sk_ino(a, &aID) && sk_ino(b, &bID) |
| 39 | && aID.ino == bID.ino |
| 40 | && aID.dev == bID.dev; |
| 41 | } |
| 42 | |
| 43 | void sk_fmunmap(const void* addr, size_t length) { |
| 44 | munmap(const_cast<void*>(addr), length); |
| 45 | } |
| 46 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 47 | void* sk_fdmmap(int fd, size_t* size) { |
| 48 | struct stat status; |
| 49 | if (0 != fstat(fd, &status)) { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 50 | return NULL; |
| 51 | } |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 52 | if (!S_ISREG(status.st_mode)) { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 53 | return NULL; |
| 54 | } |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 55 | if (!SkTFitsIn<size_t>(status.st_size)) { |
| 56 | return NULL; |
| 57 | } |
| 58 | size_t fileSize = static_cast<size_t>(status.st_size); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 59 | |
| 60 | void* addr = mmap(NULL, fileSize, PROT_READ, MAP_PRIVATE, fd, 0); |
| 61 | if (MAP_FAILED == addr) { |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | *size = fileSize; |
| 66 | return addr; |
| 67 | } |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 68 | |
| 69 | int sk_fileno(SkFILE* f) { |
| 70 | return fileno((FILE*)f); |
| 71 | } |
| 72 | |
| 73 | void* sk_fmmap(SkFILE* f, size_t* size) { |
| 74 | int fd = sk_fileno(f); |
| 75 | if (fd < 0) { |
| 76 | return NULL; |
| 77 | } |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 78 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 79 | return sk_fdmmap(fd, size); |
| 80 | } |