51Degrees Common C/C++  4.1

A shared functionality library that is used by 51Degrees products

file.h

1 /* *********************************************************************
2  * This Source Code Form is copyright of 51 Degrees Mobile Experts Limited.
3  * Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
4  * Caversham, Reading, Berkshire, United Kingdom RG4 7BY
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0.
8  *
9  * If a copy of the MPL was not distributed with this file, You can obtain
10  * one at http://mozilla.org/MPL/2.0/.
11  *
12  * This Source Code Form is "Incompatible With Secondary Licenses", as
13  * defined by the Mozilla Public License, v. 2.0.
14  * ******************************************************************** */
15 
137 #ifndef FIFTYONE_DEGREES_FILE_H_INCLUDED
138 #define FIFTYONE_DEGREES_FILE_H_INCLUDED
139 
140 /* Define NDEBUG if needed, to ensure asserts are disabled in release builds */
141 #if !defined(DEBUG) && !defined(_DEBUG) && !defined(NDEBUG)
142 #define NDEBUG
143 #endif
144 
145 #include <limits.h>
146 #include <stdio.h>
147 #include <errno.h>
148 #include <stdint.h>
149 #include <stdbool.h>
150 #ifdef _MSC_VER
151 #include <windows.h>
152 #include <direct.h>
153 #include <tchar.h>
154 #else
155 #include <dirent.h>
156 #include <sys/stat.h>
157 #endif
158 #include <time.h>
159 #include <assert.h>
160 #include <limits.h>
161 #include <time.h>
162 #include "data.h"
163 #include "exceptions.h"
164 #include "status.h"
165 #include "memory.h"
166 #include "pool.h"
167 #include "threading.h"
168 
172 #ifdef _MSC_FULL_VER
173 #define GetCurrentDir _getcwd
174 #include <windows.h>
175 #include <direct.h>
176 #include <tchar.h>
177 #else
178 #define GetCurrentDir getcwd
179 #include <unistd.h>
180 #include <dirent.h>
181 #include <sys/stat.h>
182 #endif
183 
192 #ifdef _MSC_FULL_VER
193 #define FIFTYONE_DEGREES_FILE_MAX_PATH 260
194 #else
195 #define FIFTYONE_DEGREES_FILE_MAX_PATH 4096
196 #endif
197 
198 #ifdef __cplusplus
199 #define EXTERNAL extern "C"
200 #else
201 #define EXTERNAL
202 #endif
203 
207 typedef union fiftyone_degrees_file_handle_t {
208  FILE *file;
211 
215  typedef struct fiftyone_degrees_file_pool_t {
217  long length;
219 
226 
235  const char* fileName,
236  FILE** handle);
237 
246  const char* fileName,
247  const void *data,
248  const size_t length);
249 
260  const char *source,
261  const char *destination);
262 
270  const char *fileName);
271 
279  const char *pathName);
280 
297  const char *dataFolderName,
298  const char *fileName,
299  char *destination,
300  size_t size);
301 
319  const char *masterFile,
320  const char **paths,
321  int count,
322  long bytesToCompare,
323  const char *destination);
324 
338  const char *masterFile,
339  const char **paths,
340  int count,
341  const char *destination);
342 
355  fiftyoneDegreesFilePool *filePool,
356  const char *fileName,
357  uint16_t concurrency,
358  fiftyoneDegreesException *exception);
359 
371  fiftyoneDegreesFilePool *filePool,
372  fiftyoneDegreesException *exception);
373 
380  fiftyoneDegreesFileHandle* handle);
381 
388 EXTERNAL long fiftyoneDegreesFileGetSize(const char *fileName);
389 
399  const char *fileName,
401 
407 
413 EXTERNAL const char* fiftyoneDegreesFileGetFileName(const char *filePath);
414 
418 #endif
EXTERNAL fiftyoneDegreesStatusCode fiftyoneDegreesFileCreateTempFile(const char *masterFile, const char **paths, int count, const char *destination)
Create a temporary file containing a copy of the master file using the first writable path in the lis...
Definition: file.c:478
fiftyoneDegreesStatusCode
Status returned from the initialisation of a resource.
Definition: status.h:75
Pool item node in the stack of items.
Definition: pool.h:172
EXTERNAL long fiftyoneDegreesFileGetSize(const char *fileName)
Returns the size of a file in bytes, or -1 if the file does not exist or cannot be accessed.
Definition: file.c:650
Used to read data from memory in a similar manner to a file handle.
Definition: memory.h:51
FILE * file
Open read handle to the source data file.
Definition: file.h:208
fiftyoneDegreesPoolItem item
The pool item with the resource.
Definition: file.h:209
EXTERNAL void fiftyoneDegreesFilePoolReset(fiftyoneDegreesFilePool *filePool)
Resets the pool without releasing any resources.
Definition: file.c:699
fiftyoneDegreesPool pool
The pool of file handles.
Definition: file.h:216
EXTERNAL fiftyoneDegreesStatusCode fiftyoneDegreesFileDelete(const char *fileName)
Delete a file from the file system.
Definition: file.c:611
EXTERNAL void fiftyoneDegreesFileHandleRelease(fiftyoneDegreesFileHandle *handle)
Returns a handle previously retrieved via fiftyoneDegreesFileHandleGet back to the pool.
Definition: file.c:603
Pool of resources stored as items in a stack.
Definition: pool.h:192
EXTERNAL fiftyoneDegreesStatusCode fiftyoneDegreesFileReadToByteArray(const char *fileName, fiftyoneDegreesMemoryReader *reader)
Reads the contents of a file into memory.
Definition: file.c:660
EXTERNAL fiftyoneDegreesFileHandle * fiftyoneDegreesFileHandleGet(fiftyoneDegreesFilePool *filePool, fiftyoneDegreesException *exception)
Retrieves a read only open file handle from the pool.
Definition: file.c:597
EXTERNAL void fiftyoneDegreesFilePoolRelease(fiftyoneDegreesFilePool *pool)
Releases the file handles contained in the pool and frees any internal memory used by the pool.
Definition: file.c:607
EXTERNAL bool fiftyoneDegreesFileGetExistingTempFile(const char *masterFile, const char **paths, int count, long bytesToCompare, const char *destination)
Gets the path to a temporary file which is an exact copy of the master file if one exists.
Definition: file.c:447
File handle node in the stack of handles.
Definition: file.h:207
EXTERNAL fiftyoneDegreesStatusCode fiftyoneDegreesFileGetPath(const char *dataFolderName, const char *fileName, char *destination, size_t size)
Iterates up the folders from the current working directory until a file in the sub folder dataFolderN...
Definition: file.c:523
long length
Length of the file in bytes.
Definition: file.h:217
EXTERNAL fiftyoneDegreesStatusCode fiftyoneDegreesFileWrite(const char *fileName, const void *data, const size_t length)
Writes binary data to the file path provided, closing the file once finished.
Definition: file.c:405
EXTERNAL const char * fiftyoneDegreesFileGetFileName(const char *filePath)
Gets the last, file name, segment of the full file path.
Definition: file.c:704
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition: exceptions.h:109
EXTERNAL fiftyoneDegreesStatusCode fiftyoneDegreesFilePoolInit(fiftyoneDegreesFilePool *filePool, const char *fileName, uint16_t concurrency, fiftyoneDegreesException *exception)
Initialises the pool with a stack of open read only file handles all associated with the file name.
Definition: file.c:567
Stack of handles used to read data from a single source file.
Definition: file.h:215
EXTERNAL fiftyoneDegreesStatusCode fiftyoneDegreesFileCopy(const char *source, const char *destination)
Copy a file from one location to another.
Definition: file.c:625
EXTERNAL fiftyoneDegreesStatusCode fiftyoneDegreesFileCreateDirectory(const char *pathName)
Creates a directory with the specified path, and returns the result of the operation.
Definition: file.c:420
EXTERNAL fiftyoneDegreesStatusCode fiftyoneDegreesFileOpen(const char *fileName, FILE **handle)
Opens the file path provided placing the file handle in the handle parameter.
Definition: file.c:399