reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 9 | // TODO: add unittests for all these operations |
| 10 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #ifndef SkOSFile_DEFINED |
| 12 | #define SkOSFile_DEFINED |
| 13 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 14 | #include <stdio.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 16 | #include "SkString.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
| 18 | enum SkFILE_Flags { |
| 19 | kRead_SkFILE_Flag = 0x01, |
| 20 | kWrite_SkFILE_Flag = 0x02 |
| 21 | }; |
| 22 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 23 | FILE* sk_fopen(const char path[], SkFILE_Flags); |
| 24 | void sk_fclose(FILE*); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 26 | size_t sk_fgetsize(FILE*); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 28 | size_t sk_fwrite(const void* buffer, size_t byteCount, FILE*); |
humper@google.com | 18a48c3 | 2013-01-14 19:42:08 +0000 | [diff] [blame] | 29 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 30 | void sk_fflush(FILE*); |
caryclark | 7471fa4 | 2015-12-16 13:41:23 -0800 | [diff] [blame] | 31 | void sk_fsync(FILE*); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 33 | size_t sk_ftell(FILE*); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 35 | /** Maps a file into memory. Returns the address and length on success, NULL otherwise. |
| 36 | * The mapping is read only. |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 37 | * When finished with the mapping, free the returned pointer with sk_fmunmap. |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 38 | */ |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 39 | void* sk_fmmap(FILE* f, size_t* length); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 40 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 41 | /** Maps a file descriptor into memory. Returns the address and length on success, NULL otherwise. |
| 42 | * The mapping is read only. |
| 43 | * When finished with the mapping, free the returned pointer with sk_fmunmap. |
| 44 | */ |
| 45 | void* sk_fdmmap(int fd, size_t* length); |
| 46 | |
| 47 | /** Unmaps a file previously mapped by sk_fmmap or sk_fdmmap. |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 48 | * The length parameter must be the same as returned from sk_fmmap. |
| 49 | */ |
| 50 | void sk_fmunmap(const void* addr, size_t length); |
| 51 | |
| 52 | /** Returns true if the two point at the exact same filesystem object. */ |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 53 | bool sk_fidentical(FILE* a, FILE* b); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 54 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 55 | /** Returns the underlying file descriptor for the given file. |
| 56 | * The return value will be < 0 on failure. |
| 57 | */ |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 58 | int sk_fileno(FILE* f); |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 59 | |
bungeman | bf0b9ce | 2014-07-10 15:18:23 -0700 | [diff] [blame] | 60 | /** Returns true if something (file, directory, ???) exists at this path, |
| 61 | * and has the specified access flags. |
| 62 | */ |
| 63 | bool sk_exists(const char *path, SkFILE_Flags = (SkFILE_Flags)0); |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 64 | |
| 65 | // Returns true if a directory exists at this path. |
| 66 | bool sk_isdir(const char *path); |
| 67 | |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 68 | // Like pread, but may affect the file position marker. |
| 69 | // Returns the number of bytes read or SIZE_MAX if failed. |
| 70 | size_t sk_qread(FILE*, void* buffer, size_t count, size_t offset); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 71 | |
| 72 | |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 73 | // Create a new directory at this path; returns true if successful. |
| 74 | // If the directory already existed, this will return true. |
| 75 | // Description of the error, if any, will be written to stderr. |
| 76 | bool sk_mkdir(const char* path); |
| 77 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | class SkOSFile { |
| 79 | public: |
| 80 | class Iter { |
| 81 | public: |
| 82 | Iter(); |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 83 | Iter(const char path[], const char suffix[] = nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | ~Iter(); |
| 85 | |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 86 | void reset(const char path[], const char suffix[] = nullptr); |
tomhudson@google.com | 7d04280 | 2011-07-14 13:15:55 +0000 | [diff] [blame] | 87 | /** If getDir is true, only returns directories. |
| 88 | Results are undefined if true and false calls are |
| 89 | interleaved on a single iterator. |
| 90 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | bool next(SkString* name, bool getDir = false); |
| 92 | |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 93 | static const size_t kStorageSize = 40; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | private: |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 95 | SkAlignedSStorage<kStorageSize> fSelf; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | }; |
| 97 | }; |
| 98 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | #endif |