obidms.c 40.9 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 25

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

35

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

38

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

43 44


45
/**************************************************************************
46
 *
47
 * 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
48
 *
49
 **************************************************************************/
50 51


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


71 72 73 74 75 76 77 78 79
/**
 * 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.
 *
80
 * @returns A pointer on the file name.
81 82 83 84 85 86 87 88
 * @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);


89 90 91 92 93 94 95 96
/**
 * 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)
 */
97
static size_t get_platform_infos_file_size(void);
98 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


/**
 * @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);


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


166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/**
 * 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.
184 185 186 187
 * @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.
188 189 190 191
 *
 * @since October 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
192 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
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);

222 223


224
/************************************************************************
225
 *
226
 * 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
227
 *
228
 ************************************************************************/
229

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

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

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

252
	return directory_name;
253 254
}

255

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

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

	return file_name;
}


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

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	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;
295 296

	// Create file name
297 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
	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
513 514 515 516 517
	file_name = build_infos_file_name(dms_name);
	if (file_name == NULL)
		return -1;

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

	free(file_name);

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

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

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	// 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)
560
	{
561 562
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError munmapping a DMS information file");
563 564 565
		return -1;
	}

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

573 574 575
	return 0;
}

576

577 578 579
static int list_dms(OBIDMS_p dms)
{
	int i = 0;
580
	while ((global_opened_dms_list[i] != NULL) && (global_opened_dms_list[i] != dms))
581 582 583 584 585 586 587
		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;
	}
588 589 590 591 592 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
	if (global_opened_dms_list[i] == NULL)
	{    // new dms
		global_opened_dms_list[i] = dms;
		global_opened_dms_counter_list[i] = 0;
	}
	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++;
		}
	}
620 621 622 623
	return 0;
}


624
static OBIDMS_p check_if_dms_in_list(const char* full_dms_path)
625 626
{
	int i = 0;
627
	while ((i <= MAX_NB_OPENED_DMS) && (global_opened_dms_list[i] != NULL) && (strcmp(global_opened_dms_list[i]->directory_path, full_dms_path) != 0))
628
		i++;
629 630 631 632 633 634 635 636 637 638 639 640
	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))
641
		i++;
642 643 644
	if (i == MAX_NB_OPENED_DMS)
		return -1; // DMS not found
	return global_opened_dms_counter_list[i];
645 646 647
}


648
/**********************************************************************
649
 *
650
 * 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
651
 *
652
 **********************************************************************/
653

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

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

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

667
	free(directory_name);
668

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


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

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

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

703
	// Get file descriptor of DMS directory to create other directories
704 705 706 707 708 709 710 711
	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;
	}
712 713 714

	free(directory_name);

715 716 717 718 719 720 721 722
	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 committed
723 724
	// Create the indexer directory
	if (mkdirat(dms_file_descriptor, INDEXER_DIR_NAME, 00777) < 0)
725
	{
Celine Mercier committed
726
		obi_set_errno(OBI_INDEXER_ERROR);
727
		obidebug(1, "\nProblem creating the indexer directory");
728 729 730
		return NULL;
	}

731 732 733 734
	// Create the view directory
	if (mkdirat(dms_file_descriptor, VIEW_DIR_NAME, 00777) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
735 736 737 738 739 740 741 742 743
		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");
744 745 746
		return NULL;
	}

747 748 749 750 751 752 753
	// 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)
754
	{
755 756
		obidebug(1, "\nProblem opening a DMS");
		return NULL;
757 758
	}

759 760 761 762
	// 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");
763
		return NULL;
764
	}
765

766
	return dms;
767 768
}

769

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

	dms = NULL;
777

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

783 784 785
	// Get and store the absolute path to the DMS directory
	absolute_dms_path = realpath(relative_dms_path, NULL);
	if (absolute_dms_path == NULL)
786
	{
787 788 789 790
		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;
791 792
	}

793 794 795 796 797
	free(relative_dms_path);

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

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

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

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

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

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

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

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

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

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

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

888 889 890 891 892 893
	// 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");
894
		unmap_infos_file(dms);
895 896 897 898 899 900 901 902 903 904 905 906
		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");
907
		unmap_infos_file(dms);
908 909 910 911 912 913
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

914 915 916 917 918 919
	// 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");
920
		unmap_infos_file(dms);
921 922 923 924 925 926 927 928 929 930 931 932 933
		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");
934
		unmap_infos_file(dms);
935
		closedir(dms->indexer_directory);
936 937 938 939 940 941 942 943 944 945 946
		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");
947
		unmap_infos_file(dms);
948 949 950 951 952 953 954 955 956 957 958 959
		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");
960
		unmap_infos_file(dms);
961
		closedir(dms->indexer_directory);
962 963 964 965 966 967 968
		closedir(dms->tax_directory);
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

969 970 971 972
	// 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;

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

977
	// Add in the global list of opened DMS
978 979 980
	if (list_dms(dms) < 0)
	{
		obidebug(1, "\nError cleaning unfinished columns when opening an OBIDMS");
981
		unmap_infos_file(dms);
982 983 984 985 986 987 988 989 990
		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;
	}
991

992 993 994
	return dms;
}

995

996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
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;
}


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

1019 1020 1021 1022
	exists = obi_dms_exists(dms_name);

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

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

1033

1034
int obi_close_dms(OBIDMS_p dms, bool force)
1035
{
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	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;
		}
	}

1055 1056
	if (dms != NULL)
	{
1057 1058 1059 1060 1061 1062 1063 1064
		// 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;
		}

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

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

		// Remove DMS from global list of opened DMS
1100 1101 1102 1103 1104 1105
		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;
		}
1106

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

	return 0;
}

1113

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
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;
}


1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
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;
}


1178
void obi_dms_list_column(OBIDMS_p dms, OBIDMS_column_p column)	// TODO add check if column already in list?
1179 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
{
	*(((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++)
	{
1217
		if (!strcmp(obi_indexer_get_name((indexers_list->indexers)[i]), indexer_name))
1218 1219 1220 1221 1222 1223 1224 1225 1226
		{	// Found the indexer already opened, return it
			return (indexers_list->indexers)[i];
		}
	}
	// Didn't find the indexer
	return NULL;
}


1227
void obi_dms_list_indexer(OBIDMS_p dms, Obi_indexer_p indexer)  	// TODO add check if indexer already in list?
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
{
	*(((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++)
	{
1243
		if (!strcmp(obi_indexer_get_name((indexers_list->indexers)[i]), indexer->name))
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
		{	// Found the indexer. Rearrange list
			(indexers_list->nb_opened_indexers)--;
			(indexers_list->indexers)[i] = (indexers_list->indexers)[indexers_list->nb_opened_indexers];
			return 0;
		}
	}

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


1256
char* obi_dms_get_dms_path(OBIDMS_p dms)
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
{
	char* full_path;

	full_path = (char*) malloc((MAX_PATH_LEN)*sizeof(char));
	if (full_path == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for the char* path to a file or directory");
		return NULL;
	}

1268
	strcpy(full_path, dms->directory_path);
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278

	return full_path;
}


char* obi_dms_get_full_path(OBIDMS_p dms, const char* path_name)
{
	char* full_path;

	full_path = obi_dms_get_dms_path(dms);
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
	strcat(full_path, "/");
	strcat(full_path, path_name);

	return full_path;
}


DIR* opendir_in_dms(OBIDMS_p dms, const char* path_name)
{
	char* full_path;
	DIR* directory;

1291
	full_path = obi_dms_get_full_path(dms, path_name);
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
	if (full_path == NULL)
		return NULL;

	directory = opendir(full_path);
	if (directory == NULL)
	{
		obi_set_errno(OBI_UTILS_ERROR);
		obidebug(1, "\nError opening a directory");
	}

	free(full_path);

	return directory;
}
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372


// TODO move somewhere else maybe
// TODO discuss arguments
obiversion_t obi_import_column(const char* dms_path_1, const char* dms_path_2, const char* column_name, obiversion_t version_number)
{
	OBIDMS_p				dms_1;
	OBIDMS_p 				dms_2;
	OBIDMS_column_p 		column_1;
	OBIDMS_column_p 	   	column_2;
	OBIDMS_column_header_p 	header_1;
	OBIDMS_column_header_p 	header_2;
	int 					avl_exists;
	const char* 			avl_name;
	char* 					new_avl_name;
	obiversion_t 			new_version;
	int 					i;
	int 					avl_count;
	char*		 			avl_name_1;
	char* 					avl_name_2;
	char* 					avl_file_path_1;
	char* 					avl_file_path_2;
	char* 					avl_data_file_path_1;
	char* 					avl_data_file_path_2;
	char* 					complete_avl_name;
	Obi_indexer_p 			avl_group;

	dms_1 = obi_open_dms(dms_path_1);
	if (dms_1 == NULL)
	{
		obidebug(1, "\nError opening a DMS to import a column from it");
		return -1;
	}

	dms_2 = obi_dms(dms_path_2);
	if (dms_2 == NULL)
	{
		obidebug(1, "\nError opening or creating a DMS to import a column into it");
		return -1;
	}

	column_1 = obi_open_column(dms_1, column_name, version_number);
	if (column_1 == NULL)
	{
		obidebug(1, "\nError opening a column to import in another DMS");
		return -1;
	}

	header_1 = column_1->header;

	// Check if associated indexer exists BEFORE creating the new column as that will automatically create it if it doesn't already exist
	avl_name = header_1->indexer_name;
	avl_exists = obi_indexer_exists(dms_2, avl_name);
	if (avl_exists == -1)
	{
		obidebug(1, "\nError checking if an indexer exists while importing a column");
		return -1;
	}
	if (avl_exists)
		// Use automatic name
		new_avl_name = NULL;
	else
		// Name can stay the same
		new_avl_name = header_1->indexer_name;

	// Create new column
	column_2 = obi_create_column(dms_2, column_name, header_1->returned_data_type, header_1->line_count,
1373
								 header_1->nb_elements_per_line, header_1->elements_names, true, header_1->tuples,
1374 1375
								 header_1->to_eval, new_avl_name, (header_1->associated_column).column_name,
								 (header_1->associated_column).version, header_1->comments);
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508

	if (column_2 == NULL)
	{
		obidebug(1, "\nError creating the new column while importing a column");
		return -1;
	}

	header_2 = column_2->header;

	// Get the new version to return
	new_version = header_2->version;

	// Copy lines_used informations
	header_2->lines_used = header_1->lines_used;

	// Copy data TODO check how much time and memory that costs, eventually use write() instead
	memcpy(column_2->data, column_1->data, header_1->data_size);

	// Copy the AVL files if there are some (overwriting the automatically created files)
	if ((header_1->returned_data_type == OBI_STR) || (header_1->returned_data_type == OBI_SEQ) || (header_1->returned_data_type == OBI_QUAL))
	{
		avl_name_1 = (char*) malloc((strlen(header_1->indexer_name) + 1)  * sizeof(char));
		if (avl_name_1 == NULL)
		{
			obi_set_errno(OBI_MALLOC_ERROR);
			obidebug(1, "\nError allocating memory for an AVL name when importing a column");
			return -1;
		}
		strcpy(avl_name_1, header_1->indexer_name);

		avl_name_2 = (char*) malloc((strlen(header_2->indexer_name) + 1)  * sizeof(char));
		if (avl_name_2 == NULL)
		{
			obi_set_errno(OBI_MALLOC_ERROR);
			obidebug(1, "\nError allocating memory for an AVL name when importing a column");
			return -1;
		}
		strcpy(avl_name_2, header_2->indexer_name);

		avl_count = (column_1->indexer->last_avl_idx) + 1;

		// Close column to manipulate AVL files safely (but not multithreading safe) (TODO not sure how important this is, can't find informations about conflicts when using write() on mmapped files)
		if (obi_close_column(column_1) < 0)
		{
			obidebug(1, "\nError closing an imported column");
			return -1;
		}
		if (obi_close_column(column_2) < 0)
		{
			obidebug(1, "\nError closing an imported column");
			return -1;
		}

		for (i=0; i < avl_count; i++)
		{
			avl_file_path_1 = obi_get_full_path_of_avl_file_name(dms_1, avl_name_1, i);
			if (avl_file_path_1 == NULL)
			{
				obidebug(1, "\nError getting an AVL file path while importing a column");
				return -1;
			}

			avl_data_file_path_1 = obi_get_full_path_of_avl_data_file_name(dms_1, avl_name_1, i);
			if (avl_data_file_path_1 == NULL)
			{
				obidebug(1, "\nError getting an AVL file path while importing a column");
				return -1;
			}
			avl_file_path_2 = obi_get_full_path_of_avl_file_name(dms_2, avl_name_2, i);
			if (avl_file_path_2 == NULL)
			{
				obidebug(1, "\nError getting an AVL file path while importing a column");
				return -1;
			}
			avl_data_file_path_2 = obi_get_full_path_of_avl_data_file_name(dms_2, avl_name_2, i);
			if (avl_data_file_path_2 == NULL)
			{
				obidebug(1, "\nError getting an AVL file path while importing a column");
				return -1;
			}

			// Copy AVL file
			if (copy_file(avl_file_path_1, avl_file_path_2) < 0)
			{
				obidebug(1, "\nError copying an AVL file while importing a column");
				return -1;
			}

			// Copy AVL data file
			if (copy_file(avl_data_file_path_1, avl_data_file_path_2) < 0)
			{
				obidebug(1, "\nError copying a data AVL file while importing a column");
				return -1;
			}

			free(avl_file_path_1);
			free(avl_file_path_2);
			free(avl_data_file_path_1);
			free(avl_data_file_path_2);
		}

		// Update AVL names in headers
		avl_group = obi_open_indexer(dms_2, avl_name_2);
		for (i=0; i < avl_count; i++)
		{
			complete_avl_name = obi_build_avl_name_with_idx(avl_name_2, i);
			strcpy((((avl_group->sub_avls)[i])->header)->avl_name, complete_avl_name);
			strcpy(((((avl_group->sub_avls)[i])->data)->header)->avl_name, complete_avl_name);
			free(complete_avl_name);
		}

		free(avl_name_1);
		free(avl_name_2);
	}
	else
	{
		if (obi_close_column(column_1) < 0)
		{
			obidebug(1, "\nError closing an imported column");
			return -1;
		}
		if (obi_close_column(column_2) < 0)
		{
			obidebug(1, "\nError closing an imported column");
			return -1;
		}
	}

	// Copy associated column (update version)
	//new_associated_col_version = import_column(dms_path_1, dms_path_2, header_1->associated_column_name, header_1->associated_column_version);
	// TODO no? because if iterating over all columns in a view etc.... iterate and change associated columns version refs afterwards?

	// Close the DMS
1509 1510
	obi_close_dms(dms_1, false);
	obi_close_dms(dms_2, false);
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590

	return new_version;
}


// TODO move somewhere else maybe
// TODO discuss arguments
int obi_import_view(const char* dms_path_1, const char* dms_path_2, const char* view_name_1, const char* view_name_2)
{
	OBIDMS_p 		dms_1;
	OBIDMS_p	 	dms_2;
	Obiview_p 		view_1;
	Obiview_p 		view_2;
	obiversion_t 	new_version;
	int 			i;

	dms_1 = obi_open_dms(dms_path_1);
	if (dms_1 == NULL)
	{
		obidebug(1, "\nError opening a DMS to import a view from it");
		return -1;
	}

	dms_2 = obi_dms(dms_path_2);
	if (dms_2 == NULL)
	{
		obidebug(1, "\nError opening or creating a DMS to import a view into it");
		return -1;
	}

	// Open view to import
	view_1 = obi_open_view(dms_1, view_name_1);

	// Create new view
	if (strcmp((view_1->infos)->view_type, VIEW_TYPE_NUC_SEQS) == 0)
		view_2 = obi_new_view_nuc_seqs(dms_2, view_name_2, NULL, NULL, (view_1->infos)->comments, false);
	else	// Non-typed view
		view_2 = obi_new_view(dms_2, view_name_2, NULL, NULL, (view_1->infos)->comments);

	// Import line count
	view_2->infos->line_count = view_1->infos->line_count;

	// Import the line selection column if there is one
	if (! view_1->infos->all_lines)
	{
		view_2->infos->all_lines = false;
		new_version = obi_import_column(dms_path_1, dms_path_2, ((view_1->infos)->line_selection).column_name, ((view_1->infos)->line_selection).version);
		if (new_version == -1)
		{
			obidebug(1, "\nError importing a line selection column while importing a view");
			return -1;
		}
		strcpy(((view_2->infos)->line_selection).column_name, ((view_1->infos)->line_selection).column_name);
		((view_2->infos)->line_selection).version = new_version;
		view_2->line_selection = obi_open_column(dms_2, ((view_2->infos)->line_selection).column_name, ((view_2->infos)->line_selection).version);
		if (view_2->line_selection == NULL)
		{
			obidebug(1, "\nError opening a line selection column while importing a view");
			return -1;
		}
	}

	// Import each column and update with the new version number
	for (i=0; i < (view_1->infos->column_count); i++)
	{
		new_version = obi_import_column(dms_path_1, dms_path_2, ((((view_1->infos)->column_references)[i]).column_refs).column_name, ((((view_1->infos)->column_references)[i]).column_refs).version);
		if (new_version == -1)
		{
			obidebug(1, "\nError importing a column while importing a view");
			return -1;
		}

		if (obi_view_add_column(view_2,
							    ((((view_1->infos)->column_references)[i]).column_refs).column_name,
							    new_version,
							    (((view_1->infos)->column_references)[i]).alias,
							    0,
								0,
								0,
								NULL,
1591
								false,
1592 1593
								false,
								false,
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
								NULL,
								NULL,
								-1,
								NULL,
								false) < 0)
			{
				obidebug(1, "\nError adding a column to a view while importing it");
				return -1;
			}
	}

	// Close the views
	if (obi_save_and_close_view(view_1) < 0)
	{
		obidebug(1, "\nError closing a view after importing from it");
		return -1;
	}
	if (obi_save_and_close_view(view_2) < 0)
	{
		obidebug(1, "\nError closing a view after importing it");
		return -1;
	}

	// Close the DMS
1618 1619
	obi_close_dms(dms_1, false);
	obi_close_dms(dms_2, false);
1620 1621 1622 1623 1624

	return 0;
}


1625 1626 1627
void obi_close_atexit()
{
	int i=0;
1628

1629 1630
	while (global_opened_dms_list[i] != NULL)
	{
1631
        obi_close_dms(global_opened_dms_list[i], true);
1632 1633 1634
		i++;
	}
}
1635