michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkOSFile.h" michael@0: michael@0: #include "SkTFitsIn.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: typedef struct { michael@0: dev_t dev; michael@0: ino_t ino; michael@0: } SkFILEID; michael@0: michael@0: static bool sk_ino(SkFILE* a, SkFILEID* id) { michael@0: int fd = fileno((FILE*)a); michael@0: if (fd < 0) { michael@0: return 0; michael@0: } michael@0: struct stat status; michael@0: if (0 != fstat(fd, &status)) { michael@0: return 0; michael@0: } michael@0: id->dev = status.st_dev; michael@0: id->ino = status.st_ino; michael@0: return true; michael@0: } michael@0: michael@0: bool sk_fidentical(SkFILE* a, SkFILE* b) { michael@0: SkFILEID aID, bID; michael@0: return sk_ino(a, &aID) && sk_ino(b, &bID) michael@0: && aID.ino == bID.ino michael@0: && aID.dev == bID.dev; michael@0: } michael@0: michael@0: void sk_fmunmap(const void* addr, size_t length) { michael@0: munmap(const_cast(addr), length); michael@0: } michael@0: michael@0: void* sk_fdmmap(int fd, size_t* size) { michael@0: struct stat status; michael@0: if (0 != fstat(fd, &status)) { michael@0: return NULL; michael@0: } michael@0: if (!S_ISREG(status.st_mode)) { michael@0: return NULL; michael@0: } michael@0: if (!SkTFitsIn(status.st_size)) { michael@0: return NULL; michael@0: } michael@0: size_t fileSize = static_cast(status.st_size); michael@0: michael@0: void* addr = mmap(NULL, fileSize, PROT_READ, MAP_PRIVATE, fd, 0); michael@0: if (MAP_FAILED == addr) { michael@0: return NULL; michael@0: } michael@0: michael@0: *size = fileSize; michael@0: return addr; michael@0: } michael@0: michael@0: int sk_fileno(SkFILE* f) { michael@0: return fileno((FILE*)f); michael@0: } michael@0: michael@0: void* sk_fmmap(SkFILE* f, size_t* size) { michael@0: int fd = sk_fileno(f); michael@0: if (fd < 0) { michael@0: return NULL; michael@0: } michael@0: michael@0: return sk_fdmmap(fd, size); michael@0: }