\r\n

51Degrees Device Detection C/C++  4.5

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 2023 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 
144 #ifndef FIFTYONE_DEGREES_FILE_H_INCLUDED
145 #define FIFTYONE_DEGREES_FILE_H_INCLUDED
146 
147 /* Define NDEBUG if needed, to ensure asserts are disabled in release builds */
148 #if !defined(DEBUG) && !defined(_DEBUG) && !defined(NDEBUG)
149 #define NDEBUG
150 #endif
151 
152 #include <limits.h>
153 #include <stdio.h>
154 #include <errno.h>
155 #include <stdint.h>
156 #include <stdbool.h>
157 #ifdef _MSC_VER
158 #pragma warning (push)
159 #pragma warning (disable: 5105)
160 #include <windows.h>
161 #pragma warning (default: 5105)
162 #pragma warning (pop)
163 #include <direct.h>
164 #include <tchar.h>
165 #else
166 #include <dirent.h>
167 #include <sys/stat.h>
168 #endif
169 #include <time.h>
170 #include <assert.h>
171 #include <limits.h>
172 #include <time.h>
173 #include "data.h"
174 #include "fileOffset.h"
175 #include "exceptions.h"
176 #include "status.h"
177 #include "memory.h"
178 #include "pool.h"
179 #include "threading.h"
180 #include "common.h"
181 
185 #ifdef _MSC_FULL_VER
186 #define GET_CURRENT_DIR _getcwd
187 #pragma warning (push)
188 #pragma warning (disable: 5105)
189 #include <windows.h>
190 #pragma warning (default: 5105)
191 #pragma warning (pop)
192 #include <direct.h>
193 #include <tchar.h>
194 #else
195 #define GET_CURRENT_DIR getcwd
196 #include <unistd.h>
197 #include <dirent.h>
198 #include <sys/stat.h>
199 #endif
200 
201 #define TEMP_UNIQUE_STRING_LENGTH 20
202 
211 #ifdef _MSC_FULL_VER
212 #define FIFTYONE_DEGREES_FILE_MAX_PATH 260
213 #else
214 #define FIFTYONE_DEGREES_FILE_MAX_PATH 4096
215 #endif
216 
217 
221 typedef union fiftyone_degrees_file_handle_t {
222  FILE *file;
225 
229  typedef struct fiftyone_degrees_file_pool_t {
233 
241 EXTERNAL int fiftyoneDegreesFileSeek(
242  FILE *stream,
244  int origin);
245 
252  FILE *stream
253 );
254 
261 
270  const char* fileName,
271  FILE** handle);
272 
281  const char* fileName,
282  const void *data,
283  const size_t length);
284 
295  const char *source,
296  const char *destination);
297 
305  const char *fileName);
306 
314  const char *pathName);
315 
332  const char *dataFolderName,
333  const char *fileName,
334  char *destination,
335  size_t size);
336 
354  const char *masterFile,
355  const char **paths,
356  int count,
357  fiftyoneDegreesFileOffset bytesToCompare,
358  const char *destination);
359 
378  const char *masterFileName,
379  const char **paths,
380  int count,
381  fiftyoneDegreesFileOffset bytesToCompare);
382 
392  const char* masterFileName,
393  char* destination,
394  size_t nameStart,
395  size_t length);
396 
411  const char* masterFile,
412  const char** paths,
413  int count,
414  char* destination,
415  size_t length);
416 
431  const char *masterFile,
432  const char **paths,
433  int count,
434  const char *destination);
435 
448  fiftyoneDegreesFilePool *filePool,
449  const char *fileName,
450  uint16_t concurrency,
451  fiftyoneDegreesException *exception);
452 
464  fiftyoneDegreesFilePool *filePool,
465  fiftyoneDegreesException *exception);
466 
473  fiftyoneDegreesFileHandle* handle);
474 
481 EXTERNAL fiftyoneDegreesFileOffset fiftyoneDegreesFileGetSize(const char *fileName);
482 
492  const char *fileName,
494 
500 
506 EXTERNAL const char* fiftyoneDegreesFileGetFileName(const char *filePath);
507 
511 #endif
fiftyoneDegreesFileOffset fiftyoneDegreesFileTell(FILE *stream)
Gets the current position of a file pointer.
const char * fiftyoneDegreesFileGetFileName(const char *filePath)
Gets the last, file name, segment of the full file path.
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.
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.
fiftyoneDegreesStatusCode
Status returned from the initialisation of a resource.
Definition: status.h:77
Pool item node in the stack of items.
Definition: pool.h:178
fiftyoneDegreesStatusCode fiftyoneDegreesFileAddTempFileName(const char *masterFileName, char *destination, size_t nameStart, size_t length)
Create a temporary file name and add it to the destination.
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.
Used to read data from memory in a similar manner to a file handle.
Definition: memory.h:54
FILE * file
Open read handle to the source data file.
Definition: file.h:222
fiftyoneDegreesPoolItem item
The pool item with the resource.
Definition: file.h:223
void fiftyoneDegreesFilePoolReset(fiftyoneDegreesFilePool *filePool)
Resets the pool without releasing any resources.
fiftyoneDegreesStatusCode fiftyoneDegreesFileOpen(const char *fileName, FILE **handle)
Opens the file path provided placing the file handle in the handle parameter.
fiftyoneDegreesPool pool
The pool of file handles.
Definition: file.h:230
fiftyoneDegreesFileHandle * fiftyoneDegreesFileHandleGet(fiftyoneDegreesFilePool *filePool, fiftyoneDegreesException *exception)
Retrieves a read only open file handle from the pool.
Pool of resources stored as items in a stack.
Definition: pool.h:198
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...
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.
fiftyoneDegreesFileOffset length
Length of the file in bytes.
Definition: file.h:231
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.
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 fiftyoneDegreesFilePoolRelease(fiftyoneDegreesFilePool *pool)
Releases the file handles contained in the pool and frees any internal memory used by the pool.
long fiftyoneDegreesFileOffset
Type for file offset in API.
Definition: fileOffset.h:69
File handle node in the stack of handles.
Definition: file.h:221
fiftyoneDegreesStatusCode fiftyoneDegreesFileReadToByteArray(const char *fileName, fiftyoneDegreesMemoryReader *reader)
Reads the contents of a file into memory.
fiftyoneDegreesStatusCode fiftyoneDegreesFileCreateDirectory(const char *pathName)
Creates a directory with the specified path, and returns the result of the operation.
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition: exceptions.h:111
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.
Stack of handles used to read data from a single source file.
Definition: file.h:229
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...