Select Git revision
db-device.c
db-device.c 22.94 KiB
/* db - Database for xAAL Agent
* Part of the xaaws software
* (c) 2019 Christophe Lohr <christophe.lohr@imt-atlantique.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <errno.h>
#include <uuid/uuid.h>
#include <regex.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <xaal_jc.h>
#include "db-device.h"
#include "db-schema.h"
/*
* Section Devices
*/
/* load a DB of devices from a cbor file */
void load_devices(devices_t *devices, char *file) {
struct cbor_load_result cresult;
cbor_item_t *cdevices;
unsigned char *file_map;
struct stat sb;
size_t i, sz;
int fd;
if (!file) return;
fd = open(file, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Could not open(r) database file %s. %s\n", file, strerror(errno));
return;
}
if (fstat(fd, &sb) == -1) { /* get file size */
fprintf(stderr, "Could not do fstat on database file %s. %s\n", file, strerror(errno));
return;
}
file_map = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (file_map == MAP_FAILED) {
fprintf(stderr, "Could not mmap database file %s. %s\n", file, strerror(errno));
return;
}
cdevices = cbor_load(file_map, sb.st_size, &cresult);
munmap(file_map, sb.st_size);