int OTF_File_revive( OTF_File* file, OTF_FileMode mode ) { switch ( mode ) { case OTF_FILEMODE_READ : /* *** read *** */ if ( NULL == file->file ) { /* file currently closed */ /* OTF_fprintf( stderr, "OTF_File_revive() READ: ask FileManager for free handle\n" ); */ if ( 0 == OTF_FileManager_guaranteeFile( file->manager ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_guaranteeFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } if ( 0 != file->pos ) { /* re-open */ file->file= fopen( file->filename, "rb" ); if( NULL == file->file ) { /* show this error every time */ OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "cannot open file %s for reading. Maybe the number of " "opened filehandles exceeds your system's limit\n", __FUNCTION__, __FILE__, __LINE__, file->filename ); return 0; } fseeko( file->file, file->pos, SEEK_SET ); } else { /* open first time */ file->file= fopen( file->filename, "rb" ); if( NULL == file->file ) { /* show this error every time */ OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "cannot open file %s for reading. Maybe the number of " "opened filehandles exceeds your system's limit\n", __FUNCTION__, __FILE__, __LINE__, file->filename ); return 0; } } if ( 0 == OTF_FileManager_registerFile( file->manager, file ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_registerFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } } else { /* file already opened */ /* OTF_fprintf( stderr, "OTF_File_revive() READ: update FileManagers LRU list\n" ); */ if ( 0 == OTF_FileManager_touchFile( file->manager, file ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_touchFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } } return 1; case OTF_FILEMODE_WRITE : /* *** write *** */ if ( NULL == file->file ) { /* file currently closed */ /* OTF_fprintf( stderr, "OTF_File_revive() WRITE: ask FileManager for free handle\n" ); */ if ( 0 == OTF_FileManager_guaranteeFile( file->manager ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_guaranteeFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } if ( 0 != file->pos ) { /* re-open */ file->file= fopen( file->filename, "ab" ); if( NULL == file->file ) { /* show this error every time */ OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "cannot open file %s for writing. Maybe the number of " "opened filehandles exceeds your system's limit\n", __FUNCTION__, __FILE__, __LINE__, file->filename ); return 0; } } else { /* open first time */ file->file= fopen( file->filename, "wb" ); if( NULL == file->file ) { /* show this error every time */ OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "cannot open file %s for writing. Maybe the number of " "opened filehandles exceeds your system's limit\n", __FUNCTION__, __FILE__, __LINE__, file->filename ); return 0; } } /* OTF_fprintf( stderr, "OTF_File_revive() WRITE: register opened file with FileManager\n" ); */ if ( 0 == OTF_FileManager_registerFile( file->manager, file ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_registerFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } } else { /* file already opened */ /* OTF_fprintf( stderr, "OTF_File_revive() WRITE: update FileManagers LRU list\n" ); */ if ( 0 == OTF_FileManager_touchFile( file->manager, file ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_touchFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } } return 1; case OTF_FILEMODE_SEEK : /* *** seek *** */ if ( NULL == file->file ) { /* file currently closed */ /* OTF_fprintf( stderr, "OTF_File_revive() READ: ask FileManager for free handle\n" ); */ if ( 0 == OTF_FileManager_guaranteeFile( file->manager ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_guaranteeFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } if ( 0 != file->pos ) { /* re-open */ file->file= fopen( file->filename, "rb" ); if( NULL == file->file ) { /* show this error every time */ OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "cannot open file %s for reading. Maybe the number of " "opened filehandles exceeds your system's limit\n", __FUNCTION__, __FILE__, __LINE__, file->filename ); return 0; } /* dont need to seek to the saved position because there will be another seek anyway*/ /* fseeko( file->file, file->pos, SEEK_SET ); */ } else { /* open first time */ file->file= fopen( file->filename, "rb" ); if( NULL == file->file ) { /* show this error every time */ OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "cannot open file %s for reading. Maybe the number of " "opened filehandles exceeds your system's limit\n", __FUNCTION__, __FILE__, __LINE__, file->filename ); return 0; } } /* OTF_fprintf( stderr, "OTF_File_revive() SEEK: register opened file with FileManager\n" ); */ if ( 0 == OTF_FileManager_registerFile( file->manager, file ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_registerFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } } else { /* file already opened */ /* OTF_fprintf( stderr, "OTF_File_revive() READ: update FileManagers LRU list\n" ); */ if ( 0 == OTF_FileManager_touchFile( file->manager, file ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_touchFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } } return 1; default: /* *** unknown mode *** */ return 0; } }
int OTF_File_revive( OTF_File* file, OTF_FileMode mode ) { if ( NULL != file->externalbuffer ) { /* no need to revive, everything is fine in 'external buffer' mode */ return 1; } switch ( mode ) { case OTF_FILEMODE_READ : /* *** read *** */ if ( NULL == file->file ) { /* file currently closed, aka open or reopen */ /* OTF_fprintf( stderr, "OTF_File_revive() READ: ask FileManager for free handle\n" ); */ if ( 0 == OTF_FileManager_guaranteeFile( file->manager ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_guaranteeFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } /* open first time, as we open O_RDONLY plus O_NOATIME, which fopen doesn't know, use open/fdopen */ #ifdef _GNU_SOURCE { int fd; int retry_num = 5; int flags = O_RDONLY | O_NOATIME; while ( -1 == ( fd = open( file->filename, flags ) ) ) { /* if the user is not the owner of the file, open with O_NOATIME will fail with errno == EPERM; try to open without O_NOATIME again to avoid this problem */ if ( EPERM == errno ) { flags = O_RDONLY; continue; /* the file name might be stale, e.g. on Network File System (NFS) */ } else if ( ESTALE == errno && 0 < --retry_num ) { sleep(1); continue; } else { /* show this error every time */ OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "cannot open file %s for reading. Maybe the number of " "opened filehandles exceeds your system's limit\n", __FUNCTION__, __FILE__, __LINE__, file->filename ); return 0; } } file->file= fdopen( fd, "r" ); } #else /* _GNU_SOURCE */ file->file= fopen( file->filename, "rb" ); #endif /* _GNU_SOURCE */ if( NULL == file->file ) { /* show this error every time */ OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "cannot open file %s for reading. Maybe the number of " "opened filehandles exceeds your system's limit\n", __FUNCTION__, __FILE__, __LINE__, file->filename ); return 0; } /* Upon repoen, seek to the current position */ if ( 0 != file->pos ) { fseeko( file->file, file->pos, SEEK_SET ); } if ( 0 == OTF_FileManager_registerFile( file->manager, file ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_registerFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } } else { /* file already opened */ /* OTF_fprintf( stderr, "OTF_File_revive() READ: update FileManagers LRU list\n" ); */ if ( 0 == OTF_FileManager_touchFile( file->manager, file ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_touchFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } } return 1; case OTF_FILEMODE_WRITE : /* *** write *** */ if ( NULL == file->file ) { /* file currently closed */ /* OTF_fprintf( stderr, "OTF_File_revive() WRITE: ask FileManager for free handle\n" ); */ if ( 0 == OTF_FileManager_guaranteeFile( file->manager ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_guaranteeFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } if ( 0 != file->pos ) { /* re-open */ file->file= fopen( file->filename, "ab" ); if( NULL == file->file ) { /* show this error every time */ OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "cannot open file %s for writing. Maybe the number of " "opened filehandles exceeds your system's limit\n", __FUNCTION__, __FILE__, __LINE__, file->filename ); return 0; } } else { /* open first time */ file->file= fopen( file->filename, "wb" ); if( NULL == file->file ) { /* show this error every time */ OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "cannot open file %s for writing. Maybe the number of " "opened filehandles exceeds your system's limit\n", __FUNCTION__, __FILE__, __LINE__, file->filename ); return 0; } } /* OTF_fprintf( stderr, "OTF_File_revive() WRITE: register opened file with FileManager\n" ); */ if ( 0 == OTF_FileManager_registerFile( file->manager, file ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_registerFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } } else { /* file already opened */ /* OTF_fprintf( stderr, "OTF_File_revive() WRITE: update FileManagers LRU list\n" ); */ if ( 0 == OTF_FileManager_touchFile( file->manager, file ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_touchFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } } return 1; case OTF_FILEMODE_SEEK : /* *** seek *** */ if ( NULL == file->file ) { /* file currently closed */ /* OTF_fprintf( stderr, "OTF_File_revive() READ: ask FileManager for free handle\n" ); */ if ( 0 == OTF_FileManager_guaranteeFile( file->manager ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_guaranteeFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } if ( 0 != file->pos ) { /* re-open */ file->file= fopen( file->filename, "rb" ); if( NULL == file->file ) { /* show this error every time */ OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "cannot open file %s for reading. Maybe the number of " "opened filehandles exceeds your system's limit\n", __FUNCTION__, __FILE__, __LINE__, file->filename ); return 0; } /* dont need to seek to the saved position because there will be another seek anyway*/ /* fseeko( file->file, file->pos, SEEK_SET ); */ } else { /* open first time */ file->file= fopen( file->filename, "rb" ); if( NULL == file->file ) { /* show this error every time */ OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "cannot open file %s for reading. Maybe the number of " "opened filehandles exceeds your system's limit\n", __FUNCTION__, __FILE__, __LINE__, file->filename ); return 0; } } /* OTF_fprintf( stderr, "OTF_File_revive() SEEK: register opened file with FileManager\n" ); */ if ( 0 == OTF_FileManager_registerFile( file->manager, file ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_registerFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } } else { /* file already opened */ /* OTF_fprintf( stderr, "OTF_File_revive() READ: update FileManagers LRU list\n" ); */ if ( 0 == OTF_FileManager_touchFile( file->manager, file ) ) { OTF_fprintf( stderr, "ERROR in function %s, file: %s, line: %i:\n " "OTF_FileManager_touchFile() failed.\n", __FUNCTION__, __FILE__, __LINE__ ); return 0; } } return 1; default: /* *** unknown mode *** */ return 0; } }