A device detection library that is used natively or by 51Degrees products

file.h

1/* *********************************************************************
2 * This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 * Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
4 * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
5 *
6 * This Original Work is licensed under the European Union Public Licence
7 * (EUPL) v.1.2 and is subject to its terms as set out below.
8 *
9 * If a copy of the EUPL was not distributed with this file, You can obtain
10 * one at https://opensource.org/licenses/EUPL-1.2.
11 *
12 * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13 * amended by the European Commission) shall be deemed incompatible for
14 * the purposes of the Work and the provisions of the compatibility
15 * clause in Article 5 of the EUPL shall not apply.
16 *
17 * If using the Work as, or as part of, a network application, by
18 * including the attribution notice(s) required under Article 5 of the EUPL
19 * in the end user terms of the application under an appropriate heading,
20 * such notice(s) shall fulfill the requirements of that article.
21 * ********************************************************************* */
22
142
143#ifndef FIFTYONE_DEGREES_FILE_H_INCLUDED
144#define FIFTYONE_DEGREES_FILE_H_INCLUDED
145
146#ifdef FIFTYONE_DEGREES_IPI_SYMBOLS
147#include "ipiSymbols.h"
148#endif
149
150/* Define NDEBUG if needed, to ensure asserts are disabled in release builds */
151#if !defined(DEBUG) && !defined(_DEBUG) && !defined(NDEBUG)
152#define NDEBUG
153#endif
154
155#include <limits.h>
156#include <stdio.h>
157#include <errno.h>
158#include <stdint.h>
159#include <stdbool.h>
160#ifdef _MSC_VER
161#pragma warning (push)
162#pragma warning (disable: 5105)
163#include <windows.h>
164#pragma warning (default: 5105)
165#pragma warning (pop)
166#include <direct.h>
167#include <tchar.h>
168#else
169#include <dirent.h>
170#include <sys/stat.h>
171#endif
172#include <time.h>
173#include <assert.h>
174#include <limits.h>
175#include <time.h>
176#include "data.h"
177#include "fileOffset.h"
178#include "exceptions.h"
179#include "status.h"
180#include "memory.h"
181#include "pool.h"
182#include "threading.h"
183#include "common.h"
184
188#ifdef _MSC_FULL_VER
189#define GET_CURRENT_DIR _getcwd
190#pragma warning (push)
191#pragma warning (disable: 5105)
192#include <windows.h>
193#pragma warning (default: 5105)
194#pragma warning (pop)
195#include <direct.h>
196#include <tchar.h>
197#else
198#define GET_CURRENT_DIR getcwd
199#include <unistd.h>
200#include <dirent.h>
201#include <sys/stat.h>
202#endif
203
204#define TEMP_UNIQUE_STRING_LENGTH 20
205
214#ifdef _MSC_FULL_VER
215#define FIFTYONE_DEGREES_FILE_MAX_PATH 260
216#else
217#define FIFTYONE_DEGREES_FILE_MAX_PATH 4096
218#endif
219
225#define FIFTYONE_DEGREES_FILE_POOL_MAX_HANDLES 64
226
227
231typedef union fiftyone_degrees_file_handle_t {
232 FILE *file;
235
239 typedef struct fiftyone_degrees_file_pool_t {
243
252 FILE *stream,
254 int origin);
255
262 FILE *stream
263);
264
271
280 const char* fileName,
281 FILE** handle);
282
291 const char* fileName,
292 const void *data,
293 const size_t length);
294
305 const char *source,
306 const char *destination);
307
315 const char *fileName);
316
324 const char *pathName);
325
342 const char *dataFolderName,
343 const char *fileName,
344 char *destination,
345 size_t size);
346
364 const char *masterFile,
365 const char **paths,
366 int count,
367 fiftyoneDegreesFileOffset bytesToCompare,
368 const char *destination);
369
388 const char *masterFileName,
389 const char **paths,
390 int count,
391 fiftyoneDegreesFileOffset bytesToCompare);
392
402 const char* masterFileName,
403 char* destination,
404 size_t nameStart,
405 size_t length);
406
421 const char* masterFile,
422 const char** paths,
423 int count,
424 char* destination,
425 size_t length);
426
441 const char *masterFile,
442 const char **paths,
443 int count,
444 const char *destination);
445
459 fiftyoneDegreesFilePool *filePool,
460 const char *fileName,
461 uint16_t concurrency,
462 fiftyoneDegreesException *exception);
463
476 fiftyoneDegreesFilePool *filePool,
477 fiftyoneDegreesException *exception);
478
486
494
504 const char *fileName,
506
512
518EXTERNAL const char* fiftyoneDegreesFileGetFileName(const char *filePath);
519
526EXTERNAL const char* fiftyoneDegreesFileGetBaseName(const char *path);
527
531#endif
long fiftyoneDegreesFileOffset
Type for file offset in API.
Definition fileOffset.h:73
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.
fiftyoneDegreesStatusCode fiftyoneDegreesFileCreateDirectory(const char *pathName)
Creates a directory with the specified path, and returns the result of the operation.
fiftyoneDegreesStatusCode fiftyoneDegreesFileReadToByteArray(const char *fileName, fiftyoneDegreesMemoryReader *reader)
Reads the contents of a file into memory.
int fiftyoneDegreesFileDeleteUnusedTempFiles(const char *masterFileName, const char **paths, int count, fiftyoneDegreesFileOffset bytesToCompare)
Finds all the temporary files which is an exact copy of the master file if any exist.
const char * fiftyoneDegreesFileGetBaseName(const char *path)
Gets the name of the file after the last separator in the path.
fiftyoneDegreesFileOffset fiftyoneDegreesFileGetSize(const char *fileName)
Returns the size of a file in bytes, or -1 if the file does not exist or cannot be accessed.
void fiftyoneDegreesFilePoolReset(fiftyoneDegreesFilePool *filePool)
Resets the pool without releasing any resources.
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.
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...
void fiftyoneDegreesFileHandleRelease(fiftyoneDegreesFileHandle *handle)
Returns a handle previously retrieved via fiftyoneDegreesFileHandleGet back to the pool.
fiftyoneDegreesFileOffset fiftyoneDegreesFileTell(FILE *stream)
Gets the current position of a file pointer.
fiftyoneDegreesStatusCode fiftyoneDegreesFileOpen(const char *fileName, FILE **handle)
Opens the file path provided placing the file handle in the handle parameter.
fiftyoneDegreesStatusCode fiftyoneDegreesFileCopy(const char *source, const char *destination)
Copy a file from one location to another.
DEPRECATED fiftyoneDegreesStatusCode fiftyoneDegreesFileCreateTempFile(const char *masterFile, const char **paths, int count, const char *destination)
[DEPRECATED - Use fiftyoneDegreesFileNewTempFile instead] Create a temporary file containing a copy o...
int fiftyoneDegreesFileSeek(FILE *stream, fiftyoneDegreesFileOffset offset, int origin)
Moves the file pointer to a specified location.
fiftyoneDegreesStatusCode fiftyoneDegreesFileDelete(const char *fileName)
Delete a file from the file system.
bool fiftyoneDegreesFileGetExistingTempFile(const char *masterFile, const char **paths, int count, fiftyoneDegreesFileOffset bytesToCompare, const char *destination)
Gets the path to a temporary file which is an exact copy of the master file if one exists.
fiftyoneDegreesFileHandle * fiftyoneDegreesFileHandleGet(fiftyoneDegreesFilePool *filePool, fiftyoneDegreesException *exception)
Retrieves a read only open file handle from the pool, waiting for one to be released if every handle ...
fiftyoneDegreesStatusCode fiftyoneDegreesFileNewTempFile(const char *masterFile, const char **paths, int count, char *destination, size_t length)
Create a temporary file containing a copy of the master file using the first writable path in the lis...
fiftyoneDegreesStatusCode fiftyoneDegreesFileAddTempFileName(const char *masterFileName, char *destination, size_t nameStart, size_t length)
Create a temporary file name and add it to the destination.
void fiftyoneDegreesFilePoolRelease(fiftyoneDegreesFilePool *pool)
Releases the file handles contained in the pool and frees any internal memory used by the pool.
const char * fiftyoneDegreesFileGetFileName(const char *filePath)
Gets the last, file name, segment of the full file path.
fiftyoneDegreesStatusCode
Status returned from the initialisation of a resource.
Definition status.h:81
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition exceptions.h:115
Stack of handles used to read data from a single source file.
Definition file.h:239
fiftyoneDegreesPool pool
The pool of file handles.
Definition file.h:240
fiftyoneDegreesFileOffset length
Length of the file in bytes.
Definition file.h:241
Used to read data from memory in a similar manner to a file handle.
Definition memory.h:58
Pool of resources stored as items in a stack.
Definition pool.h:205
Pool item node in the stack of items.
Definition pool.h:185
File handle node in the stack of handles.
Definition file.h:231
FILE * file
Open read handle to the source data file.
Definition file.h:232
fiftyoneDegreesPoolItem item
The pool item with the resource.
Definition file.h:233