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
143
144#ifndef FIFTYONE_DEGREES_FILE_H_INCLUDED
145#define FIFTYONE_DEGREES_FILE_H_INCLUDED
146
147#ifdef FIFTYONE_DEGREES_IPI_SYMBOLS
148#include "ipiSymbols.h"
149#endif
150
151/* Define NDEBUG if needed, to ensure asserts are disabled in release builds */
152#if !defined(DEBUG) && !defined(_DEBUG) && !defined(NDEBUG)
153#define NDEBUG
154#endif
155
156#include <limits.h>
157#include <stdio.h>
158#include <errno.h>
159#include <stdint.h>
160#include <stdbool.h>
161#ifdef _MSC_VER
162#pragma warning (push)
163#pragma warning (disable: 5105)
164#include <windows.h>
165#pragma warning (default: 5105)
166#pragma warning (pop)
167#include <direct.h>
168#include <tchar.h>
169#else
170#include <dirent.h>
171#include <sys/stat.h>
172#endif
173#include <time.h>
174#include <assert.h>
175#include <limits.h>
176#include <time.h>
177#include "data.h"
178#include "fileOffset.h"
179#include "exceptions.h"
180#include "status.h"
181#include "memory.h"
182#include "pool.h"
183#include "threading.h"
184#include "common.h"
185
189#ifdef _MSC_FULL_VER
190#define GET_CURRENT_DIR _getcwd
191#pragma warning (push)
192#pragma warning (disable: 5105)
193#include <windows.h>
194#pragma warning (default: 5105)
195#pragma warning (pop)
196#include <direct.h>
197#include <tchar.h>
198#else
199#define GET_CURRENT_DIR getcwd
200#include <unistd.h>
201#include <dirent.h>
202#include <sys/stat.h>
203#endif
204
205#define TEMP_UNIQUE_STRING_LENGTH 20
206
215#ifdef _MSC_FULL_VER
216#define FIFTYONE_DEGREES_FILE_MAX_PATH 260
217#else
218#define FIFTYONE_DEGREES_FILE_MAX_PATH 4096
219#endif
220
221
225typedef union fiftyone_degrees_file_handle_t {
226 FILE *file;
229
233 typedef struct fiftyone_degrees_file_pool_t {
237
246 FILE *stream,
248 int origin);
249
256 FILE *stream
257);
258
265
274 const char* fileName,
275 FILE** handle);
276
285 const char* fileName,
286 const void *data,
287 const size_t length);
288
299 const char *source,
300 const char *destination);
301
309 const char *fileName);
310
318 const char *pathName);
319
336 const char *dataFolderName,
337 const char *fileName,
338 char *destination,
339 size_t size);
340
358 const char *masterFile,
359 const char **paths,
360 int count,
361 fiftyoneDegreesFileOffset bytesToCompare,
362 const char *destination);
363
382 const char *masterFileName,
383 const char **paths,
384 int count,
385 fiftyoneDegreesFileOffset bytesToCompare);
386
396 const char* masterFileName,
397 char* destination,
398 size_t nameStart,
399 size_t length);
400
415 const char* masterFile,
416 const char** paths,
417 int count,
418 char* destination,
419 size_t length);
420
435 const char *masterFile,
436 const char **paths,
437 int count,
438 const char *destination);
439
452 fiftyoneDegreesFilePool *filePool,
453 const char *fileName,
454 uint16_t concurrency,
455 fiftyoneDegreesException *exception);
456
468 fiftyoneDegreesFilePool *filePool,
469 fiftyoneDegreesException *exception);
470
478
486
496 const char *fileName,
498
504
510EXTERNAL const char* fiftyoneDegreesFileGetFileName(const char *filePath);
511
518EXTERNAL const char* fiftyoneDegreesFileGetBaseName(const char *path);
519
523#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.
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:233
fiftyoneDegreesPool pool
The pool of file handles.
Definition file.h:234
fiftyoneDegreesFileOffset length
Length of the file in bytes.
Definition file.h:235
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:202
Pool item node in the stack of items.
Definition pool.h:182
File handle node in the stack of handles.
Definition file.h:225
FILE * file
Open read handle to the source data file.
Definition file.h:226
fiftyoneDegreesPoolItem item
The pool item with the resource.
Definition file.h:227