Changed file and key-value API according GENIVI naming conventions
[profile/ivi/persistence-client-library.git] / src / persistence_client_library_file.c
1 /******************************************************************************
2  * Project         Persistency
3  * (c) copyright   2012
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 ******************************************************************************/
11  /**
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
17  * @see            
18  */
19
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"
23
24 #include "persistence_client_library_pas_interface.h"
25 #include "persistence_client_library_handle.h"
26 #include "persistence_client_library_prct_access.h"
27
28 #include <fcntl.h>   // for open flags
29 #include <errno.h>
30 #include <string.h>
31 #include <sys/mman.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36
37
38 int pclFileClose(int fd)
39 {
40    int rval = -1;
41
42    if(fd < MaxPersHandle)
43    {
44       __sync_fetch_and_sub(&gOpenFdArray[fd], FileClosed);   // set closed flag
45       rval = close(fd);
46    }
47    return rval;
48 }
49
50
51
52 int pclFileGetSize(int fd)
53 {
54    int rval = -1;
55
56    struct stat buf;
57    int ret = 0;
58    ret = fstat(fd, &buf);
59
60    if(ret != -1)
61    {
62       rval = buf.st_size;
63    }
64    return rval;
65 }
66
67
68
69 void* pclFileMapData(void* addr, long size, long offset, int fd)
70 {
71    void* ptr = 0;
72
73    if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
74    {
75       int mapFlag = PROT_WRITE | PROT_READ;
76       ptr = mmap(addr,size, mapFlag, MAP_SHARED, fd, offset);
77    }
78    else
79    {
80       ptr = EPERS_MAP_LOCKFS;
81    }
82    return ptr;
83 }
84
85
86
87 int pclFileOpen(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no)
88 {
89    int handle = -1, shared_DB = 0, flags = O_RDWR;
90
91    PersistenceInfo_s dbContext;
92
93    char dbKey[DbKeyMaxLen];      // database key
94    char dbPath[DbPathMaxLen];    // database location
95
96    memset(dbKey, 0, DbKeyMaxLen);
97    memset(dbPath, 0, DbPathMaxLen);
98
99    dbContext.context.ldbid   = ldbid;
100    dbContext.context.seat_no = seat_no;
101    dbContext.context.user_no = user_no;
102
103    // get database context: database path and database key
104    shared_DB = get_db_context(&dbContext, resource_id, ResIsFile, dbKey, dbPath);
105
106    if(shared_DB != -1)  // check valid database context
107    {
108       handle = open(dbPath, flags);
109       if(handle != -1)
110       {
111          if(handle < MaxPersHandle)
112          {
113             __sync_fetch_and_add(&gOpenFdArray[handle], FileOpen); // set open flag
114          }
115          else
116          {
117             close(handle);
118             handle = EPERS_MAXHANDLE;
119          }
120       }
121       else
122       {
123          // file does not exist, create file and folder
124
125          const char* delimiters = "/\n";   // search for blank and end of line
126          char* tokenArray[24];
127          char createPath[DbPathMaxLen];
128          int numTokens = 0;
129          int i = 0;
130          int validPath = 1;
131
132          tokenArray[numTokens++] = strtok(dbPath, delimiters);
133          while(tokenArray[numTokens-1] != NULL )
134          {
135            tokenArray[numTokens] = strtok(NULL, delimiters);
136            if(tokenArray[numTokens] != NULL)
137            {
138               numTokens++;
139               if(numTokens >= 24)
140               {
141                  validPath = 0;
142                  break;
143               }
144            }
145            else
146            {
147               break;
148            }
149          }
150
151          if(validPath == 1)
152          {
153             memset(createPath, 0, DbPathMaxLen);
154             snprintf(createPath, DbPathMaxLen, "/%s",tokenArray[0] );
155             for(i=1; i<numTokens-1; i++)
156             {
157                // create folders
158                strncat(createPath, "/", DbPathMaxLen-1);
159                strncat(createPath, tokenArray[i], DbPathMaxLen-1);
160                mkdir(createPath, 0744);
161             }
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);
166             if(handle != -1)
167             {
168                if(handle < MaxPersHandle)
169                {
170                   __sync_fetch_and_add(&gOpenFdArray[handle], FileOpen); // set open flag
171                }
172                else
173                {
174                   close(handle);
175                   handle = EPERS_MAXHANDLE;
176                }
177             }
178          }
179          else
180          {
181             printf("file_open ==> no valid path to create: %s\n", dbPath);
182          }
183       }
184    }
185
186    return handle;
187 }
188
189
190
191 int pclFileReadData(int fd, void * buffer, int buffer_size)
192 {
193    return read(fd, buffer, buffer_size);
194 }
195
196
197
198 int pclFileRemove(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no)
199 {
200    int rval = 0;
201
202    if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
203    {
204       int shared_DB = 0;
205       PersistenceInfo_s dbContext;
206
207       char dbKey[DbKeyMaxLen];      // database key
208       char dbPath[DbPathMaxLen];    // database location
209
210       memset(dbKey, 0, DbKeyMaxLen);
211       memset(dbPath, 0, DbPathMaxLen);
212
213       dbContext.context.ldbid   = ldbid;
214       dbContext.context.seat_no = seat_no;
215       dbContext.context.user_no = user_no;
216
217       // get database context: database path and database key
218       shared_DB = get_db_context(&dbContext, resource_id, ResIsFile, dbKey, dbPath);
219
220       if(shared_DB != -1)  // check valid database context
221       {
222          rval = remove(dbPath);
223          if(rval == -1)
224          {
225             printf("file_remove ERROR: %s \n", strerror(errno) );
226          }
227       }
228    }
229    else
230    {
231       rval = EPERS_LOCKFS;
232    }
233
234    return rval;
235 }
236
237
238
239 int pclFileSeek(int fd, long int offset, int whence)
240 {
241    int rval = 0;
242
243    if(AccessNoLock == isAccessLocked() ) // check if access to persistent data is locked
244    {
245       rval = lseek(fd, offset, whence);
246    }
247    else
248    {
249       rval = EPERS_LOCKFS;
250    }
251
252    return rval;
253 }
254
255
256
257 int pclFileUnmapData(void* address, long size)
258 {
259    int rval = 0;
260
261    if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
262    {
263       rval =  munmap(address, size);
264    }
265    else
266    {
267       rval = EPERS_LOCKFS;
268    }
269
270    return rval;
271 }
272
273
274
275 int pclFileWriteData(int fd, const void * buffer, int buffer_size)
276 {
277    int size = 0;
278
279    if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
280    {
281       size = write(fd, buffer, buffer_size);
282    }
283    else
284    {
285       size = EPERS_LOCKFS;
286    }
287
288    return size;
289 }
290
291