Home | History | Annotate | Download | only in ports
      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 
     10 #include "SkTFitsIn.h"
     11 
     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 
     47 void* sk_fdmmap(int fd, size_t* size) {
     48     struct stat status;
     49     if (0 != fstat(fd, &status)) {
     50         return NULL;
     51     }
     52     if (!S_ISREG(status.st_mode)) {
     53         return NULL;
     54     }
     55     if (!SkTFitsIn<size_t>(status.st_size)) {
     56         return NULL;
     57     }
     58     size_t fileSize = static_cast<size_t>(status.st_size);
     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 }
     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     }
     78 
     79     return sk_fdmmap(fd, size);
     80 }
     81