ObjMapMgr Class Reference

#include <ObjMapMgr.h>

Public Member Functions

 ObjMapMgr ()
 ~ObjMapMgr ()
int Initialize (int Size, unsigned int BlockSize, int MaxReplications, int UseReplications, DiskMgr *DiskMgr, ostream *LogStream)
int Start ()
int Stop ()
int Open (char *Name, RioAccess Access, RioObject **Object)
void CleanUpDelete ()
int GetObjectInfo (char *Name, ObjectInfo *ObjectInfo)
int createobject (char *name)

Static Public Attributes

static const int OBJHDRSIZE = 64

Private Attributes

int m_used
int m_n
unsigned int m_BlockSize
int m_MaxReplications
int m_UseReplications
DiskMgrm_DiskMgr
pthread_mutex_t m_mutexmeta
pthread_mutex_t m_mutexfast
ostream * m_log

Friends

class RioObject

Detailed Description

Definition at line 174 of file ObjMapMgr.h.


Constructor & Destructor Documentation

ObjMapMgr::ObjMapMgr (  ) 

Definition at line 63 of file ObjMapMgr.cpp.

00064 {
00065     m_used            = 0;
00066     m_n               = 0;
00067     m_BlockSize       = 0;
00068     m_MaxReplications = 0;
00069     m_UseReplications = 0;
00070     m_DiskMgr         = NULL;
00071     m_log             = NULL;
00072     pthread_mutex_init( &m_mutexmeta, NULL );
00073     pthread_mutex_init( &m_mutexfast, NULL );
00074 }

ObjMapMgr::~ObjMapMgr (  ) 

Definition at line 77 of file ObjMapMgr.cpp.

00078 {
00079     pthread_mutex_destroy(&m_mutexmeta);
00080     pthread_mutex_destroy(&m_mutexfast);
00081 }


Member Function Documentation

void ObjMapMgr::CleanUpDelete (  ) 
int ObjMapMgr::createobject ( char *  name  ) 

Definition at line 131 of file ObjMapMgr.cpp.

00132 {
00133     int handle, nbytes, rc, error = 0;
00134     ObjectHeader header;
00135 
00136     pthread_mutex_lock(&m_mutexmeta);
00137 
00138     handle = open( name, O_RDWR | O_CREAT | O_EXCL, 0700 );
00139     if( handle == -1 )
00140     {
00141         error = errno;
00142     }
00143 
00144     if( handle == -1 )
00145     {
00146         (*m_log) << "Create object failed " << strerror(errno) << endl;
00147         switch (error)
00148         {
00149             case EEXIST:
00150                 rc = ERROR_OBJECTMANAGER + ERROR_OBJECT_EXISTS;
00151                 goto unlockret;
00152 
00153             case EISDIR:
00154             case ENAMETOOLONG:
00155             case ENOENT:
00156             case ENOTDIR:
00157                 rc = ERROR_OBJECTMANAGER + ERROR_INVALID_OBJECTNAME;
00158                 goto unlockret;
00159 
00160             default:
00161                 rc = ERROR_OBJECTMANAGER + ERROR_OBJECT_OPEN_FAILED;
00162                 goto unlockret;
00163         }
00164     }
00165 
00166     memset(&header, 0, sizeof(header));
00167     memcpy(header.Signature, ObjectSignatureValue, sizeof(header.Signature));
00168     header.Type      = ObjectInfo::FILE_TYPE_DATA;
00169     header.nRep      = m_UseReplications;
00170     header.BlockSize = m_BlockSize;
00171     header.nBlocks   = 0;
00172     header.Size      = 0;
00173 
00174     header.nullmd5 = true;
00175 
00176     nbytes = write( handle, &header, sizeof( header ));
00177     if( nbytes != sizeof( header ) )
00178     {
00179         close( handle );
00180         rc = ERROR_OBJECTMANAGER + ERROR_OBJECT_METADATA_WRITE;
00181         goto unlockret;
00182     }
00183     close( handle );
00184     rc = S_OK;
00185 
00186     unlockret:
00187     pthread_mutex_unlock( &m_mutexmeta );
00188 
00189     return rc;
00190 }

int ObjMapMgr::GetObjectInfo ( char *  Name,
ObjectInfo ObjectInfo 
)

Definition at line 1139 of file ObjMapMgr.cpp.

01140 {
01141     struct stat mystat;
01142 
01143     if( stat( Name, &mystat ) )
01144         return ERROR_OBJECTMANAGER + ERROR_OBJECT_OPEN_FAILED;
01145 
01146     if( S_ISREG( mystat.st_mode ) )
01147     {
01148         ObjectInfo->setType( ObjectInfo::FILE_TYPE_DATA );
01149     }
01150     else if(S_ISDIR(mystat.st_mode))
01151     {
01152         ObjectInfo->setType( ObjectInfo::FILE_TYPE_DIRECTORY );
01153     }
01154     else return ERROR_OBJECTMANAGER + ERROR_UNEXPECTED;
01155 
01156     struct tm LastWrite;
01157     AccessTime LastModificationTime;
01158 
01159     // convert time_t to .. using thread safe localtime_r
01160     localtime_r(&mystat.st_ctime, &LastWrite);
01161     LastModificationTime.Year       = LastWrite.tm_year+1900;
01162     LastModificationTime.Month      = LastWrite.tm_mon+1;
01163     LastModificationTime.DayOfWeek  = LastWrite.tm_wday;
01164     LastModificationTime.Day        = LastWrite.tm_mday;
01165     LastModificationTime.Hour       = LastWrite.tm_hour;
01166     LastModificationTime.Minute     = LastWrite.tm_min;
01167 
01168     ObjectInfo->setTime( LastModificationTime );
01169 
01170     if( ObjectInfo->isDir() )
01171         ObjectInfo->setPermission( 0777 );// octal (allow all for now)
01172     else
01173         ObjectInfo->setPermission( 0666 );
01174 
01175     if( !ObjectInfo->isDir() )
01176     {
01177         int Handle;
01178         pthread_mutex_lock( &m_mutexmeta );
01179 
01180         Handle = open( Name, O_RDWR );
01181         if( Handle == -1 )
01182         {
01183             RioErr << "ObjMapMgr: [Warning] Could not open " << Name
01184                    << " with RDRW permissions. Trying to open it with "
01185                    << "RDONLY permission" << endl;
01186             Handle = open( Name, O_RDONLY );
01187             if( Handle == -1 )
01188             {
01189                 pthread_mutex_unlock( &m_mutexmeta );
01190                 if(errno == ENOENT)
01191                     return ERROR_OBJECTMANAGER + ERROR_INVALID_OBJECTNAME;
01192                 return ERROR_OBJECTMANAGER + ERROR_OBJECT_OPEN_FAILED;
01193             }
01194         }
01195 
01196         ObjectHeader header;
01197         int nbytes;
01198 
01199         // Read object metadata and make sure it is a valid RIO object
01200         nbytes = read( Handle, &header, sizeof( header ));
01201         pthread_mutex_unlock( &m_mutexmeta );
01202 
01203         if( ( nbytes != sizeof( header )) || (header.BlockSize != m_BlockSize) ||
01204            ( memcmp( header.Signature, ObjectSignatureValue,
01205                      sizeof(ObjectSignatureValue))) )
01206         {
01207             close(Handle);
01208             (*m_log) << "ObjMapMgr.GetObjectInfo(): "
01209                      << "Object metadata is corrupted" << endl;
01210             return ERROR_OBJECTMANAGER + ERROR_OBJECT_METADATA_CORRUPTED;
01211         }
01212 
01213         // Close object
01214         close(Handle);
01215 
01216         // Update object size
01217         ObjectInfo->setSize( header.Size );
01218 
01219         // Fill MD5sum field
01220         if( header.nullmd5 )
01221             ObjectInfo->setMd5sum( NULL );
01222         else
01223         {
01224             char hexmd5sum[33];
01225             char hex[17] = "0123456789abcdef";
01226             for( int i = 0; i < 16; ++i ) 
01227             {
01228                 hexmd5sum[2*i]     = hex[ ((unsigned char)header.md5sum[i]) >> 4 ];
01229                 hexmd5sum[2*i + 1] = hex[ ((unsigned char)header.md5sum[i]) & 15 ];
01230             }
01231             hexmd5sum[32] = '\0';
01232             ObjectInfo->setMd5sum( hexmd5sum );
01233         }
01234 
01235         // Preenche o campo VideoRate de ObjectInfo com a informacao obtida do
01236         // objeto (usado pelo controle de fluxo) [um arquivo].
01237         if( header.VideoRate == 0 ) // Metadados antigo -> usamos o valor 
01238                                     // default.
01239             ObjectInfo->setVideoRate( DEFAULTVIDEORATE );                         
01240         else                            
01241             ObjectInfo->setVideoRate( header.VideoRate );
01242        
01243     }
01244     else
01245     {
01246         ObjectInfo->setSize( 0 );
01247 
01248         ObjectInfo->setMd5sum( NULL );
01249 
01250         // Preenche o campo VideoRate de ObjectInfo com a informacao obtida do
01251         // objeto (usado pelo controle de fluxo) [um diretorio].
01252         ObjectInfo->setVideoRate( 0 );
01253     }
01254 
01255     // Fill in User and Group owner with Invalid value for now
01256     ObjectInfo->setOwner( "NoUser" );
01257     ObjectInfo->setGroup( "NoGroup" );
01258     
01259     return S_OK;
01260 }

int ObjMapMgr::Initialize ( int  Size,
unsigned int  BlockSize,
int  MaxReplications,
int  UseReplications,
DiskMgr DiskMgr,
ostream *  LogStream 
)

Definition at line 84 of file ObjMapMgr.cpp.

00089 {
00090     if( sizeof( ObjectHeader ) != OBJHDRSIZE )
00091     {
00092         (*LogStream) << "ObjMapMgr.Initialize sizeof(ObjectHeader) "
00093                      << sizeof(ObjectHeader) << " not " << OBJHDRSIZE << " \n";
00094         return -1;
00095     }
00096 
00097     m_BlockSize = BlockSize;
00098 
00099     // MaxReplications aren't really used yet
00100     // code contains hard coded value of 2 everywhere (DiskMgr.cpp)
00101     // need to add replications used for object to each metadata file
00102     // as it determines number of disk addresses for each logical block
00103     m_MaxReplications = MaxReplications;
00104     m_UseReplications = UseReplications;
00105 
00106     m_DiskMgr = DiskMgr;
00107     m_log     = LogStream;
00108 
00109     // update table size and number of used entries
00110     m_n       = Size;
00111     m_used    = 0;
00112 
00113     return S_OK;
00114 }

int ObjMapMgr::Open ( char *  Name,
RioAccess  Access,
RioObject **  Object 
)

Definition at line 199 of file ObjMapMgr.cpp.

00200 {
00201     int rc, nBytes = 0;
00202     off_t offset = 0;
00203     off_t readl = 0;
00204 
00205     RioObject *op = new RioObject( this );
00206 
00207     char *namep = new char[ strlen( Name ) + 1 ];
00208 
00209     if( ( op == 0 ) || ( namep == 0 ) )
00210     {
00211         delete op;
00212         delete [] namep;
00213         return ERROR_OBJECTMANAGER + ERROR_MEMORY;
00214     }
00215 
00216     strcpy(namep, Name);
00217     op->o_Name = namep;
00218 
00219     pthread_mutex_lock( &m_mutexmeta );
00220 
00221     m_used++;
00222 
00223     rc = open( op->o_Name, O_RDWR );
00224     if( rc == -1 )
00225     {
00226         #ifdef RIO_DEBUG2
00227         RioErr << "ObjMapMgr: [Warning] Could not open " << op->o_Name
00228                << " with RDRW permissions. Trying to open it with RDONLY "
00229                << "permission" << endl;
00230         #endif
00231 
00232         rc = open( op->o_Name, O_RDONLY );
00233         if( rc == -1 ){
00234             int error = errno;
00235             delete op;
00236             pthread_mutex_unlock(&m_mutexmeta);
00237             switch( error )
00238             {
00239                 case EISDIR:
00240                 case EACCES:
00241                 case ENAMETOOLONG:
00242                 case ENOENT:
00243                 case ENOTDIR:
00244                     return ERROR_OBJECTMANAGER + ERROR_INVALID_OBJECTNAME;
00245 
00246                 default:
00247                     return ERROR_OBJECTMANAGER + ERROR_OBJECT_OPEN_FAILED;
00248             }
00249         }
00250     }
00251 
00252     char *info = myInfo();
00253     (*m_log) << info << " Object Manager: opening object " << Name
00254              << ". active objs " << m_used << endl;
00255     free( info );
00256 
00257 
00258     op->o_FileHandle = rc;
00259 
00260     // Read object metadata and make sure it is a valid RIO object
00261     int nbytes = read( op->o_FileHandle, &op->o_Hdr, sizeof( op->o_Hdr ));
00262 
00263     if( nbytes != sizeof( op->o_Hdr ) )
00264     {
00265         rc = 1;
00266         goto err;
00267     }
00268     if( op->o_Hdr.BlockSize != m_BlockSize )
00269     {
00270         rc = 2;
00271         goto err;
00272     }
00273     if( memcmp( op->o_Hdr.Signature, ObjectSignatureValue,
00274                 sizeof( ObjectSignatureValue )))
00275     {
00276         rc = 3;
00277         goto err;
00278     }
00279     op->o_HdrValid = 1;
00280 
00281     offset = lseek( op->o_FileHandle, 0, SEEK_END );
00282     if( offset == (off_t) -1 )
00283     {
00284         rc = 4;
00285         goto err;
00286     }
00287     if( ( unsigned ) offset != ( sizeof( op->o_Hdr )
00288             + op->o_Hdr.nBlocks * op->o_Hdr.nRep * sizeof( RioDiskBlock ) ) )
00289     {
00290         rc = 5;
00291         goto err;
00292     }
00293     if( ( (op->o_Hdr.Size + m_BlockSize - 1) / m_BlockSize ) !=
00294         op->o_Hdr.nBlocks
00295       )
00296     {
00297         rc = 6;
00298         // goto err;
00299 
00300         // copy operation was interrupted before finished
00301         // must check if this change is ok!
00302         RioErr << "ObjMapMgr.Open: metadata corrupt for " << op->o_Name;
00303         RioErr << ".Changing number of blocks from " << op->o_Hdr.nBlocks;
00304 
00305         op->o_Hdr.nBlocks = ( op->o_Hdr.Size + m_BlockSize - 1 ) / m_BlockSize;
00306 
00307         RioErr << " to " << op->o_Hdr.nBlocks  << endl;
00308 
00309         op->SaveObject();
00310         // --------------------------------------------------------------------
00311     }
00312 
00313     op->o_Map = new RioDiskBlock[ op->o_Hdr.nBlocks * op->o_Hdr.nRep ];
00314     if( op->o_Map == 0 )
00315     {
00316         rc = 7;
00317         goto err;
00318     }
00319     op->o_nMapBlocks = op->o_Hdr.nBlocks;
00320 
00321     offset = lseek( op->o_FileHandle, sizeof( op->o_Hdr ), SEEK_SET );
00322     if( offset == (off_t) -1 )
00323     {
00324         rc = 8;
00325         goto err;
00326     }
00327 
00328     readl = op->o_Hdr.nBlocks * op->o_Hdr.nRep * sizeof( RioDiskBlock );
00329     nBytes = read( op->o_FileHandle, op->o_Map, readl );
00330     if( nBytes != readl )
00331     {
00332         rc = 9;
00333         goto err;
00334     }
00335     op->o_MapValid = 1;
00336 
00337     pthread_mutex_lock( &m_mutexfast );
00338     op->o_Locked = 0;
00339     pthread_mutex_unlock( &m_mutexfast );
00340 
00341     // return ptr to RioObject
00342     *Object = op;
00343     
00344     // Imprime a taxa de transferencia
00345     
00346     unsigned int VideoRate, VideoRateHeader;
00347     VideoRateHeader = op->o_Hdr.VideoRate;
00348     
00349     pthread_mutex_unlock( &m_mutexmeta );
00350     
00351     op->GetVideoRate( &VideoRate ); 
00352     
00353     #ifdef RIO_DEBUG2
00354     RioErr << "ObjMapMgr.Open taxa de transferencia do objeto " 
00355            << Name << ": lida = " << VideoRateHeader  
00356            << ", alterada = " << VideoRate << endl;
00357     #endif
00358 
00359     return S_OK;
00360 
00361 err:
00362     RioErr << "ObjMapMgr.Open: metadata corrupt code " << rc
00363            << " for "    << op->o_Name << endl;
00364     RioErr << " offset " << offset << " readl "  << readl
00365            << " nBytes " << nBytes << endl;
00366     op->PrintHdr();
00367     delete op;
00368     pthread_mutex_unlock(&m_mutexmeta);
00369     return ERROR_OBJECTMANAGER + ERROR_OBJECT_METADATA_CORRUPTED;
00370 }

int ObjMapMgr::Start ( void   ) 

Definition at line 117 of file ObjMapMgr.cpp.

00118 {
00119     return S_OK;
00120 }

int ObjMapMgr::Stop ( void   ) 

Definition at line 123 of file ObjMapMgr.cpp.

00124 {
00125     // Can only stop if table is not being used
00126     return S_OK;
00127 }


Friends And Related Function Documentation

friend class RioObject [friend]

Definition at line 194 of file ObjMapMgr.h.


Field Documentation

unsigned int ObjMapMgr::m_BlockSize [private]

Definition at line 179 of file ObjMapMgr.h.

Definition at line 182 of file ObjMapMgr.h.

ostream* ObjMapMgr::m_log [private]

Definition at line 192 of file ObjMapMgr.h.

Definition at line 180 of file ObjMapMgr.h.

pthread_mutex_t ObjMapMgr::m_mutexfast [private]

Definition at line 190 of file ObjMapMgr.h.

pthread_mutex_t ObjMapMgr::m_mutexmeta [private]

Definition at line 186 of file ObjMapMgr.h.

int ObjMapMgr::m_n [private]

Definition at line 178 of file ObjMapMgr.h.

int ObjMapMgr::m_used [private]

Definition at line 177 of file ObjMapMgr.h.

Definition at line 181 of file ObjMapMgr.h.

const int ObjMapMgr::OBJHDRSIZE = 64 [static]

Definition at line 216 of file ObjMapMgr.h.


The documentation for this class was generated from the following files:
Generated on Wed Jul 4 16:03:35 2012 for RIO by  doxygen 1.6.3