\r\n

51Degrees Device Detection C/C++  4.3

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 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
4  * Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
5  *
6  * This Original Work is licensed under the European Union Public Licence (EUPL)
7  * 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 "exceptions.h"
175 #include "status.h"
176 #include "memory.h"
177 #include "pool.h"
178 #include "threading.h"
179 
183 #ifdef _MSC_FULL_VER
184 #define GET_CURRENT_DIR _getcwd
185 #pragma warning (push)
186 #pragma warning (disable: 5105)
187 #include <windows.h>
188 #pragma warning (default: 5105)
189 #pragma warning (pop)
190 #include <direct.h>
191 #include <tchar.h>
192 #else
193 #define GET_CURRENT_DIR getcwd
194 #include <unistd.h>
195 #include <dirent.h>
196 #include <sys/stat.h>
197 #endif
198 
207 #ifdef _MSC_FULL_VER
208 #define FIFTYONE_DEGREES_FILE_MAX_PATH 260
209 #else
210 #define FIFTYONE_DEGREES_FILE_MAX_PATH 4096
211 #endif
212 
213 #ifdef __cplusplus
214 #define EXTERNAL extern "C"
215 #else
216 #define EXTERNAL
217 #endif
218 
222 typedef union fiftyone_degrees_file_handle_t {
223  FILE *file;
226 
230  typedef struct fiftyone_degrees_file_pool_t {
232  long length;
234 
241 
250  const char* fileName,
251  FILE** handle);
252 
261  const char* fileName,
262  const void *data,
263  const size_t length);
264 
275  const char *source,
276  const char *destination);
277 
285  const char *fileName);
286 
294  const char *pathName);
295 
312  const char *dataFolderName,
313  const char *fileName,
314  char *destination,
315  size_t size);
316 
334  const char *masterFile,
335  const char **paths,
336  int count,
337  long bytesToCompare,
338  const char *destination);
339 
358  const char *masterFileName,
359  const char **paths,
360  int count,
361  long bytesToCompare);
362 
376  const char *masterFile,
377  const char **paths,
378  int count,
379  const char *destination);
380 
393  fiftyoneDegreesFilePool *filePool,
394  const char *fileName,
395  uint16_t concurrency,
396  fiftyoneDegreesException *exception);
397 
409  fiftyoneDegreesFilePool *filePool,
410  fiftyoneDegreesException *exception);
411 
418  fiftyoneDegreesFileHandle* handle);
419 
426 EXTERNAL long fiftyoneDegreesFileGetSize(const char *fileName);
427 
437  const char *fileName,
439 
445 
451 EXTERNAL const char* fiftyoneDegreesFileGetFileName(const char *filePath);
452 
456 #endif
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...
const char * fiftyoneDegreesFileGetFileName(const char *filePath)
Gets the last, file name, segment of the full file path.
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.
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
Status returned from the initialisation of a resource.
Definition: status.h:82
Pool item node in the stack of items.
Definition: pool.h:183
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...
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.
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:58
FILE * file
Open read handle to the source data file.
Definition: file.h:223
fiftyoneDegreesPoolItem item
The pool item with the resource.
Definition: file.h:224
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:231
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:203
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.
void fiftyoneDegreesFilePoolRelease(fiftyoneDegreesFilePool *pool)
Releases the file handles contained in the pool and frees any internal memory used by the pool.
File handle node in the stack of handles.
Definition: file.h:222
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.
long length
Length of the file in bytes.
Definition: file.h:232
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition: exceptions.h:116
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:230
fiftyoneDegreesStatusCode fiftyoneDegreesFileCopy(const char *source, const char *destination)
Copy a file from one location to another.
int fiftyoneDegreesFileDeleteUnusedTempFiles(const char *masterFileName, const char **paths, int count, long bytesToCompare)
Finds all the temporary files which is an exact copy of the master file if any exist.