Ignore:
Timestamp:
11/07/10 01:36:30 (13 years ago)
Author:
atzm
Message:
  • check dictionary loadable
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pymigemo/pymigemo.c

    r35 r36  
    1111#include <stdbool.h> 
    1212#include <string.h> 
     13#include <errno.h> 
     14#include <sys/types.h> 
     15#include <sys/stat.h> 
     16#include <fcntl.h> 
     17#include <unistd.h> 
    1318 
    1419#define PYMIGEMO_VERSION "0.3" 
     
    6065} 
    6166 
     67static int 
     68isloadable(const char *path) 
     69{ 
     70    struct stat st; 
     71    int ret = 0; 
     72    int fd  = open(path, O_RDONLY); 
     73 
     74    if (fd < 0) { 
     75        return errno; 
     76    } 
     77 
     78    if (fstat(fd, &st) < 0) { 
     79        ret = errno; 
     80        goto isloadable_end; 
     81    } 
     82    if (S_ISDIR(st.st_mode)) { 
     83        ret = EISDIR; 
     84        goto isloadable_end; 
     85    } 
     86 
     87  isloadable_end: 
     88    if (close(fd) < 0) { 
     89        ret = errno; 
     90    } 
     91    return ret; 
     92} 
     93 
    6294static void 
    6395Migemo_dealloc(Migemo *self) 
     
    97129 
    98130    if (dictionary) { 
     131        int ret = isloadable(dictionary); 
     132 
     133        if (ret != 0) { 
     134            PyErr_SetString(PyExc_ValueError, strerror(ret)); 
     135            return -1; 
     136        } 
     137 
    99138        if (self->migemo_obj) { 
    100139            migemo_close(self->migemo_obj); 
     
    241280 
    242281    if (dict_file) { 
     282        int ret = isloadable(dict_file); 
     283 
     284        if (ret != 0) { 
     285            PyErr_SetString(PyExc_ValueError, strerror(ret)); 
     286            return NULL; 
     287        } 
     288 
    243289        result = PyInt_FromLong((long)migemo_load(self->migemo_obj, dict_id, dict_file)); 
    244290    } 
Note: See TracChangeset for help on using the changeset viewer.