Changeset bcf71ac in tol
- Timestamp:
- May 28, 2010, 11:47:00 AM (16 years ago)
- Branches:
- 1941-docker-image-to-test, devel, master
- Children:
- af1369e
- Parents:
- d74c90b
- Location:
- tol
- Files:
-
- 5 edited
-
bbasic/sys.cpp (modified) (2 diffs)
-
bbasic/tol_bsys.h (modified) (1 diff)
-
btol/bdb/filehandle.cpp (modified) (1 diff)
-
btol/bdb/tol_filehandle.h (modified) (1 diff)
-
btol/matrix_type/matgra.cpp (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tol/bbasic/sys.cpp
rd74c90b rbcf71ac 995 995 //-------------------------------------------------------------------- 996 996 //Opens a file and locks it 997 //If cannot do it then sleeps and retries again until time is out 998 FILE* FOpenAndLock( 997 static FILE* fOpenAndLock( 998 const char *filename, 999 const char *mode) 1000 //-------------------------------------------------------------------- 1001 { 1002 FILE* fil=NULL; 1003 #ifdef _MSC_VER 1004 fopen_s(&fil, filename, mode); 1005 #else 1006 fil = fopen(filename, mode); 1007 #endif 1008 return(fil); 1009 } 1010 1011 //-------------------------------------------------------------------- 1012 //Opens a file and locks it 1013 //If doesn't can do it then sleeps and retries again until time is out 1014 FILE* BSys::FOpenAndLock( 999 1015 const char *filename, 1000 1016 const char *mode, … … 1004 1020 { 1005 1021 FILE* fil=NULL; 1022 double sleep = 1.0; 1023 double sleepCum = 0.0; 1024 int tryNum = 0; 1025 bool ok = false; 1026 do 1027 { 1028 #ifdef _MSC_VER 1029 ok = fopen_s(&fil, filename, mode)==0; 1030 #else 1031 fil = fopen(filename, mode); 1032 ok = fil!=NULL; 1033 #endif 1034 if(!ok) 1035 { 1036 fil = NULL; 1037 BSys::SleepMilliSeconds((unsigned int)sleep); 1038 sleepCum += sleep; 1039 sleep = floor(1.1*sleep)+(1+timeOutInMilliseconds/100); 1040 tryNum++; 1041 Warning(BText("[BSys::FOpenAndLock] Retraying to open locked file ")+filename+" for "+tryNum+"-th time\n"); 1042 } 1043 } while(!fil && (sleepCum<timeOutInMilliseconds)); 1044 if(!fil && errorMessage && errorMessage[0]) 1045 { 1046 Error(BText("Cannot open file ")+filename+" in mode "+mode+"\n"+ 1047 errorMessage); 1048 } 1006 1049 return(fil); 1007 1050 } 1008 1051 1009 1052 //-------------------------------------------------------------------- 1010 //Unlocks a file and close it 1011 i FUnlockAndClose(FILE* file) 1012 //-------------------------------------------------------------------- 1013 { 1014 FILE* fil=NULL; 1015 return(fil); 1016 } 1053 //Unlocks a file and closes it 1054 bool BSys::FUnlockAndClose(FILE* file, const char *filename) 1055 //-------------------------------------------------------------------- 1056 { 1057 if(fclose(file)) 1058 { 1059 Error(BText("Cannot close file ")+filename); 1060 return(false); 1061 } 1062 return(true); 1063 } -
tol/bbasic/tol_bsys.h
rd74c90b rbcf71ac 164 164 const char *filename, 165 165 const char *mode, 166 unsigned int timeOutInMilliseconds =0,167 char* errorMessage =NULL);166 unsigned int timeOutInMilliseconds, 167 char* errorMessage); 168 168 //Unlocks a file and close it 169 static void FUnlockAndClose(FILE* file);169 static bool FUnlockAndClose(FILE* file, const char *filename); 170 170 }; 171 171 -
tol/btol/bdb/filehandle.cpp
rd74c90b rbcf71ac 61 61 62 62 ///////////////////////////////////////////////////////////////////////////// 63 FILE* BFileDesc::CheckFileHandle(int handle, bool verbose, const BText& msg, 64 BFileHashByHandle::const_iterator& found) 65 ///////////////////////////////////////////////////////////////////////////// 66 { 67 FILE* file; 63 BFileDesc* BFileDesc::FindFileHandle( 64 int handle, bool verbose, const BText& msg, 65 BFileHashByHandle::const_iterator& found) 66 ///////////////////////////////////////////////////////////////////////////// 67 { 68 BFileDesc* fd; 68 69 found = FileHandle().find(handle); 69 70 if(found==FileHandle().end()) 70 71 { 71 f ile= NULL;72 fd = NULL; 72 73 } 73 74 else 74 75 { 75 f ile = found->second->file_;76 } 77 if(!f ile&& verbose)76 fd = found->second; 77 } 78 if(!fd && verbose) 78 79 { 79 80 Error(I2("Invalid file descriptor ", 80 81 "Descriptor de fichero inválido ")+handle+"\n"+msg); 81 82 } 82 return(file); 83 return(fd); 84 }; 85 86 ///////////////////////////////////////////////////////////////////////////// 87 BFileDesc* BFileDesc::FindFileHandle( 88 int handle, bool verbose, const BText& msg) 89 ///////////////////////////////////////////////////////////////////////////// 90 { 91 BFileHashByHandle::const_iterator found; 92 return(FindFileHandle(handle, verbose, msg, found)); 93 }; 94 95 ///////////////////////////////////////////////////////////////////////////// 96 FILE* BFileDesc::CheckFileHandle(int handle, bool verbose, const BText& msg, 97 BFileHashByHandle::const_iterator& found) 98 ///////////////////////////////////////////////////////////////////////////// 99 { 100 BFileDesc* fd = FindFileHandle(handle, verbose, msg, found); 101 return((fd)?fd->file_:NULL); 83 102 }; 84 103 -
tol/btol/bdb/tol_filehandle.h
rd74c90b rbcf71ac 44 44 45 45 static BFileHashByHandle& FileHandle(); 46 static BFileDesc* FindFileHandle(int handle, bool verbose, const BText& msg, 47 BFileHashByHandle::const_iterator& found); 48 49 static BFileDesc* FindFileHandle(int handle, bool verbose, const BText& msg); 46 50 static FILE* CheckFileHandle(int handle, bool verbose, const BText& msg, 47 51 BFileHashByHandle::const_iterator& found); -
tol/btol/matrix_type/matgra.cpp
rd74c90b rbcf71ac 175 175 #endif 176 176 177 //Time out for open BBM files in milliseconds 178 #define BBM_TimeOut 1000 179 177 180 //-------------------------------------------------------------------- 178 181 void BBM_BinWrite(const BText& fileName, const BMat& M) 179 182 //-------------------------------------------------------------------- 180 183 { 181 FILE* fil = fopen(fileName.String(),"wb"); 182 if(!fil) 183 { 184 Error(I2("Cannot open for write BBM file ", 185 "No se pudo abrir para escritura el fichero BBM ")+ 186 fileName); 187 return; 188 } 184 FILE* fil = BSys::FOpenAndLock(fileName.String(),"wb", 185 BBM_TimeOut, "Unexpected error writing to BBM file"); 186 if(!fil) { return; } 189 187 BInt m = M.Rows(); 190 188 BInt n = M.Columns(); … … 192 190 fwrite(&n,sizeof(BInt),1,fil); 193 191 fwrite(M.Data().Buffer(), sizeof(BDat), n*m, fil); 194 #ifndef NDEBUG 195 int lastPos = ftell(fil); 196 #endif 197 if(fclose(fil)) 198 { 199 Error(I2("Cannot close after writing BBM file ", 200 "No se pudo cerrar despues de escribir el fichero BBM ")+ 201 fileName); 202 }; 203 #ifndef NDEBUG 204 BInt fs = GetFileSize(fileName); 205 assert(fs==lastPos); 206 #endif 192 if(!BSys::FUnlockAndClose(fil,fileName.String())) { return; } 207 193 BBM_CheckSize("BBM_BinWrite",fileName,m,n); 208 194 } … … 229 215 230 216 /* open for overwrite */ 231 if(!(fil = fopen(fileName.String(), "r+b"))) { 232 Error(I2("Cannot open (rewrite mode) BBM file ", 233 "No se pudo abrir (modo de sobreescritura) el fichero BBM ") + 234 fileName); 235 return; 236 } 237 fpos_t pos = 0; 238 fsetpos(fil, &pos); 217 fil = BSys::FOpenAndLock(fileName.String(),"a+b", 218 BBM_TimeOut, "Unexpected error appending BBM file"); 219 if(!fil) { return; } 220 fpos_t pos; 221 pos = 0; fsetpos(fil, &pos); 239 222 fread(&rows,sizeof(BInt),1,fil); 240 223 fread(&cols,sizeof(BInt),1,fil); 241 224 rows2w = M.Rows(); 242 225 cols2w = M.Columns(); 243 if(cols!=cols2w) { 226 if(cols!=cols2w) 227 { 244 228 Error("MatAppendFile: Different number of columns"); 245 fclose(fil); 246 return; 247 } 248 pos = 0; 249 fsetpos(fil, &pos); 250 rows += rows2w; 251 fwrite(&rows, sizeof(BInt), 1, fil); 252 fflush(fil); 253 fclose(fil); 254 fil = NULL; 255 for(int tryNum = 0; !fil && (tryNum < 1000); tryNum++) 256 { 257 fil = fopen(fileName.String(), "ab"); 258 } 259 /* open for append */ 260 if(!fil) { 261 Error(I2("Cannot open (append mode) BBM file ", 262 "No se pudo abrir (modo de insercion) el fichero BBM ") + 263 fileName); 264 return; 265 } 266 fwrite(M.Data().Buffer(), sizeof(BDat), rows2w*cols2w, fil); 267 fflush(fil); 268 if(fclose(fil)) 269 { 270 Error(I2("Cannot close after append BBM file ", 271 "No se pudo cerrar despues de añadir al fichero BBM ")+ 272 fileName); 273 }; 229 } 230 else 231 { 232 rows += rows2w; 233 pos = rows*cols*sizeof(BDat)+2*sizeof(int); fsetpos(fil, &pos); 234 fwrite(M.Data().Buffer(), sizeof(BDat), rows2w*cols2w, fil); 235 if(!BSys::FUnlockAndClose(fil,fileName.String())) { return; } 236 fil = BSys::FOpenAndLock(fileName.String(),"r+b", 237 BBM_TimeOut*10, "Unexpected error changing BBM file"); 238 if(!fil) { return; } 239 pos = 0; fsetpos(fil, &pos); 240 fwrite(&rows, sizeof(BInt), 1, fil); 241 } 242 BSys::FUnlockAndClose(fil,fileName.String()); 274 243 //BBM_CheckSize("BBM_BinAppend",fileName,rows,cols); 275 244 } … … 282 251 BInt fs = GetFileSize(fileName); 283 252 //Std(BText("\nBBM_BinRead ")+fileName+" bytes "+fs); 284 FILE* fil = fopen(fileName.String(),"rb"); 285 if(!fil) 286 { 287 Error(I2("Cannot open for read BBM file ", 288 "No se pudo abrir para lectura el fichero BBM ")+ 289 fileName); 290 return; 291 } 253 FILE* fil = BSys::FOpenAndLock(fileName.String(),"rb", 254 BBM_TimeOut, "Unexpected error reading from BBM file"); 255 if(!fil) { return; } 256 292 257 BInt i, m, n, mn; 293 258 fread(&m,sizeof(BInt),1,fil); … … 295 260 if(!m || !n) 296 261 { 297 fclose(fil);262 BSys::FUnlockAndClose(fil,fileName.String()); 298 263 return; 299 264 } … … 306 271 if(M.Rows()!=m) { return; } 307 272 fread(M.GetData().GetBuffer(), sizeof(BDat), mn, fil); 308 if(fclose(fil)) 309 { 310 Error(I2("Cannot close after reading BBM file ", 311 "No se pudo cerrar despues de leer el fichero BBM ")+ 312 fileName); 313 }; 273 if(!BSys::FUnlockAndClose(fil,fileName.String())) { return; } 314 274 } 315 275 //In TOL versions before 2007-01-15 structure page size = 8 => sizeof(BDat)=16 … … 320 280 BArray<BOis::BDatOld> M_; 321 281 fread(M_.GetBuffer(), sizeof(BOis::BDatOld), mn, fil); 322 if(fclose(fil)) 323 { 324 Error(I2("Cannot close after reading BBM file ", 325 "No se pudo cerrar despues de leer el fichero BBM ")+ 326 fileName); 327 }; 282 if(!BSys::FUnlockAndClose(fil,fileName.String())) { return; } 328 283 for(i=0; i<mn; i++) 329 284 { … … 343 298 BDat* data = M.GetData().GetBuffer(); 344 299 fread(buffer, 1, s, fil); 345 if(fclose(fil)) 346 { 347 Error(I2("Cannot close after reading BBM file ", 348 "No se pudo cerrar despues de leer el fichero BBM ")+ 349 fileName); 350 }; 300 if(!BSys::FUnlockAndClose(fil,fileName.String())) { return; } 351 301 for(i=0; i<mn; i++) 352 302 { … … 371 321 M.Alloc(m,n); 372 322 fread(M.GetData().GetBuffer(), sizeof(BDat), mn, fil); 373 if(fclose(fil)) 374 { 375 Error(I2("Cannot close after reading BBM file ", 376 "No se pudo cerrar despues de leer el fichero BBM ")+ 377 fileName); 378 }; 379 323 if(!BSys::FUnlockAndClose(fil,fileName.String())) { return; } 380 324 } 381 325 else … … 386 330 387 331 //-------------------------------------------------------------------- 388 void BBM_BinReadRows(FILE* fil, BMat& M, 332 void BBM_BinReadRows(FILE* fil, 333 const BText& fileName, 334 BMat& M, 389 335 int m, int n, 390 336 int i, int r, int t=1) … … 441 387 442 388 //-------------------------------------------------------------------- 443 void BBM_BinReadCell(FILE* fil, BDat& cell, 389 void BBM_BinReadCell(FILE* fil, 390 const BText& fileName, 391 BDat& cell, 444 392 int m, int n, 445 393 int i, int j) … … 455 403 (0>j)||(j>=n) ) 456 404 { 457 fclose(fil);405 BSys::FUnlockAndClose(fil,fileName.String()); 458 406 return; 459 407 } … … 470 418 BInt fs = GetFileSize(fileName); 471 419 //Std(BText("\nBBM_BinRead ")+fileName+" bytes "+fs); 472 FILE* fil = fopen(fileName.String(),"rb"); 473 if(!fil) 474 { 475 Error(I2("Cannot open for read BBM file ", 476 "No se pudo abrir para lectura el fichero BBM ")+ 477 fileName); 478 return; 479 } 420 FILE* fil = BSys::FOpenAndLock(fileName.String(),"rb", 421 BBM_TimeOut, "Unexpected error reading from BBM file"); 422 if(!fil) { return; } 480 423 fread(&rows, sizeof(BInt),1,fil); 481 424 fread(&columns,sizeof(BInt),1,fil); 482 if(fclose(fil)) 483 { 484 Error(I2("Cannot close after reading BBM file ", 485 "No se pudo cerrar despues de leer el fichero BBM ")+ 486 fileName); 487 }; 425 BSys::FUnlockAndClose(fil,fileName.String()); 488 426 } 489 427 … … 649 587 { 650 588 int handle = (int)Real(Arg(1)); 651 FILE* file = BFileDesc::CheckFileHandle(handle,true,589 BFileDesc* fd = BFileDesc::FindFileHandle(handle,true, 652 590 I2("Fail in ","Fallo en ")+"BMatReadRows"); 653 if(f ile)654 { 655 BBM_BinReadRows(f ile,contens_,m,n,i,r,t);591 if(fd) 592 { 593 BBM_BinReadRows(fd->file_,fd->path_.String(),contens_,m,n,i,r,t); 656 594 } 657 595 } … … 659 597 { 660 598 BText& path = Text(Arg(1)); 661 file = fopen(path.String(),"rb"); 662 if(!file) 663 { 664 Error(I2("Cannot open for MatReadRows BBM file ", 665 "No se pudo abrir para MatReadRows el fichero BBM ")+ 666 path); 667 return; 668 } 669 BBM_BinReadRows(file,contens_,m,n,i,r,t); 670 fclose(file); 599 file = BSys::FOpenAndLock(path.String(),"rb", 600 BBM_TimeOut, "Unexpected error reading from BBM file"); 601 if(!file) { return; } 602 BBM_BinReadRows(file,path.String(),contens_,m,n,i,r,t); 603 BSys::FUnlockAndClose(file,path.String()); 671 604 } 672 605 } … … 711 644 { 712 645 int handle = (int)Real(Arg(1)); 713 FILE* file = BFileDesc::CheckFileHandle(handle,true,646 BFileDesc* fd = BFileDesc::FindFileHandle(handle,true, 714 647 I2("Fail in ","Fallo en ")+"MatReadCell"); 715 if(f ile)716 { 717 BBM_BinReadCell(f ile,contens_,m,n,i,j);648 if(fd) 649 { 650 BBM_BinReadCell(fd->file_,fd->path_.String(),contens_,m,n,i,j); 718 651 } 719 652 } … … 721 654 { 722 655 BText& path = Text(Arg(1)); 723 file = fopen(path.String(),"rb"); 724 if(!file) 725 { 726 Error(I2("Cannot open for MatReadCell BBM file ", 727 "No se pudo abrir para MatReadCell el fichero BBM ")+ 728 path); 729 return; 730 } 731 BBM_BinReadCell(file,contens_,m,n,i,j); 732 fclose(file); 656 file = BSys::FOpenAndLock(path.String(),"rb", 657 BBM_TimeOut, "Unexpected error reading from BBM file"); 658 if(!file) { return; } 659 BBM_BinReadCell(file,path.String(),contens_,m,n,i,j); 660 BSys::FUnlockAndClose(file,path.String()); 733 661 } 734 662 }
Note: See TracChangeset
for help on using the changeset viewer.
