Functions to truncate and/or close a column

This commit is contained in:
Celine Mercier
2015-08-26 17:01:54 +02:00
parent fdc3b96beb
commit 48d10ea17c
9 changed files with 193 additions and 9 deletions

View File

@ -948,15 +948,130 @@ OBIDMS_column_p obi_clone_column(OBIDMS_p dms, const char* column_name, obiversi
(new_column->header)->lines_used = nb_lines;
}
// close column_to_clone TODO
return new_column;
}
int obi_close_column(OBIDMS_column_p column)
{
//munmap? TODO
//truncate to lines used TODO
size_t data_size;
// Munmap data
data_size = (column->header)->line_count * (column->header)->nb_elements_per_line * sizeof((column->header)->data_type);
if (munmap(column->data, data_size) < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError munmapping column data");
return -1;
}
// Munmap header
if (munmap(column->header, (column->header)->header_size) < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError munmapping a column header");
return -1;
}
free(column);
return 0;
}
int obi_truncate_column_to_lines_used(OBIDMS_column_p column)
{
size_t file_size;
size_t data_size;
int column_dir_file_descriptor;
int column_file_descriptor;
char* column_file_name;
// Get the file descriptor associated to the column directory
column_dir_file_descriptor = dirfd((column->column_directory)->directory);
if (column_dir_file_descriptor < 0)
{
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
obidebug(1, "\nError getting the file descriptor for a column directory");
return -1;
}
// Get the column file name
column_file_name = build_column_file_name((column->header)->name, (column->header)->version);
if (column_file_name == NULL)
{
return -1;
}
// Open the column file
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDWR | O_CREAT);
if (column_file_descriptor < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(column_file_name);
return -1;
}
// Unmap the data before truncating the file
data_size = (column->header)->line_count * (column->header)->nb_elements_per_line * sizeof((column->header)->data_type);
if (munmap(column->data, data_size) < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError munmapping the data of a column before truncating");
free(column_file_name);
close(column_file_descriptor);
return -1;
}
// Truncate the column file at the number of lines used
data_size = (column->header)->lines_used * (column->header)->nb_elements_per_line * sizeof((column->header)->data_type);
file_size = (column->header)->header_size + data_size;
if (ftruncate(column_file_descriptor, file_size) < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError truncating a column file at the number of lines used");
free(column_file_name);
close(column_file_descriptor);
return -1;
}
// Remap the data
column->data = mmap(NULL,
data_size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
column_file_descriptor,
(column->header)->header_size
);
if (column->data == MAP_FAILED)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError re-mmapping the data of a column after truncating");
free(column_file_name);
close(column_file_descriptor);
return -1;
}
// Set line_count to lines_used
(column->header)->line_count = (column->header)->lines_used;
free(column_file_name);
close(column_file_descriptor);
return 0;
}
int obi_truncate_and_close_column(OBIDMS_column_p column)
{
if (obi_truncate_column_to_lines_used(column) < 0)
return -1;
if (obi_close_column(column) < 0)
return -1;
return 0;
}