blob: ddbcc2b26433c94ce632f134ce5321fe8199a14b [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
epoger@google.come8ebeb12012-10-29 16:42:11 +00009// TODO: add unittests for all these operations
10
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#ifndef SkOSFile_DEFINED
12#define SkOSFile_DEFINED
13
halcanaryd76be9c2015-11-20 13:47:49 -080014#include <stdio.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
halcanaryd76be9c2015-11-20 13:47:49 -080016#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
18enum SkFILE_Flags {
19 kRead_SkFILE_Flag = 0x01,
20 kWrite_SkFILE_Flag = 0x02
21};
22
halcanaryd76be9c2015-11-20 13:47:49 -080023FILE* sk_fopen(const char path[], SkFILE_Flags);
24void sk_fclose(FILE*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
halcanaryd76be9c2015-11-20 13:47:49 -080026size_t sk_fgetsize(FILE*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
halcanaryd76be9c2015-11-20 13:47:49 -080028size_t sk_fwrite(const void* buffer, size_t byteCount, FILE*);
humper@google.com18a48c32013-01-14 19:42:08 +000029
halcanaryd76be9c2015-11-20 13:47:49 -080030void sk_fflush(FILE*);
caryclark7471fa42015-12-16 13:41:23 -080031void sk_fsync(FILE*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000032
halcanaryd76be9c2015-11-20 13:47:49 -080033size_t sk_ftell(FILE*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000034
bungeman@google.com6cab1a42013-05-29 13:43:31 +000035/** Maps a file into memory. Returns the address and length on success, NULL otherwise.
36 * The mapping is read only.
bungeman@google.com11c9a552013-06-03 17:10:35 +000037 * When finished with the mapping, free the returned pointer with sk_fmunmap.
bungeman@google.com6cab1a42013-05-29 13:43:31 +000038 */
halcanaryd76be9c2015-11-20 13:47:49 -080039void* sk_fmmap(FILE* f, size_t* length);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000040
bungeman@google.com11c9a552013-06-03 17:10:35 +000041/** 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 */
45void* sk_fdmmap(int fd, size_t* length);
46
47/** Unmaps a file previously mapped by sk_fmmap or sk_fdmmap.
bungeman@google.com6cab1a42013-05-29 13:43:31 +000048 * The length parameter must be the same as returned from sk_fmmap.
49 */
50void sk_fmunmap(const void* addr, size_t length);
51
52/** Returns true if the two point at the exact same filesystem object. */
halcanaryd76be9c2015-11-20 13:47:49 -080053bool sk_fidentical(FILE* a, FILE* b);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000054
bungeman@google.com11c9a552013-06-03 17:10:35 +000055/** Returns the underlying file descriptor for the given file.
56 * The return value will be < 0 on failure.
57 */
halcanaryd76be9c2015-11-20 13:47:49 -080058int sk_fileno(FILE* f);
bungeman@google.com11c9a552013-06-03 17:10:35 +000059
bungemanbf0b9ce2014-07-10 15:18:23 -070060/** Returns true if something (file, directory, ???) exists at this path,
61 * and has the specified access flags.
62 */
63bool sk_exists(const char *path, SkFILE_Flags = (SkFILE_Flags)0);
epoger@google.come8ebeb12012-10-29 16:42:11 +000064
65// Returns true if a directory exists at this path.
66bool sk_isdir(const char *path);
67
Ben Wagner4d1955c2017-03-10 13:08:15 -050068// Like pread, but may affect the file position marker.
69// Returns the number of bytes read or SIZE_MAX if failed.
70size_t sk_qread(FILE*, void* buffer, size_t count, size_t offset);
humper@google.com7af56be2013-01-14 18:49:19 +000071
72
epoger@google.come8ebeb12012-10-29 16:42:11 +000073// 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.
76bool sk_mkdir(const char* path);
77
reed@android.com8a1c16f2008-12-17 15:59:43 +000078class SkOSFile {
79public:
80 class Iter {
81 public:
82 Iter();
Ben Wagnera93a14a2017-08-28 10:34:05 -040083 Iter(const char path[], const char suffix[] = nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 ~Iter();
85
Ben Wagnera93a14a2017-08-28 10:34:05 -040086 void reset(const char path[], const char suffix[] = nullptr);
tomhudson@google.com7d042802011-07-14 13:15:55 +000087 /** 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.com8a1c16f2008-12-17 15:59:43 +000091 bool next(SkString* name, bool getDir = false);
92
bungemane998b7f2015-02-12 07:18:27 -080093 static const size_t kStorageSize = 40;
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 private:
bungemane998b7f2015-02-12 07:18:27 -080095 SkAlignedSStorage<kStorageSize> fSelf;
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 };
97};
98
reed@android.com8a1c16f2008-12-17 15:59:43 +000099#endif