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