1 /******************************************************************************
4 * Company XS Embedded GmbH
5 *****************************************************************************/
6 /******************************************************************************
7 * This Source Code Form is subject to the terms of the
8 * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
9 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 ******************************************************************************/
12 * @file persistence_client_library_file.c
13 * @ingroup Persistence client library
14 * @author Ingo Huerner
15 * @brief Implementation of the persistence client library.
16 * Library provides an API to access persistent data
20 #include "persistence_client_library_file.h"
21 #include "../include_protected/persistence_client_library_data_organization.h"
22 #include "../include_protected/persistence_client_library_db_access.h"
24 #include "persistence_client_library_pas_interface.h"
25 #include "persistence_client_library_handle.h"
26 #include "persistence_client_library_prct_access.h"
28 #include <fcntl.h> // for open flags
33 #include <sys/types.h>
38 int pclFileClose(int fd)
42 if(fd < MaxPersHandle)
44 __sync_fetch_and_sub(&gOpenFdArray[fd], FileClosed); // set closed flag
52 int pclFileGetSize(int fd)
58 ret = fstat(fd, &buf);
69 void* pclFileMapData(void* addr, long size, long offset, int fd)
73 if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
75 int mapFlag = PROT_WRITE | PROT_READ;
76 ptr = mmap(addr,size, mapFlag, MAP_SHARED, fd, offset);
80 ptr = EPERS_MAP_LOCKFS;
87 int pclFileOpen(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no)
89 int handle = -1, shared_DB = 0, flags = O_RDWR;
91 PersistenceInfo_s dbContext;
93 char dbKey[DbKeyMaxLen]; // database key
94 char dbPath[DbPathMaxLen]; // database location
96 memset(dbKey, 0, DbKeyMaxLen);
97 memset(dbPath, 0, DbPathMaxLen);
99 dbContext.context.ldbid = ldbid;
100 dbContext.context.seat_no = seat_no;
101 dbContext.context.user_no = user_no;
103 // get database context: database path and database key
104 shared_DB = get_db_context(&dbContext, resource_id, ResIsFile, dbKey, dbPath);
106 if(shared_DB != -1) // check valid database context
108 handle = open(dbPath, flags);
111 if(handle < MaxPersHandle)
113 __sync_fetch_and_add(&gOpenFdArray[handle], FileOpen); // set open flag
118 handle = EPERS_MAXHANDLE;
123 // file does not exist, create file and folder
125 const char* delimiters = "/\n"; // search for blank and end of line
126 char* tokenArray[24];
127 char createPath[DbPathMaxLen];
132 tokenArray[numTokens++] = strtok(dbPath, delimiters);
133 while(tokenArray[numTokens-1] != NULL )
135 tokenArray[numTokens] = strtok(NULL, delimiters);
136 if(tokenArray[numTokens] != NULL)
153 memset(createPath, 0, DbPathMaxLen);
154 snprintf(createPath, DbPathMaxLen, "/%s",tokenArray[0] );
155 for(i=1; i<numTokens-1; i++)
158 strncat(createPath, "/", DbPathMaxLen-1);
159 strncat(createPath, tokenArray[i], DbPathMaxLen-1);
160 mkdir(createPath, 0744);
162 // finally create the file
163 strncat(createPath, "/", DbPathMaxLen-1);
164 strncat(createPath, tokenArray[i], DbPathMaxLen-1);
165 handle = open(createPath, O_CREAT|O_RDWR |O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
168 if(handle < MaxPersHandle)
170 __sync_fetch_and_add(&gOpenFdArray[handle], FileOpen); // set open flag
175 handle = EPERS_MAXHANDLE;
181 printf("file_open ==> no valid path to create: %s\n", dbPath);
191 int pclFileReadData(int fd, void * buffer, int buffer_size)
193 return read(fd, buffer, buffer_size);
198 int pclFileRemove(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no)
202 if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
205 PersistenceInfo_s dbContext;
207 char dbKey[DbKeyMaxLen]; // database key
208 char dbPath[DbPathMaxLen]; // database location
210 memset(dbKey, 0, DbKeyMaxLen);
211 memset(dbPath, 0, DbPathMaxLen);
213 dbContext.context.ldbid = ldbid;
214 dbContext.context.seat_no = seat_no;
215 dbContext.context.user_no = user_no;
217 // get database context: database path and database key
218 shared_DB = get_db_context(&dbContext, resource_id, ResIsFile, dbKey, dbPath);
220 if(shared_DB != -1) // check valid database context
222 rval = remove(dbPath);
225 printf("file_remove ERROR: %s \n", strerror(errno) );
239 int pclFileSeek(int fd, long int offset, int whence)
243 if(AccessNoLock == isAccessLocked() ) // check if access to persistent data is locked
245 rval = lseek(fd, offset, whence);
257 int pclFileUnmapData(void* address, long size)
261 if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
263 rval = munmap(address, size);
275 int pclFileWriteData(int fd, const void * buffer, int buffer_size)
279 if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
281 size = write(fd, buffer, buffer_size);