From f019dcad074e3f901f2bcffeba4b07db1f88ba8c Mon Sep 17 00:00:00 2001 From: Carsten Haitzler Date: Sat, 20 May 2006 02:44:24 +0000 Subject: [PATCH] return write errors on close. SVN revision: 22758 --- legacy/eet/src/lib/Eet.h | 51 +++++++++++++++++++------------- legacy/eet/src/lib/Eet_private.h | 1 + legacy/eet/src/lib/eet_lib.c | 49 +++++++++++++++++++++--------- 3 files changed, 67 insertions(+), 34 deletions(-) diff --git a/legacy/eet/src/lib/Eet.h b/legacy/eet/src/lib/Eet.h index da2ffa4606..452dcdcf72 100644 --- a/legacy/eet/src/lib/Eet.h +++ b/legacy/eet/src/lib/Eet.h @@ -61,15 +61,26 @@ extern "C" { /***************************************************************************/ - enum _Eet_File_Mode + typedef enum _Eet_File_Mode { EET_FILE_MODE_INVALID = -1, EET_FILE_MODE_READ, EET_FILE_MODE_WRITE, EET_FILE_MODE_READ_WRITE - }; + } Eet_File_Mode; - typedef enum _Eet_File_Mode Eet_File_Mode; + typedef enum _Eet_Error + { + EET_ERROR_NONE, + EET_ERROR_BAD_OBJECT, + EET_ERROR_EMPTY, + EET_ERROR_NOT_WRITABLE, + EET_ERROR_WRITE_ERROR, + EET_ERROR_WRITE_ERROR_FILE_TOO_BIG, + EET_ERROR_WRITE_ERROR_IO_ERROR, + EET_ERROR_WRITE_ERROR_OUT_OF_SPACE, + EET_ERROR_WRITE_ERROR_FILE_CLOSED + } Eet_Error; typedef struct _Eet_File Eet_File; typedef struct _Eet_Data_Descriptor Eet_Data_Descriptor; @@ -183,14 +194,14 @@ extern "C" { * } * @endcode */ - EAPI Eet_File *eet_open (const char *file, Eet_File_Mode mode); + EAPI Eet_File *eet_open(const char *file, Eet_File_Mode mode); /** * Get the mode an Eet_File was opened with. * @param ef A valid eet file handle. * @return The mode ef was opened with. */ - EAPI Eet_File_Mode eet_mode_get (Eet_File *ef); + EAPI Eet_File_Mode eet_mode_get(Eet_File *ef); /** * Close an eet file handle and flush and writes pending. @@ -202,7 +213,7 @@ extern "C" { * * If the eet file handle is not valid nothing will be done. */ - EAPI void eet_close (Eet_File *ef); + EAPI Eet_Error eet_close(Eet_File *ef); /** * Read a specified entry from an eet file and return data @@ -221,7 +232,7 @@ extern "C" { * If the eet file handle is not valid NULL is returned and size_ret is * filled with 0. */ - EAPI void *eet_read (Eet_File *ef, const char *name, int *size_ret); + EAPI void *eet_read(Eet_File *ef, const char *name, int *size_ret); /** * Write a specified entry to an eet file handle @@ -246,7 +257,7 @@ extern "C" { * a flush to disk (it will stay in ram till the eet file handle is * closed though). */ - EAPI int eet_write (Eet_File *ef, const char *name, const void *data, int size, int compress); + EAPI int eet_write(Eet_File *ef, const char *name, const void *data, int size, int compress); /** * Delete a specified entry from an Eet file being written or re-written @@ -263,7 +274,7 @@ extern "C" { * * Name, must not be NULL, otherwise 0 will be returned. */ - EAPI int eet_delete(Eet_File *ef, const char *name); + EAPI int eet_delete(Eet_File *ef, const char *name); /** * List all entries in eet file matching shell glob. @@ -292,7 +303,7 @@ extern "C" { * Hint: an easy way to list all entries in an eet file is to use a glob * value of "*". */ - EAPI char **eet_list (Eet_File *ef, const char *glob, int *count_ret); + EAPI char **eet_list(Eet_File *ef, const char *glob, int *count_ret); /** * Return the number of entries in the specified eet file. @@ -300,7 +311,7 @@ extern "C" { * @return Number of entries in ef or -1 if the number of entries * cannot be read due to open mode restrictions. */ - EAPI int eet_num_entries(Eet_File *ef); + EAPI int eet_num_entries(Eet_File *ef); /***************************************************************************/ @@ -335,7 +346,7 @@ extern "C" { * On success the function returns 1 indicating the header was read and * decoded properly, or 0 on failure. */ - EAPI int eet_data_image_header_read(Eet_File *ef, const char *name, unsigned int *w, unsigned int *h, int *alpha, int *compress, int *quality, int *lossy); + EAPI int eet_data_image_header_read(Eet_File *ef, const char *name, unsigned int *w, unsigned int *h, int *alpha, int *compress, int *quality, int *lossy); /** * Read image data from the named key in the eet file. @@ -370,7 +381,7 @@ extern "C" { * when it is done with it. On failure NULL is returned and the parameter * values may not contain any sensible data. */ - EAPI void *eet_data_image_read(Eet_File *ef, const char *name, unsigned int *w, unsigned int *h, int *alpha, int *compress, int *quality, int *lossy); + EAPI void *eet_data_image_read(Eet_File *ef, const char *name, unsigned int *w, unsigned int *h, int *alpha, int *compress, int *quality, int *lossy); /** * Write image data to the named key in an eet file. @@ -402,7 +413,7 @@ extern "C" { * On success this function returns the number of bytes that were required * to encode the image data, or on failure it returns 0. */ - EAPI int eet_data_image_write(Eet_File *ef, const char *name, const void *data, unsigned int w, unsigned int h, int alpha, int compress, int quality, int lossy); + EAPI int eet_data_image_write(Eet_File *ef, const char *name, const void *data, unsigned int w, unsigned int h, int alpha, int compress, int quality, int lossy); /** * Decode Image data header only to get information. @@ -435,7 +446,7 @@ extern "C" { * On success the function returns 1 indicating the header was read and * decoded properly, or 0 on failure. */ - EAPI int eet_data_image_header_decode(const void *data, int size, unsigned int *w, unsigned int *h, int *alpha, int *compress, int *quality, int *lossy); + EAPI int eet_data_image_header_decode(const void *data, int size, unsigned int *w, unsigned int *h, int *alpha, int *compress, int *quality, int *lossy); /** * Decode Image data into pixel data. @@ -470,7 +481,7 @@ extern "C" { * when it is done with it. On failure NULL is returned and the parameter * values may not contain any sensible data. */ - EAPI void *eet_data_image_decode(const void *data, int size, unsigned int *w, unsigned int *h, int *alpha, int *compress, int *quality, int *lossy); + EAPI void *eet_data_image_decode(const void *data, int size, unsigned int *w, unsigned int *h, int *alpha, int *compress, int *quality, int *lossy); /** * Encode image data for storage or transmission. @@ -501,7 +512,7 @@ extern "C" { * On success this function returns a pointer to the encoded data that you * can free with free() when no longer needed. */ - EAPI void *eet_data_image_encode(const void *data, int *size_ret, unsigned int w, unsigned int h, int alpha, int compress, int quality, int lossy); + EAPI void *eet_data_image_encode(const void *data, int *size_ret, unsigned int w, unsigned int h, int alpha, int compress, int quality, int lossy); /***************************************************************************/ @@ -694,7 +705,7 @@ extern "C" { * call the descriptor is no longer valid. * */ - EAPI void eet_data_descriptor_free(Eet_Data_Descriptor *edd); + EAPI void eet_data_descriptor_free(Eet_Data_Descriptor *edd); /** * This function is an internal used by macros. @@ -705,7 +716,7 @@ extern "C" { * thus is not documented. * */ - EAPI void eet_data_descriptor_element_add(Eet_Data_Descriptor *edd, const char *name, int type, int group_type, int offset, int count, const char *counter_name, Eet_Data_Descriptor *subtype); + EAPI void eet_data_descriptor_element_add(Eet_Data_Descriptor *edd, const char *name, int type, int group_type, int offset, int count, const char *counter_name, Eet_Data_Descriptor *subtype); /** * Read a data structure from an eet file and decodes it. @@ -745,7 +756,7 @@ extern "C" { * to an eet file. * */ - EAPI int eet_data_write(Eet_File *ef, Eet_Data_Descriptor *edd, const char *name, const void *data, int compress); + EAPI int eet_data_write(Eet_File *ef, Eet_Data_Descriptor *edd, const char *name, const void *data, int compress); /** * Decode a data structure from an arbitary location in memory. diff --git a/legacy/eet/src/lib/Eet_private.h b/legacy/eet/src/lib/Eet_private.h index abcab1265d..df6eeb0c70 100644 --- a/legacy/eet/src/lib/Eet_private.h +++ b/legacy/eet/src/lib/Eet_private.h @@ -11,6 +11,7 @@ #include #include #include +#include #if HAVE_ALLOCA_H #include #endif diff --git a/legacy/eet/src/lib/eet_lib.c b/legacy/eet/src/lib/eet_lib.c index 1e5c16b93c..3391aa7f1b 100644 --- a/legacy/eet/src/lib/eet_lib.c +++ b/legacy/eet/src/lib/eet_lib.c @@ -79,7 +79,7 @@ static void eet_cache_add(Eet_File *ef, Eet_File ***cache, int *cache_num, static void eet_cache_del(Eet_File *ef, Eet_File ***cache, int *cache_num, int *cache_alloc); static int eet_string_match(const char *s1, const char *s2); static int eet_hash_gen(const char *key, int hash_size); -static void eet_flush(Eet_File *ef); +static Eet_Error eet_flush(Eet_File *ef); static Eet_File_Node *find_node_by_name (Eet_File *ef, const char *name); static int read_data_from_disk(Eet_File *ef, Eet_File_Node *efn, void *buf, int len); @@ -253,7 +253,7 @@ eet_hash_gen(const char *key, int hash_size) } /* flush out writes to an eet file */ -static void +static Eet_Error eet_flush(Eet_File *ef) { int i, count, size, num, offset; @@ -263,11 +263,11 @@ eet_flush(Eet_File *ef) /* check to see its' an eet file pointer */ if ((!ef) || (ef->magic != EET_MAGIC_FILE)) - return; - if (!ef->header) return; - if (!ef->header->directory) return; - if ((ef->mode != EET_FILE_MODE_WRITE) && (ef->mode != EET_FILE_MODE_READ_WRITE)) return; - if (!ef->writes_pending) return; + return EET_ERROR_BAD_OBJECT; + if (!ef->header) return EET_ERROR_EMPTY; + if (!ef->header->directory) return EET_ERROR_EMPTY; + if ((ef->mode != EET_FILE_MODE_WRITE) && (ef->mode != EET_FILE_MODE_READ_WRITE)) return EET_ERROR_NOT_WRITABLE; + if (!ef->writes_pending) return EET_ERROR_NONE; /* calculate total size in bytes of directory block */ size = 0; @@ -340,7 +340,24 @@ eet_flush(Eet_File *ef) i2 = htonl(i1); *((int *)(buf + 16)) = (int)i2; memcpy(buf + 20, efn->name, name_size); - if (fwrite(buf, buf_size, 1, ef->fp) != 1) return; + if (fwrite(buf, buf_size, 1, ef->fp) != 1) + { + write_error: + switch (ferror(ef->fp)) + { + case EFBIG: + return EET_ERROR_WRITE_ERROR_FILE_TOO_BIG; + case EIO: + return EET_ERROR_WRITE_ERROR_IO_ERROR; + case ENOSPC: + return EET_ERROR_WRITE_ERROR_OUT_OF_SPACE; + case EPIPE: + return EET_ERROR_WRITE_ERROR_FILE_CLOSED; + default: + return EET_ERROR_WRITE_ERROR; + } + return EET_ERROR_WRITE_ERROR; + } offset += buf_size; } } @@ -353,12 +370,13 @@ eet_flush(Eet_File *ef) if (efn->compression >= 0) { if (fwrite(efn->data, efn->size, 1, ef->fp) != 1) - return; + goto write_error; } } } /* no more writes pending */ ef->writes_pending = 0; + return EET_ERROR_NONE; } EAPI int @@ -721,20 +739,22 @@ eet_mode_get(Eet_File *ef) return ef->mode; } -EAPI void +EAPI Eet_Error eet_close(Eet_File *ef) { + Eet_Error err; + /* check to see its' an eet file pointer */ if ((!ef) || (ef->magic != EET_MAGIC_FILE)) - return; + return EET_ERROR_BAD_OBJECT; /* deref */ ef->references--; /* if its still referenced - dont go any further */ - if (ef->references > 0) return; + if (ef->references > 0) return EET_ERROR_NONE; /* if we are in cacheburst mode - dont free it - leave it in cache */ if (eet_cacheburst_mode) { - if (!ef->delete_me_now) return; + if (!ef->delete_me_now) return EET_ERROR_NONE; } /* remove from cache */ if (ef->mode == EET_FILE_MODE_READ) @@ -742,7 +762,7 @@ eet_close(Eet_File *ef) else if ((ef->mode == EET_FILE_MODE_WRITE) || (ef->mode == EET_FILE_MODE_READ_WRITE)) eet_cache_del(ef, &eet_writers, &eet_writers_num, &eet_writers_alloc); /* flush any writes */ - eet_flush(ef); + err = eet_flush(ef); /* free up members */ if (ef->fp) fclose(ef->fp); @@ -779,6 +799,7 @@ eet_close(Eet_File *ef) memset(ef, 0, sizeof(Eet_File)); /* free it */ free(ef); + return err; } EAPI void *