obidmscolumndir.c 5.31 KB
Newer Older
1
/****************************************************************************
2
 * OBIDMS column directories functions                                      *
3 4 5
 ****************************************************************************/

/**
6
 * @file obidmscolumndir.c
7
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
8
 * @date 18 June 2015
9
 * @brief Functions for OBIDMS column directories.
10 11 12 13 14 15
 */


#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
16
#include <sys/stat.h>
17 18 19 20
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>

21
#include "obidmscolumndir.h"
22
#include "obidms.h"
23
#include "utils.h"
24
#include "obierrno.h"
25 26 27
#include "obidebug.h"


28
#define DEBUG_LEVEL 0	// TODO has to be defined somewhere else (cython compil flag?)
29 30 31



32
/**********************************************************************
33
 *
34
 * 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
35
 *
36
 **********************************************************************/
37 38


39
char* obi_build_column_directory_name(const char* column_name)
40 41 42 43
{
	char* column_directory_name;

	// Build the database directory name
Celine Mercier committed
44 45
	column_directory_name = (char*) malloc((strlen(column_name) + 8)*sizeof(char));
	if (sprintf(column_directory_name, "%s.obicol", column_name) < 0)
46 47
	{
		obi_set_errno(OBICOLDIR_MEMORY_ERROR);
48
		obidebug(1, "\nError building a column directory name");
49 50 51 52
		return NULL;
	}

	// Test if the database name is not too long
53
	if (strlen(column_directory_name) >= OBIDMS_COLUMN_MAX_NAME)
54 55
	{
		obi_set_errno(OBICOLDIR_LONG_NAME_ERROR);
56
		obidebug(1, "\nError due to column name too long");
57 58 59 60 61 62 63 64
		free(column_directory_name);
		return NULL;
	}

	return column_directory_name;
}


65
int obi_column_directory_exists(OBIDMS_p dms, const char* column_name)
66 67 68 69 70 71 72
{
    struct 	stat buffer;
	char* 	column_directory_name;
	char* 	full_path;
    int 	check_dir;

	// Build and check the directory name
73
    column_directory_name = obi_build_column_directory_name(column_name);
74 75 76 77
	if (column_directory_name == NULL)
		return -1;

	// Get the full path for the column directory
78
	full_path = obi_dms_get_full_path(dms, column_directory_name);
79 80 81
	if (full_path == NULL)
	{
		obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
82
		obidebug(1, "\nError getting path for an OBIDMS directory");
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
		free(column_directory_name);
		return -1;
	}

	check_dir = stat(full_path, &buffer);

	free(column_directory_name);
	free(full_path);

    if(check_dir == 0)
        return 1;
    else
        return 0;
}


99
OBIDMS_column_directory_p obi_create_column_directory(OBIDMS_p dms, const char* column_name)
100 101 102 103
{
	char* 	column_directory_name;

	// Build and check the directory name
104
	column_directory_name = obi_build_column_directory_name(column_name);
105 106 107 108 109 110 111
	if (column_directory_name == NULL)
	{
		obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
		return NULL;
	}

	// Try to create the directory
112
	if (mkdirat(dms->dir_fd, column_directory_name, 00777) < 0)
113 114 115 116 117
	{
		if (errno == EEXIST)
			obi_set_errno(OBICOLDIR_EXIST_ERROR);
		else
			obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
118
		obidebug(1, "\nError creating a column directory");
119 120 121 122 123 124
		free(column_directory_name);
		return NULL;
	}

	free(column_directory_name);

125
	return obi_open_column_directory(dms, column_name);
126 127 128
}


129
OBIDMS_column_directory_p obi_open_column_directory(OBIDMS_p dms, const char* column_name)
130
{
131
	OBIDMS_column_directory_p	column_directory;
132 133
	char*	 					column_directory_name;
	DIR*						directory;
134

135
	column_directory = NULL;
136 137

	// Build and check the directory name
138
	column_directory_name = obi_build_column_directory_name(column_name);
139 140 141 142
	if (column_directory_name == NULL)
		return NULL;

	// Try to open the column directory
Celine Mercier committed
143
	directory = opendir_in_dms(dms, column_directory_name);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	if (directory == NULL) {
		switch (errno)
		{
			case ENOENT:
				obi_set_errno(OBICOLDIR_NOT_EXIST_ERROR);
				break;

			case EACCES:
				obi_set_errno(OBICOLDIR_ACCESS_ERROR);
				break;

			case ENOMEM:
				obi_set_errno(OBICOLDIR_MEMORY_ERROR);
				break;

			default:
				obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
		}
162
		obidebug(1, "\nError opening a column directory");
163 164 165 166 167
		free(column_directory_name);
		return NULL;
	}

	// Allocate the column dir structure
168 169
	column_directory = (OBIDMS_column_directory_p) malloc(sizeof(OBIDMS_column_directory_t));
	if (column_directory == NULL)
170 171
	{
		obi_set_errno(OBICOLDIR_MEMORY_ERROR);
172
		obidebug(1, "\nError allocating the memory for an OBIDMS column directory structure");
173 174 175 176 177
		free(column_directory_name);
		return NULL;
	}

	// Initialize the data structure
178
	column_directory->dms = dms;
179 180
	strcpy(column_directory->directory_name, column_directory_name);
	strcpy(column_directory->column_name, column_name);
181

182
	if (closedir(directory) < 0)
183 184
	{
		obi_set_errno(OBICOLDIR_MEMORY_ERROR);
185
		obidebug(1, "\nError closing a DIR after opening a column directory");
186
		free(column_directory_name);
187
		return NULL;
188 189
	}

190 191
	free(column_directory_name);

192
	return column_directory;
193 194 195
}


196
OBIDMS_column_directory_p obi_column_directory(OBIDMS_p dms, const char* column_name)
197 198
{
	int exists;
199
	exists = obi_column_directory_exists(dms, column_name);
200 201 202 203

	switch (exists)
	{
		case 0:
204
			return obi_create_column_directory(dms, column_name);
205
		case 1:
206
			return obi_open_column_directory(dms, column_name);
207 208
	};

209
	obidebug(1, "\nError checking if a column directory exists");
210 211 212 213
	return NULL;
}


214
int obi_close_column_directory(OBIDMS_column_directory_p column_directory)
215
{
216 217
	if (column_directory != NULL)
		free(column_directory);
218 219 220 221

	return 0;
}