/* * obidms.c * * @date 23 May 2015 * @Author: coissac */ #include /*********************************************************************** * * 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 * ***********************************************************************/ /** * Internal function building the directory name from a OBIDMS name. * * The function build the directory name corresponding to a OBIDMS. * It checks also that the name is not too long. * * @warning The returned pointer has to be freed by the caller. * * @param name the name of the OBIDMS * * @return a pointer to the directory name * @retvalue if everything is ok * @retvalue NULL if an error occurs * * ###Error values * - OBIDMS_MEMORY_ERROR : something wrong occurs during memory allocation. * - OBIDMS_LONG_NAME_ERROR : the database name exceeds the limit. * * @since May 2015 * @author Eric Coissac (eric.coissac@metabarcoding.org) */ static char *build_directory_name(const char *name) { char *dirdbname; // Build the database directory name if (asprintf(&dirdbname,"%s.obidms",name) < 0) { obi_set_errno(OBIDMS_MEMORY_ERROR); return NULL; } // Test if the database name is not too long if (strlen(dirdbname) >= OBIDMS_MAX_NAME) { obi_set_errno(OBIDMS_LONG_NAME_ERROR); free(dirdbname); return NULL; } return dirdbname; } /*********************************************************************** * * 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 * ***********************************************************************/ int obi_dms_exists(const char* name) { struct stat buffer; char *dirdbname; int exist; // Build and check the directory name dirdbname = build_directory_name(name); if (dirdbname==NULL) return -1; exist = stat(dirdbname,&buffer); free(dirdbname); if(exist == 0) return 1; else // -1 return 0; } OBIDMS_p obi_create_dms(const char *name) { char *dirdbname; // Build and check the directory name dirdbname = build_directory_name(name); if (dirdbname==NULL) return NULL; // Try to create the directory if (mkdir(dirdbname,0x777) < 0) { if (errno==EEXIST) obi_set_errno(OBIDMS_EXIST_ERROR); else obi_set_errno(OBIDMS_UNKNOWN_ERROR); free(dirdbname); return NULL; } free(dirdbname); return obi_open_dms(name); } OBIDMS_p obi_open_dms(const char *name) { OBIDMS_p dms=NULL; char *dirdbname; DIR *directory; // Build and check the directory name dirdbname = build_directory_name(name); if (dirdbname==NULL) return NULL; // try to open the directory directory = opendir(dirdbname); if (directory==NULL) { switch (errno) { case ENOENT: obi_set_errno(OBIDMS_NOT_EXIST_ERROR); break; case EACCES: obi_set_errno(OBIDMS_ACCESS_ERROR); break; case ENOMEM: obi_set_errno(OBIDMS_MEMORY_ERROR); break; default: obi_set_errno(OBIDMS_UNKNOWN_ERROR); } free(dirdbname); return NULL; } // Allocate the data structure dms = (OBIDMS_p) malloc(sizeof(OBIDMS_t)); if (dms==NULL) { obi_set_errno(OBIDMS_MEMORY_ERROR); free(dirdbname); return NULL; } // Initialise the data structure strcpy(dms->dirname,dirdbname); dms->directory=directory; free(dirdbname); return dms; } OBIDMS_p obi_dms(const char *name) { int exist = obi_dms_exists(name); switch (exist) { case 0: return obi_create_dms(name); case 1: return obi_open_dms(name); }; return NULL; } int obi_close_dms(OBIDMS_p dms) { if (dms != NULL) { if (closedir(dms->directory) < 0) { obi_set_errno(OBIDMS_MEMORY_ERROR); free(dms); return -1; } free(dms); } return 0; }