--- modules/core/src/persistence.cpp.orig 2013-10-19 18:45:28.000000000 -0700 +++ modules/core/src/persistence.cpp 2013-10-20 22:17:44.000000000 -0700 @@ -46,6 +46,9 @@ #include #include #include +#include "naclmounts/base/Mount.h" +#include "naclmounts/base/MountManager.h" +#include "naclmounts/memory/MemMount.h" #define USE_ZLIB 1 @@ -246,8 +249,9 @@ CvString struct_tag; int space; char* filename; - FILE* file; - gzFile gzfile; + KernelProxy* kp; + int file; + char* gzfile; char* buffer; char* buffer_start; char* buffer_end; @@ -277,11 +281,15 @@ { if( fs->outbuf ) std::copy(str, str + strlen(str), std::back_inserter(*fs->outbuf)); - else if( fs->file ) - fputs( str, fs->file ); + else if( fs->file != -1){ + if(fs->kp != NULL) + fs->kp->write(fs->file, str, strlen(str)); + else + CV_Error( CV_StsError, "NaclMount is not already setup"); + } #if USE_ZLIB - else if( fs->gzfile ) - gzputs( fs->gzfile, str ); + //else if( fs->gzfile ) + //gzputs( fs->gzfile, str ); #endif else CV_Error( CV_StsError, "The storage is not opened" ); @@ -307,11 +315,25 @@ fs->strbufpos = i; return j > 1 ? str : 0; } - if( fs->file ) - return fgets( str, maxCount, fs->file ); + if( fs->file != -1) { + // Read until newline or end-of-line to mimic fgets + char *c = (char*)malloc(sizeof(char)); + int i = 0; + do { + int numRead = (int) fs->kp->read(fs->file, c, 1); + if(numRead == 0) break; // EOF + str[i++] = *c; + } while (*c != '\n' && i < maxCount - 1); + + // EOF and nothing read + if (i == 0) return 0; + + str[i] = '\0'; + return str; + } #if USE_ZLIB - if( fs->gzfile ) - return gzgets( fs->gzfile, str, maxCount ); + //if( fs->gzfile ) + //return gzgets( fs->gzfile, str, maxCount ); #endif CV_Error( CV_StsError, "The storage is not opened" ); return 0; @@ -321,22 +343,30 @@ { if( fs->strbuf ) return fs->strbufpos >= fs->strbufsize; - if( fs->file ) - return feof(fs->file); + if( fs->file != -1){ + char *c = (char*)malloc(sizeof(char)); + int l = fs->kp->read(fs->file, c, 1); + if(l > 0){ + // restore to last position + fs->kp->lseek(fs->file, -1, SEEK_CUR); + } + //EOF if length is zero + return (l == 0) ? 1 : 0; + } #if USE_ZLIB - if( fs->gzfile ) - return gzeof(fs->gzfile); + //if( fs->gzfile ) + //return gzeof(fs->gzfile); #endif return false; } static void icvCloseFile( CvFileStorage* fs ) { - if( fs->file ) - fclose( fs->file ); + if( fs->file != -1) + fs->kp->close(fs->file); #if USE_ZLIB - else if( fs->gzfile ) - gzclose( fs->gzfile ); + //else if( fs->gzfile ) + //gzclose( fs->gzfile ); #endif fs->file = 0; fs->gzfile = 0; @@ -347,11 +377,11 @@ static void icvRewind( CvFileStorage* fs ) { - if( fs->file ) - rewind(fs->file); + if( fs->file != -1) + fs->kp->lseek(fs->file, 0, SEEK_SET); #if USE_ZLIB - else if( fs->gzfile ) - gzrewind(fs->gzfile); + //else if( fs->gzfile ) + //gzrewind(fs->gzfile); #endif fs->strbufpos = 0; } @@ -362,7 +392,7 @@ #define CV_FS_MAX_LEN 4096 #define CV_FILE_STORAGE ('Y' + ('A' << 8) + ('M' << 16) + ('L' << 24)) -#define CV_IS_FILE_STORAGE(fs) ((fs) != 0 && (fs)->flags == CV_FILE_STORAGE) +#define CV_IS_FILE_STORAGE(fs) ((fs)->file != -1 && (fs)->flags == CV_FILE_STORAGE) #define CV_CHECK_FILE_STORAGE(fs) \ { \ @@ -556,7 +586,7 @@ if( fs->is_opened ) { - if( fs->write_mode && (fs->file || fs->gzfile || fs->outbuf) ) + if( fs->write_mode && (fs->file != -1 || fs->gzfile || fs->outbuf) ) { if( fs->write_stack ) { @@ -2738,17 +2768,18 @@ if( !isGZ ) { - fs->file = fopen(fs->filename, !fs->write_mode ? "rt" : !append ? "wt" : "a+t" ); - if( !fs->file ) + fs->kp = MountManager::MMInstance()->kp(); + fs->file = fs->kp->open(filename, !fs->write_mode ? O_RDONLY : O_WRONLY, 0); + if( fs->file == -1) goto _exit_; } else { #if USE_ZLIB - char mode[] = { fs->write_mode ? 'w' : 'r', 'b', compression ? compression : '3', '\0' }; - fs->gzfile = gzopen(fs->filename, mode); - if( !fs->gzfile ) - goto _exit_; + //char mode[] = { fs->write_mode ? 'w' : 'r', 'b', compression ? compression : '3', '\0' }; + //fs->gzfile = gzopen(fs->filename, mode); + //if( !fs->gzfile ) + //goto _exit_; #else CV_Error(CV_StsNotImplemented, "There is no compressed file storage support in this configuration"); #endif @@ -2782,7 +2813,7 @@ int buf_size = CV_FS_MAX_LEN*(fs->fmt == CV_STORAGE_FORMAT_XML ? 6 : 4) + 1024; if( append ) - fseek( fs->file, 0, SEEK_END ); + fs->kp->lseek(fs->file, 0, SEEK_END); fs->write_stack = cvCreateSeq( 0, sizeof(CvSeq), fs->fmt == CV_STORAGE_FORMAT_XML ? sizeof(CvXMLStackRecord) : sizeof(int), fs->memstorage ); @@ -2793,7 +2824,12 @@ fs->buffer_end = fs->buffer_start + buf_size; if( fs->fmt == CV_STORAGE_FORMAT_XML ) { - size_t file_size = fs->file ? ftell( fs->file ) : (size_t)0; + size_t file_size = (size_t)0; + if(fs->file != -1){ + off_t current = fs->kp->lseek(fs->file, 0, SEEK_CUR); + file_size = (size_t)fs->kp->lseek(fs->file, 0, SEEK_END); + fs->kp->lseek(fs->file, current, SEEK_SET); + } fs->strstorage = cvCreateChildMemStorage( fs->memstorage ); if( !append || file_size == 0 ) { @@ -2819,12 +2855,12 @@ char substr[] = ""; int last_occurence = -1; xml_buf_size = MIN(xml_buf_size, int(file_size)); - fseek( fs->file, -xml_buf_size, SEEK_END ); + fs->kp->lseek( fs->file, -xml_buf_size, SEEK_END ); xml_buf = (char*)cvAlloc( xml_buf_size+2 ); // find the last occurence of for(;;) { - int line_offset = ftell( fs->file ); + int line_offset = fs->kp->lseek(fs->file, 0, SEEK_CUR ); char* ptr0 = icvGets( fs, xml_buf, xml_buf_size ), *ptr; if( !ptr0 ) break; @@ -2841,11 +2877,11 @@ if( last_occurence < 0 ) CV_Error( CV_StsError, "Could not find in the end of file.\n" ); icvCloseFile( fs ); - fs->file = fopen( fs->filename, "r+t" ); - fseek( fs->file, last_occurence, SEEK_SET ); + fs->file = fs->kp->open( fs->filename, O_RDONLY, 0 ); + fs->kp->lseek( fs->file, last_occurence, SEEK_SET ); // replace the last "" with " ", which has the same length icvPuts( fs, " " ); - fseek( fs->file, 0, SEEK_END ); + fs->kp->lseek( fs->file, 0, SEEK_END ); icvPuts( fs, "\n" ); } fs->start_write_struct = icvXMLStartWriteStruct; @@ -2890,8 +2926,8 @@ { if( !mem ) { - fseek( fs->file, 0, SEEK_END ); - buf_size = ftell( fs->file ); + fs->kp->lseek( fs->file, 0, SEEK_END ); + buf_size = fs->kp->lseek( fs->file, 0, SEEK_CUR ); } else buf_size = fs->strbufsize; @@ -2928,7 +2964,7 @@ _exit_: if( fs ) { - if( cvGetErrStatus() < 0 || (!fs->file && !fs->gzfile && !fs->outbuf && !fs->strbuf) ) + if( cvGetErrStatus() < 0 || (fs->file == -1 && !fs->gzfile && !fs->outbuf && !fs->strbuf) ) { cvReleaseFileStorage( &fs ); } @@ -5187,7 +5223,7 @@ bool FileStorage::isOpened() const { - return !fs.empty() && fs.obj->is_opened; + return !fs.empty() && fs->is_opened && fs->file != -1; } void FileStorage::release()