18339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine/* Copyright (C) 2007-2010 The Android Open Source Project
28339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine**
38339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine** This software is licensed under the terms of the GNU General Public
48339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine** License version 2, as published by the Free Software Foundation, and
58339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine** may be copied, distributed, and modified under those terms.
68339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine**
78339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine** This program is distributed in the hope that it will be useful,
88339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine** but WITHOUT ANY WARRANTY; without even the implied warranty of
98339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
108339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine** GNU General Public License for more details.
118339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine*/
128339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
138339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine/*
148339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * Contains declarations of routines that implement platform-independent
158339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * file I/O.
168339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine */
178339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
188339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine#ifndef _ANDROID_UTILS_FILEIO_H
198339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine#define _ANDROID_UTILS_FILEIO_H
208339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
21af81d7432594d8459c4fb9f76c5e8a981f69a94cDavid 'Digit' Turner#include "android/utils/compiler.h"
22af81d7432594d8459c4fb9f76c5e8a981f69a94cDavid 'Digit' Turner
23af81d7432594d8459c4fb9f76c5e8a981f69a94cDavid 'Digit' Turner#include <stddef.h>
24af81d7432594d8459c4fb9f76c5e8a981f69a94cDavid 'Digit' Turner#include <unistd.h>
25af81d7432594d8459c4fb9f76c5e8a981f69a94cDavid 'Digit' Turner
26af81d7432594d8459c4fb9f76c5e8a981f69a94cDavid 'Digit' TurnerANDROID_BEGIN_HEADER
278339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
288339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkinetypedef struct MapFile MapFile;
298339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
308339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine#ifdef WIN32
318339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine/* Declare constants that are missing in Win32 headers. */
328339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine#define PROT_READ   0x1
338339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine#define PROT_WRITE  0x2
348339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine#define PROT_EXEC   0x4
358339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine#define PROT_NONE   0x0
368339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine#endif
378339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
388339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine/* Checks if file handle is a valid one.
398339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * Return:
408339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  boolean: 1 if handle is valid, or 0 if it's not valid.
418339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine */
428339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkinestatic inline int
438339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkinemapfile_is_valid(MapFile* handle)
448339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine{
45d9b6cb97a8a9e93f1bbe5351874b03f7faa81783David 'Digit' Turner    return handle != (void*)(ptrdiff_t)-1;
468339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine}
478339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
488339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine/* Opens file in selected mode.
498339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * Param:
508339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  path - Path to the file to open.
518339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  oflag - Defines mode in which file is to be opened. This value follows the
528339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *      symantics of O_XXX flags defined for standard open routine.
538339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  share_mode Defines sharing mode for the opened file. This value follows the
548339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *      symantics of S_IXXX flags defined for standard open routine.
558339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * Return:
568339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  A valid handle to the opened file on success, or an invalid value on
578339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  failure. In case of failure errno contains error code.
588339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine */
598339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkineextern MapFile* mapfile_open(const char* path, int oflag, int share_mode);
608339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
618339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine/* Closes a file handle opened with mapfile_open routine.
628339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * Param:
638339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  handle - A handle to a file previously obtained via successful call to
648339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *      mapfile_open routine.
658339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * Return:
668339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  0 on success, or -1 on failure with errno containing the error code.
678339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine */
688339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkineextern int mapfile_close(MapFile* handle);
698339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
708339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine/* Reads from a file opened with mapfile_open routine.
718339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * Except for handle parameter, semantics of this call are the same as for
728339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * the regualar read routine.
738339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * Param:
748339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  handle - A handle to a file previously obtained via successful call to
758339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *      mapfile_open routine.
768339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine */
778339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkineextern ssize_t mapfile_read(MapFile* handle, void* buf, size_t nbyte);
788339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
798339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine/* Reads from a specific offset in a file opened with mapfile_open routine.
808339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * Param:
818339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  handle - A handle to a file previously obtained via successful call to
828339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *      mapfile_open routine.
838339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  offset - Offset in the file where to start reading from.
848339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  Rest of the parameters and return value are the same as in file_read.
858339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine */
868339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkineextern ssize_t mapfile_read_at(MapFile* handle,
878339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine                               size_t offset,
888339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine                               void* buf,
898339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine                               size_t nbyte);
908339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
918339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine/* Maps a section of a file to memory.
928339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * Param:
938339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  handle - A handle to a file previously obtained via successful call to
948339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *      mapfile_open routine.
958339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  offset - Offset in the file where mapping should begin.
968339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  size - Number of bytes starting with offset that should be mapped.
978339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  prot - Determines whether read, write, execute, or some combination of
988339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *      accesses are permitted to the data being mapped. This parameter has the
998339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *      same semantics as in regular mmap routene.
1008339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  mapped_offset - Upon success, contains pointer to the requested offset
1018339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *      within the mapped section of the file.
1028339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  size - Upon success, contains total number of bytes that were actually
1038339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *      mapped.
1048339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * Return:
1058339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  Upon successful completion returns pointer to the beginning of memory
1068339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  mapping, containing mapping of the requested section of a file. Note that
1078339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  value returned from this routine doesn't necessarily points to the beginning
1088339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  of the requested section mapping. Use value returned in mapped_offset
1098339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  parameter to get actual pointer to the beginning of the requested section
1108339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  mapping. Value returned from this routine must eventually be passed to
1118339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  file_unmap_section reoutine to unmap section mapped with this routine.
1128339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  This routine returns NULL on failure and sets errno to indicate the error.
1138339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine */
1148339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkineextern void* mapfile_map(MapFile* handle,
1158339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine                         size_t offset,
1168339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine                         size_t size,
1178339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine                         int prot,
1188339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine                         void** mapped_offset,
1198339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine                         size_t* mapped_size);
1208339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
1218339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine/* Umaps section of a file previously mapped with mapfile_map routine.
1228339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * Param:
1238339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  mapped_at - A pointer to the base address of the mapped section of a file
1248339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *      that is to be unmapped.
1258339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  len - Byte size of the section that is to be unmapped.
1268339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine * Return:
1278339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  Upon successful completion returns 0. Otherwise, returns -1 and sets
1288339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine *  errno to indicate the error.
1298339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine */
1308339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkineextern int mapfile_unmap(void* mapped_at, size_t len);
1318339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
132af81d7432594d8459c4fb9f76c5e8a981f69a94cDavid 'Digit' TurnerANDROID_END_HEADER
1338339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine
1348339d18223eed408bfefcd00f649a2b13ccac52cVladimir Chtchetkine#endif  // _ANDROID_UTILS_FILEIO_H
135