\r\n

51Degrees Device Detection C/C++  4.4

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 #include "common.h"
180 
184 #ifdef _MSC_FULL_VER
185 #define GET_CURRENT_DIR _getcwd
186 #pragma warning (push)
187 #pragma warning (disable: 5105)
188 #include <windows.h>
189 #pragma warning (default: 5105)
190 #pragma warning (pop)
191 #include <direct.h>
192 #include <tchar.h>
193 #else
194 #define GET_CURRENT_DIR getcwd
195 #include <unistd.h>
196 #include <dirent.h>
197 #include <sys/stat.h>
198 #endif
199 
200 #define TEMP_UNIQUE_STRING_LENGTH 20
201 
210 #ifdef _MSC_FULL_VER
211 #define FIFTYONE_DEGREES_FILE_MAX_PATH 260
212 #else
213 #define FIFTYONE_DEGREES_FILE_MAX_PATH 4096
214 #endif
215 
219 typedef union fiftyone_degrees_file_handle_t {
220  FILE *file;
223 
227  typedef struct fiftyone_degrees_file_pool_t {
229  long length;
231 
238 
247  const char* fileName,
248  FILE** handle);
249 
258  const char* fileName,
259  const void *data,
260  const size_t length);
261 
272  const char *source,
273  const char *destination);
274 
282  const char *fileName);
283 
291  const char *pathName);
292 
309  const char *dataFolderName,
310  const char *fileName,
311  char *destination,
312  size_t size);
313 
331  const char *masterFile,
332  const char **paths,
333  int count,
334  long bytesToCompare,
335  const char *destination);
336 
355  const char *masterFileName,
356  const char **paths,
357  int count,
358  long bytesToCompare);
359 
369  const char* masterFileName,
370  char* destination,
371  size_t nameStart,
372  size_t length);
373 
388  const char* masterFile,
389  const char** paths,
390  int count,
391  char* destination,
392  size_t length);
393 
408  const char *masterFile,
409  const char **paths,
410  int count,
411  const char *destination);
412 
425  fiftyoneDegreesFilePool *filePool,
426  const char *fileName,
427  uint16_t concurrency,
428  fiftyoneDegreesException *exception);
429 
441  fiftyoneDegreesFilePool *filePool,
442  fiftyoneDegreesException *exception);
443 
450  fiftyoneDegreesFileHandle* handle);
451 
458 EXTERNAL long fiftyoneDegreesFileGetSize(const char *fileName);
459 
469  const char *fileName,
471 
477 
483 EXTERNAL const char* fiftyoneDegreesFileGetFileName(const char *filePath);
484 
488 #endif
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: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...
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:53
FILE * file
Open read handle to the source data file.
Definition: file.h:220
fiftyoneDegreesPoolItem item
The pool item with the resource.
Definition: file.h:221
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:228
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...
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:219
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:229
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition: exceptions.h:111
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:227
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 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.