obidms.c 42.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
/********************************************************************
 * OBIDMS functions                                                 *
 ********************************************************************/

/**
 * @file obidms.c
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 * @date 23 May 2015
 * @brief OBIDMS functions.
10 11
 */

12

13 14
#include <stdlib.h>
#include <stdio.h>
15
#include <string.h>
16
#include <stdbool.h>
17
#include <sys/stat.h>
18
#include <fcntl.h>
19 20
#include <sys/types.h>
#include <dirent.h>
21
#include <unistd.h>
22
#include <libgen.h>   /* <EC> : Added July 28th 2017 to include basename */
23
#include <sys/mman.h>
24
#include <math.h>
25 26

#include "obidms.h"
27
#include "obierrno.h"
28 29
#include "obidebug.h"
#include "obidmscolumn.h"
30
#include "obiview.h"
31
#include "obiblob_indexer.h"
32
#include "utils.h"
33
#include "obilittlebigman.h"
34
#include "libjson/json_utils.h"
35

36

37
#define DEBUG_LEVEL 0	// TODO has to be defined somewhere else (cython compil flag?)
38

39

40
// Initialize global list of opened DMS and their associated counters
41
OBIDMS_p global_opened_dms_list[MAX_NB_OPENED_DMS+1] = { 0 };
42 43
int global_opened_dms_counter_list[MAX_NB_OPENED_DMS+1] = { 0 };

44 45


46
/**************************************************************************
47
 *
48
 * D E C L A R A T I O N   O F   T H E   P R I V A T E   F U N C T I O N S
49
 *
50
 **************************************************************************/
51 52


53
/**
54
 * Internal function building the OBIDMS directory name from an OBIDMS name.
55
 *
56
 * The function builds the directory name corresponding to an OBIDMS.
57
 * It also checks that the name is not too long.
58 59 60
 *
 * @warning The returned pointer has to be freed by the caller.
 *
61
 * @param dms_name The name of the OBIDMS.
62
 *
63 64
 * @returns A pointer to the directory name.
 * @retval NULL if an error occurred.
65 66 67 68
 *
 * @since May 2015
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 */
69
static char* build_directory_name(const char* dms_name);
70 71


72 73 74 75 76 77 78 79 80
/**
 * Internal function building the informations file name from an OBIDMS name.
 *
 * The function builds the file name for the informations file of an OBIDMS.
 *
 * @warning The returned pointer has to be freed by the caller.
 *
 * @param dms_name The name of the OBIDMS.
 *
81
 * @returns A pointer on the file name.
82 83 84 85 86 87 88 89
 * @retval NULL if an error occurred.
 *
 * @since November 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static char* build_infos_file_name(const char* dms_name);


90 91 92 93 94 95 96 97
/**
 * Internal function calculating the initial size of the file where the informations about a DMS are stored.
 *
 * @returns The initial size of the file in bytes, rounded to a multiple of page size.
 *
 * @since September 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
98
static size_t get_platform_infos_file_size(void);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147


/**
 * @brief Internal function enlarging a DMS information file.
 *
 * @param dms A pointer on the DMS.
 * @param new_size The new size needed, in bytes (not necessarily rounded to a page size multiple).
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since September 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int enlarge_infos_file(OBIDMS_p dms, size_t new_size);


/**
 * @brief Internal function mapping a DMS information file and returning the mapped structure stored in it.
 *
 * The function checks that endianness of the platform and of the DMS match.
 *
 * @param dms_file_descriptor The file descriptor of the DMS directory.
 * @param dms_name The base name of the DMS.
 *
 * @returns A pointer on the mapped DMS infos structure.
 * @retval NULL if an error occurred.
 *
 * @since September 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static OBIDMS_infos_p map_infos_file(int dms_file_descriptor, const char* dms_name);


/**
 * @brief Unmaps a DMS information file.
 *
 * @param dms A pointer on the OBIDMS.
 *
 * @returns A value indicating the success of the operation.
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since October 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int unmap_infos_file(OBIDMS_p dms);


148 149 150
/**
 * Internal function creating the file containing basic informations on the OBIDMS.
 *
151
 * This file contains an OBIDMS_infos structure.
152 153 154
 *
 * @warning The returned pointer has to be freed by the caller.
 *
155 156
 * @param dms_file_descriptor The file descriptor of the DMS directory.
 * @param dms_name The base name of the DMS.
157 158 159 160 161 162 163
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since November 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
164
static int create_dms_infos_file(int dms_file_descriptor, const char* dms_name);
165 166


167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/**
 * Internal function adding a DMS in the global list of DMS opened by a program.
 *
 * @param dms A pointer on the DMS to add.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since October 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int list_dms(OBIDMS_p dms);


/**
 * Internal function removing a DMS from the global list of DMS opened by a program.
 *
 * @param dms A pointer on the DMS to remove.
185 186 187 188
 * @param force Whether the DMS should be unlisted even if it is opened more than once.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if the DMS was not found.
189 190 191 192
 *
 * @since October 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
static int unlist_dms(OBIDMS_p dms, bool force);


/**
 * Internal function checking if a DMS is already in the global list of DMS opened by the program.
 *
 * @param full_dms_path The absolute path to the DMS directory.
 *
 * @returns A pointer on the DMS if it is already opened, or NULL if it was not opened.
 *
 * @since November 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static OBIDMS_p check_if_dms_in_list(const char* full_dms_path);


/**
 * Internal function returning the count for an opened DMS (how many times the DMS is opened) in the global list of DMS opened by the program.
 *
 * @param dms A pointer on the DMS.
 *
 * @returns The count indicating how many times the DMS has been opened by the program.
 *
 * @retval -1 if the DMS was not found.
 *
 * @since November 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int dms_count_in_list(OBIDMS_p dms);

223 224


225
/************************************************************************
226
 *
227
 * D E F I N I T I O N   O F   T H E   P R I V A T E   F U N C T I O N S
228
 *
229
 ************************************************************************/
230

231
static char* build_directory_name(const char* dms_name)
232
{
233
	char* directory_name;
234 235

	// Build the database directory name
Celine Mercier's avatar
Celine Mercier committed
236 237
	directory_name = (char*) malloc((strlen(dms_name) + 8)*sizeof(char));
	if (sprintf(directory_name, "%s.obidms", dms_name) < 0)
238 239
	{
		obi_set_errno(OBIDMS_MEMORY_ERROR);
240
		obidebug(1, "\nProblem building an OBIDMS directory name");
241 242 243 244
		return NULL;
	}

	// Test if the database name is not too long
245
	if (strlen(directory_name) >= OBIDMS_MAX_NAME)
246 247
	{
		obi_set_errno(OBIDMS_LONG_NAME_ERROR);
248
		obidebug(1, "\nProblem building an OBIDMS directory name");
249
		free(directory_name);
250 251 252
		return NULL;
	}

253
	return directory_name;
254 255
}

256

257 258 259 260 261
static char* build_infos_file_name(const char* dms_name)
{
	char* file_name;

	// Build file name
Celine Mercier's avatar
Celine Mercier committed
262 263
	file_name = (char*) malloc((strlen(dms_name) + 7)*sizeof(char));
	if (sprintf(file_name, "%s_infos", dms_name) < 0)
264 265 266 267 268 269 270 271 272 273
	{
		obi_set_errno(OBIDMS_MEMORY_ERROR);
		obidebug(1, "\nProblem building an informations file name");
		return NULL;
	}

	return file_name;
}


274
static size_t get_platform_infos_file_size()
275
{
276 277 278
	size_t infos_size;
	size_t rounded_infos_size;
	double multiple;
279

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	infos_size = sizeof(OBIDMS_infos_t);

	multiple = 	ceil((double) (infos_size) / (double) getpagesize());

	rounded_infos_size = multiple * getpagesize();

	return rounded_infos_size;
}


static int enlarge_infos_file(OBIDMS_p dms, size_t new_size)
{
	int    infos_file_descriptor;
	double multiple;
	size_t rounded_new_size;
	char*  file_name;
296 297

	// Create file name
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	file_name = build_infos_file_name(dms->dms_name);
	if (file_name == NULL)
		return -1;

    // Open infos file
	infos_file_descriptor = openat(dms->dir_fd, file_name, O_RDWR, 0777);
	if (infos_file_descriptor < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError opening a DMS information file");
		free(file_name);
		return -1;
	}

	free(file_name);

	// Round new size to a multiple of page size	// TODO make function in utils
	multiple = 	ceil((double) new_size / (double) getpagesize());
	rounded_new_size = multiple * getpagesize();

	// Enlarge the file
	if (ftruncate(infos_file_descriptor, rounded_new_size) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError enlarging a DMS information file");
		close(infos_file_descriptor);
		return -1;
	}

	// Unmap and remap the file
	if (munmap(dms->infos, (dms->infos)->file_size) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError munmapping a DMS information file when enlarging");
		close(infos_file_descriptor);
		return -1;
	}

	dms->infos = mmap(NULL,
					  rounded_new_size,
					  PROT_READ | PROT_WRITE,
					  MAP_SHARED,
					  infos_file_descriptor,
					  0
					  );
	if (dms->infos == MAP_FAILED)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError re-mmapping a DMS information file after enlarging the file");
		close(infos_file_descriptor);
		return -1;
	}

	// Set new size
	(dms->infos)->file_size = rounded_new_size;

	if (close(infos_file_descriptor) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError closing a DMS information file");
		return -1;
	}

	return 0;
}


static OBIDMS_infos_p map_infos_file(int dms_file_descriptor, const char* dms_name)
{
	char*				file_name;
	OBIDMS_infos_p		dms_infos;
	int 				file_descriptor;
	size_t  			min_size;
	size_t  			full_size;
	bool                little_endian_platform;

	// Build file name
	file_name = build_infos_file_name(dms_name);
	if (file_name == NULL)
		return NULL;

    // Open file
	file_descriptor = openat(dms_file_descriptor,
							 file_name,
							 O_RDWR, 0777);
	if (file_descriptor < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError opening a DMS information file (DMS %s)", dms_name);
		free(file_name);
		return NULL;
	}

	free(file_name);

	// Map minimum size to read endianness and file size
	// (fread() fails to read properly from the structure in the file because of padding)
	min_size = get_platform_infos_file_size();

	dms_infos = mmap(NULL,
					 min_size,
					 PROT_READ | PROT_WRITE,
					 MAP_SHARED,
					 file_descriptor,
					 0
	   	   	 	 	);
	if (dms_infos == NULL)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError mapping a DMS information file");
		return NULL;
	}

	// Check endianness of the platform and DMS
	little_endian_platform = obi_is_little_endian();
	if (little_endian_platform != dms_infos->little_endian)
	{
		obi_set_errno(OBIDMS_BAD_ENDIAN_ERROR);
		obidebug(1, "\nError: The DMS and the platform have different endianness");
		close(file_descriptor);
		return NULL;
	}

	// Read actual file size
	full_size = dms_infos->file_size;

	// Unmap the minimum size and remap the file with the full size
	if (munmap(dms_infos, min_size) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError munmapping a DMS information file");
		close(file_descriptor);
		return NULL;
	}

	dms_infos = mmap(NULL,
				 	 full_size,
					 PROT_READ | PROT_WRITE,
					 MAP_SHARED,
					 file_descriptor,
					 0
	   	   	 	 	);
	if (dms_infos == NULL)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError mapping a DMS information file");
		return NULL;
	}

	if (close(file_descriptor) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError closing a DMS information file");
		return NULL;
	}

	return dms_infos;
}


static int unmap_infos_file(OBIDMS_p dms)
{
	OBIDMS_infos_p dms_infos;
	char* 	       file_name;
	int 	       file_descriptor;
	size_t         file_size;

	dms_infos = dms->infos;

	// Build file name
	file_name = build_infos_file_name(dms->dms_name);
	if (file_name == NULL)
		return -1;

    // Open file
	file_descriptor = openat(dms->dir_fd,
							 file_name,
							 O_RDWR, 0777);
	if (file_descriptor < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError opening a DMS information file (DMS %s) to unmap it", dms->dms_name);
		free(file_name);
		return -1;
	}

	free(file_name);

	// Unmap the DMS infos structure
	file_size = dms_infos->file_size;
	if (munmap(dms_infos, file_size) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError unmapping a DMS information file");
		close(file_descriptor);
		return -1;
	}

	if (close(file_descriptor) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError closing a DMS information file");
		return -1;
	}

	return 0;
}


static int create_dms_infos_file(int dms_file_descriptor, const char* dms_name)
{
	char*				file_name;
	int 				infos_file_descriptor;
	size_t  			file_size;

	// Build file name
514 515 516 517 518
	file_name = build_infos_file_name(dms_name);
	if (file_name == NULL)
		return -1;

    // Create file
519 520
	infos_file_descriptor = openat(dms_file_descriptor,
								   file_name,
521 522
								   O_RDWR | O_CREAT | O_EXCL,
								   0777);
523 524 525
	if (infos_file_descriptor < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
526
		obidebug(1, "\nError creating a DMS information file (DMS %s)", dms_name);
527 528 529 530 531 532
		free(file_name);
		return -1;
	}

	free(file_name);

533 534 535
	// Truncate file to the initial size
	file_size = get_platform_infos_file_size();

536 537 538
	if (ftruncate(infos_file_descriptor, file_size) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
539
		obidebug(1, "\nError truncating a DMS information file to the right size");
540 541 542 543
		close(infos_file_descriptor);
		return -1;
	}

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
	// Map the DMS infos structure
	OBIDMS_infos_p infos = mmap(NULL,
				 	 file_size,
					 PROT_READ | PROT_WRITE,
					 MAP_SHARED,
					 infos_file_descriptor,
					 0
	   	   	 	 	);

	// Initialize values
	infos->little_endian = obi_is_little_endian();
	infos->file_size = file_size;
	infos->used_size = 0;
	infos->comments[0] = '\0';

	// Unmap the infos file
	if (munmap(infos, file_size) < 0)
561
	{
562 563
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError munmapping a DMS information file");
564 565 566
		return -1;
	}

567 568 569
	if (close(infos_file_descriptor) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
570
		obidebug(1, "\nError closing a view file");
571 572 573
		return -1;
	}

574 575 576
	return 0;
}

577

578 579 580
static int list_dms(OBIDMS_p dms)
{
	int i = 0;
581
	while ((global_opened_dms_list[i] != NULL) && (global_opened_dms_list[i] != dms))
582 583 584 585 586 587 588
		i++;
	if (i == (MAX_NB_OPENED_DMS-1))
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError opening a DMS: maximum number of DMS opened by a program reached");
		return -1;
	}
589 590 591
	if (global_opened_dms_list[i] == NULL)
	{    // new dms
		global_opened_dms_list[i] = dms;
592
		global_opened_dms_counter_list[i] = 1;
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	}
	else
	{    // already opened dms
		(global_opened_dms_counter_list[i])++;
	}

	return 0;
}


static int unlist_dms(OBIDMS_p dms, bool force)
{
	int i = 0;
	while ((global_opened_dms_list[i] != dms) && (i <= MAX_NB_OPENED_DMS))
		i++;
	if (i == MAX_NB_OPENED_DMS)
		return -1; // DMS not found
	// If opened more than once, and the unlisting is not forced, decrement counter
	if ((global_opened_dms_counter_list[i] > 1) && (! force))
		(global_opened_dms_counter_list[i])--;
	else
	{	// If opened once or forced unlisting, delete and unlist by shifting the list
		while (global_opened_dms_list[i] != NULL)
		{
			global_opened_dms_list[i] = global_opened_dms_list[i+1];
			i++;
		}
	}
621 622 623 624
	return 0;
}


625
static OBIDMS_p check_if_dms_in_list(const char* full_dms_path)
626 627
{
	int i = 0;
628
	while ((i <= MAX_NB_OPENED_DMS) && (global_opened_dms_list[i] != NULL) && (strcmp(global_opened_dms_list[i]->directory_path, full_dms_path) != 0))
629
		i++;
630 631 632 633 634 635 636 637 638 639 640 641
	if (i == MAX_NB_OPENED_DMS)
		return NULL; // DMS not found
	if (global_opened_dms_list[i] != NULL)
		return global_opened_dms_list[i];
	return NULL;
}


static int dms_count_in_list(OBIDMS_p dms)
{
	int i = 0;
	while ((i <= MAX_NB_OPENED_DMS) && (global_opened_dms_list[i] != dms))
642
		i++;
643 644 645
	if (i == MAX_NB_OPENED_DMS)
		return -1; // DMS not found
	return global_opened_dms_counter_list[i];
646 647 648
}


649
/**********************************************************************
650
 *
651
 * D E F I N I T I O N   O F   T H E   P U B L I C   F U N C T I O N S
652
 *
653
 **********************************************************************/
654

655
int obi_dms_exists(const char* dms_path)
656
{
657
    struct stat buffer;
Celine Mercier's avatar
Celine Mercier committed
658 659
	char*       directory_name;
    int 		check_dir;
660 661

	// Build and check the directory name
662
    directory_name = build_directory_name(dms_path);
663
	if (directory_name == NULL)
664 665
		return -1;

666
	check_dir = stat(directory_name, &buffer);
667

668
	free(directory_name);
669

670
    if (check_dir == 0)
671
        return 1;
672 673
    else
    	return 0;
674 675 676
}


677
OBIDMS_p obi_create_dms(const char* dms_path)
678
{
679 680 681 682
	char*  			directory_name;
	DIR*  			dms_dir;
	int    			dms_file_descriptor;
	OBIDMS_p        dms;
683 684

	// Build and check the directory name
685
	directory_name = build_directory_name(dms_path);
686
	if (directory_name == NULL)
687 688 689
		return NULL;

	// Try to create the directory
690
	if (mkdir(directory_name, 00777) < 0)
691
	{
692
		if (errno == EEXIST)
693
		{
694
			obi_set_errno(OBIDMS_EXIST_ERROR);
695 696
			obidebug(1, "\nAn OBIDMS directory with the same name already exists in this directory.");
		}
697 698
		else
			obi_set_errno(OBIDMS_UNKNOWN_ERROR);
699
		obidebug(1, "\nProblem creating an OBIDMS directory");
700
		free(directory_name);
701 702 703
		return NULL;
	}

704
	// Get file descriptor of DMS directory to create other directories
705 706 707 708 709 710 711 712
	dms_dir = opendir(directory_name);
	if (dms_dir == NULL)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nProblem opening a newly created OBIDMS directory");
		free(directory_name);
		return NULL;
	}
713 714 715

	free(directory_name);

716 717 718 719 720 721 722 723
	dms_file_descriptor = dirfd(dms_dir);
	if (dms_file_descriptor < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nProblem getting the file descriptor of a newly created OBIDMS directory");
		return NULL;
	}

Celine Mercier's avatar
Celine Mercier committed
724 725
	// Create the indexer directory
	if (mkdirat(dms_file_descriptor, INDEXER_DIR_NAME, 00777) < 0)
726
	{
Celine Mercier's avatar
Celine Mercier committed
727
		obi_set_errno(OBI_INDEXER_ERROR);
728
		obidebug(1, "\nProblem creating the indexer directory");
729 730 731
		return NULL;
	}

732 733 734 735
	// Create the view directory
	if (mkdirat(dms_file_descriptor, VIEW_DIR_NAME, 00777) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
736 737 738 739 740 741 742 743 744
		obidebug(1, "\nProblem creating the view directory");
		return NULL;
	}

	// Create the taxonomy directory
	if (mkdirat(dms_file_descriptor, TAXONOMY_DIR_NAME, 00777) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nProblem creating the taxonomy directory");
745 746 747
		return NULL;
	}

748 749 750 751 752 753 754
	// Create the information file
	if (create_dms_infos_file(dms_file_descriptor, basename((char*)dms_path)) < 0)
		return NULL;

	// Open DMS
	dms = obi_open_dms(dms_path);
	if (dms == NULL)
755
	{
756 757
		obidebug(1, "\nProblem opening a DMS");
		return NULL;
758 759
	}

760 761 762 763
	// Write empty json string in comments (Python json lib doesn't like empty "")
	if (obi_dms_write_comments(dms, "{}") < 0)
	{
		obidebug(1, "\nProblem initializing empty json string in new DMS comments");
764
		return NULL;
765
	}
766

767
	return dms;
768 769
}

770

771
OBIDMS_p obi_open_dms(const char* dms_path)
772 773
{
	OBIDMS_p	dms;
774 775
	char*	 	relative_dms_path;
	char*	 	absolute_dms_path;
776 777

	dms = NULL;
778

779 780 781
	// Build and check the directory name including the relative path
	relative_dms_path = build_directory_name(dms_path);
	if (relative_dms_path == NULL)
782 783
		return NULL;

784 785 786
	// Get and store the absolute path to the DMS directory
	absolute_dms_path = realpath(relative_dms_path, NULL);
	if (absolute_dms_path == NULL)
787
	{
788 789 790 791
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError getting the absolute path to the DMS directory (DMS does not exist)");
		free(relative_dms_path);
		return NULL;
792 793
	}

794 795 796 797 798
	free(relative_dms_path);

	// Check if the DMS is already opened
	dms = check_if_dms_in_list(absolute_dms_path);
	if (dms != NULL)
799
	{
800 801 802
		list_dms(dms);
		free(absolute_dms_path);
		return dms;
803 804
	}

805 806 807
	// Allocate the data structure
	dms = (OBIDMS_p) malloc(sizeof(OBIDMS_t));
	if (dms == NULL)
808
	{
809 810
		obi_set_errno(OBIDMS_MEMORY_ERROR);
		obidebug(1, "\nError allocating the memory for the OBIDMS structure");
811
		return NULL;
812
	}
813

814 815
	strcpy(dms->dms_name, basename((char*)dms_path));
	strcpy(dms->directory_path, absolute_dms_path);
816

817
	// Try to open the directory
818
	dms->directory = opendir(dms->directory_path);
819
	if (dms->directory == NULL)
820 821 822 823 824 825
	{
		switch (errno)
		{
			case ENOENT:
				obi_set_errno(OBIDMS_NOT_EXIST_ERROR);
				break;
826

827 828 829
			case EACCES:
				obi_set_errno(OBIDMS_ACCESS_ERROR);
				break;
830

831 832 833
			case ENOMEM:
				obi_set_errno(OBIDMS_MEMORY_ERROR);
				break;
834

835 836
			default:
				obi_set_errno(OBIDMS_UNKNOWN_ERROR);
837
		}
838
		obidebug(1, "\nCan't open OBIDMS directory");
839
		free(dms);
840 841 842
		return NULL;
	}

843
	// Get and store file descriptor of DMS directory
844 845
	dms->dir_fd = dirfd(dms->directory);
	if (dms->dir_fd < 0)
846 847 848
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError getting the file descriptor for a newly created OBIDMS directory");
849 850
		closedir(dms->directory);
		free(dms);
851 852 853
		return NULL;
	}

854 855 856
	// Open the information file
	dms->infos = map_infos_file(dms->dir_fd, dms->dms_name);
	if (dms->infos == NULL)
857
	{
858
		obidebug(1, "\nError opening a DMS information file");
859 860 861 862 863
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

Celine Mercier's avatar
Celine Mercier committed
864 865 866
	// Open the indexer directory
	dms->indexer_directory = opendir_in_dms(dms, INDEXER_DIR_NAME);
	if (dms->indexer_directory == NULL)
867 868
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
Celine Mercier's avatar
Celine Mercier committed
869
		obidebug(1, "\nError opening the indexer directory");
870
		unmap_infos_file(dms);
871 872
		closedir(dms->directory);
		free(dms);
873 874 875
		return NULL;
	}

Celine Mercier's avatar
Celine Mercier committed
876 877 878
	// Store the indexer directory's file descriptor
	dms->indexer_dir_fd = dirfd(dms->indexer_directory);
	if (dms->indexer_dir_fd < 0)
879 880
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
Celine Mercier's avatar
Celine Mercier committed
881
		obidebug(1, "\nError getting the file descriptor of the indexer directory");
882
		unmap_infos_file(dms);
Celine Mercier's avatar
Celine Mercier committed
883
		closedir(dms->indexer_directory);
884
		closedir(dms->directory);
885 886 887
		free(dms);
		return NULL;
	}
888

889 890 891 892 893 894
	// Open the view directory
	dms->view_directory = opendir_in_dms(dms, VIEW_DIR_NAME);
	if (dms->view_directory == NULL)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError opening the view directory");
895
		unmap_infos_file(dms);
896 897 898 899 900 901 902 903 904 905 906 907
		closedir(dms->indexer_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

	// Store the view directory's file descriptor
	dms->view_dir_fd = dirfd(dms->view_directory);
	if (dms->view_dir_fd < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError getting the file descriptor of the view directory");
908
		unmap_infos_file(dms);
909 910 911 912 913 914
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

915 916 917 918 919 920
	// Open the taxonomy directory
	dms->tax_directory = opendir_in_dms(dms, TAXONOMY_DIR_NAME);
	if (dms->tax_directory == NULL)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError opening the taxonomy directory");
921
		unmap_infos_file(dms);
922 923 924 925 926 927 928 929 930 931 932 933 934
		closedir(dms->indexer_directory);
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

	// Store the taxonomy directory's file descriptor
	dms->tax_dir_fd = dirfd(dms->tax_directory);
	if (dms->tax_dir_fd < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError getting the file descriptor of the taxonomy directory");
935
		unmap_infos_file(dms);
936
		closedir(dms->indexer_directory);
937 938 939 940 941 942 943 944 945 946 947
		closedir(dms->tax_directory);
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

	// Clean unfinished views
	if (obi_clean_unfinished_views(dms) < 0)
	{
		obidebug(1, "\nError cleaning unfinished views when opening an OBIDMS");
948
		unmap_infos_file(dms);
949 950 951 952 953 954 955 956 957 958 959 960
		closedir(dms->indexer_directory);
		closedir(dms->tax_directory);
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

	// Clean unfinished columns
	if (obi_clean_unfinished_columns(dms) < 0)
	{
		obidebug(1, "\nError cleaning unfinished columns when opening an OBIDMS");
961
		unmap_infos_file(dms);
962
		closedir(dms->indexer_directory);
963 964 965 966 967 968 969
		closedir(dms->tax_directory);
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

970 971 972 973
	// Initialize the list of opened columns
	dms->opened_columns = (Opened_columns_list_p) malloc(sizeof(Opened_columns_list_t));
	(dms->opened_columns)->nb_opened_columns = 0;

974
	// Initialize the list of opened indexers
Celine Mercier's avatar
Celine Mercier committed
975 976
	dms->opened_indexers = (Opened_indexers_list_p) malloc(sizeof(Opened_indexers_list_t));
	(dms->opened_indexers)->nb_opened_indexers = 0;
977

978
	// Add in the global list of opened DMS
979 980 981
	if (list_dms(dms) < 0)
	{
		obidebug(1, "\nError cleaning unfinished columns when opening an OBIDMS");
982
		unmap_infos_file(dms);
983 984 985 986 987 988 989 990 991
		closedir(dms->indexer_directory);
		closedir(dms->tax_directory);
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms->opened_columns);
		free(dms->opened_indexers);
		free(dms);
		return NULL;
	}
992

993 994 995
	return dms;
}

996

997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
OBIDMS_p obi_test_open_dms(const char* dms_name)
{
	int exists;

	exists = obi_dms_exists(dms_name);

	switch (exists)
	{
		case 0:
			return NULL;
		case 1:
			return obi_open_dms(dms_name);
	};

	obidebug(1, "\nError checking if an OBIDMS directory exists");
	return NULL;
}


1016 1017
OBIDMS_p obi_dms(const char* dms_name)
{
1018
	int exists;
1019

1020 1021 1022 1023
	exists = obi_dms_exists(dms_name);

	switch (exists)
	{
1024
		case 0:
1025
			return obi_create_dms(dms_name);
1026
		case 1:
1027
			return obi_open_dms(dms_name);
1028 1029
	};

1030
	obidebug(1, "\nError checking if an OBIDMS directory exists");
1031 1032 1033
	return NULL;
}

1034

1035
int obi_close_dms(OBIDMS_p dms, bool force)
1036
{
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	int dms_counter;

	if (!force)
	{
		dms_counter = dms_count_in_list(dms);
		if (dms_counter < 0)
			obidebug(1, "\nError checking the counter of an OBIDMS in the global list of opened OBIDMS");

		if (dms_counter > 1)	// Don't close if the DMS is opened more than once
		{
			if (unlist_dms(dms, force) < 0)
			{
				obidebug(1, "\nError decrementing the counter of an OBIDMS in the global list of opened OBIDMS");
				return -1;
			}
			return 0;
		}
	}

1056 1057
	if (dms != NULL)
	{
1058 1059 1060 1061 1062 1063 1064 1065
		// Unmap information file
		if (unmap_infos_file(dms) < 0)
		{
			obidebug(1, "\nError unmaping a DMS information file while closing a DMS");
			free(dms);
			return -1;
		}

1066 1067 1068 1069
		// Close all columns
		while ((dms->opened_columns)->nb_opened_columns > 0)
			obi_close_column(*((dms->opened_columns)->columns));

1070
		// Close dms, and view, indexer and taxonomy directories
1071
		if (closedir(dms->indexer_directory) < 0)
1072
		{
Celine Mercier's avatar
Celine Mercier committed
1073 1074
			obi_set_errno(OBI_INDEXER_ERROR);
			obidebug(1, "\nError closing an indexer directory");
1075 1076 1077
			free(dms);
			return -1;
		}
1078 1079 1080 1081 1082 1083 1084
		if (closedir(dms->view_directory) < 0)
		{
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError closing a view directory");
			free(dms);
			return -1;
		}
1085 1086 1087 1088 1089 1090 1091
		if (closedir(dms->tax_directory) < 0)
		{
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError closing a taxonomy directory");
			free(dms);
			return -1;
		}
1092 1093 1094 1095 1096 1097 1098
		if (closedir(dms->directory) < 0)
		{
			obi_set_errno(OBIDMS_MEMORY_ERROR);
			obidebug(1, "\nError closing an OBIDMS directory");
			free(dms);
			return -1;
		}
1099 1100

		// Remove DMS from global list of opened DMS
1101 1102 1103 1104 1105 1106
		if (unlist_dms(dms, force) < 0)
		{
			obidebug(1, "\nError removing an OBIDMS from the global list of opened OBIDMS when closing it");
			free(dms);
			return -1;
		}
1107

1108 1109 1110 1111 1112 1113
		free(dms);
	}

	return 0;
}

1114

1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
int obi_dms_write_comments(OBIDMS_p dms, const char* comments)
{
	size_t new_size;

	if (comments == NULL)
		return 0;	// TODO or error? discuss

	new_size = sizeof(OBIDMS_infos_t) + strlen(comments) + 1;

	// Check if the file has to be enlarged
	if (new_size >= (dms->infos)->file_size)
	{
		if (enlarge_infos_file(dms, new_size) < 0)
			return -1;
	}

	strcpy((dms->infos)->comments, comments);

	(dms->infos)->used_size = new_size;

	return 0;
}


int obi_dms_add_comment(OBIDMS_p dms, const char* key, const char* value)
{
	char* new_comments = NULL;

	new_comments = obi_add_comment((dms->infos)->comments, key, value);
	if (new_comments == NULL)
	{
		obidebug(1, "\nError adding a comment to a dms, key: %s, value: %s", key, value);
		return -1;
	}

	if (obi_dms_write_comments(dms, new_comments) < 0)
	{
		obidebug(1, "\nError adding a comment to a dms, key: %s, value: %s", key, value);
		return -1;
	}

	free(new_comments);

	return 0;
}


1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
OBIDMS_column_p obi_dms_get_column_from_list(OBIDMS_p dms, const char* column_name, obiversion_t version)
{
	int i;

	for (i=0; i < ((dms->opened_columns)->nb_opened_columns); i++)
	{
		if (!strcmp(((*(((dms->opened_columns)->columns)+i))->header)->name, column_name)
				&& (((*(((dms->opened_columns)->columns)+i))->header)->version == version))
		{	// Found the column already opened, return it
			return *(((dms->opened_columns)->columns)+i);
		}
	}
	// Didn't find the column
	return NULL;
}


1179
void obi_dms_list_column(OBIDMS_p dms, OBIDMS_column_p column)	// TODO add check if column already in list?
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
{
	*(((dms->opened_columns)->columns)+((dms->opened_columns)->nb_opened_columns)) = column;
	((dms->opened_columns)->nb_opened_columns)++;
}


int obi_dms_unlist_column(OBIDMS_p dms, OBIDMS_column_p column)
{
	int i;
	Opened_columns_list_p columns_list;

	columns_list = dms->opened_columns;

	for (i=0; i < columns_list->nb_opened_columns; i++)
	{
		if (!strcmp(((*((columns_list->columns)+i))->header)->name, (column->header)->name)
				&& (((*((columns_list->columns)+i))->header)->version == (column->header)->version))
		{	// Found the column. Rearrange list
			(columns_list->nb_opened_columns)--;
			(columns_list->columns)[i] = (columns_list->columns)[columns_list->nb_opened_columns];
			return 0;
		}
	}

	obidebug(1, "\nCould not find the column to delete from list of open columns");
	return -1;
}


Obi_indexer_p obi_dms_get_indexer_from_list(OBIDMS_p dms, const char* indexer_name)
{
	int i;
	Opened_indexers_list_p indexers_list;

	indexers_list = dms->opened_indexers;

	for (i=0; i < (indexers_list->nb_opened_indexers); i++)
	{
1218
		if (!strcmp(obi_indexer_get_name((indexers_list->indexers)[i]), indexer_name))
1219 1220 1221 1222 1223 1224 1225 1226 1227
		{	// Found the indexer already opened, return it
			return (indexers_list->indexers)[i];
		}
	}
	// Didn't find the indexer
	return NULL;
}


1228
void obi_dms_list_indexer(OBIDMS_p dms, Obi_indexer_p indexer)  	// TODO add check if indexer already in list?
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
{
	*(((dms->opened_indexers)->indexers)+((dms->opened_indexers)->nb_opened_indexers)) = indexer;
	((dms->opened_indexers)->nb_opened_indexers)++;
}


int obi_dms_unlist_indexer(OBIDMS_p dms, Obi_indexer_p indexer)
{
	int i;
	Opened_indexers_list_p indexers_list;

	indexers_list = dms->opened_indexers;

	for (i=0; i < indexers_list->nb_opened_indexers; i++)
	{
1244
		if (!strcmp(obi_indexer_get_name((indexers_list->indexers)[i]), indexer->name))
1245 1246 1247 1248 1249 1250 1251 1252 1253