Example #1
0
	long File::find(const ByteVector &pattern, long fromOffset, const ByteVector &before)
	{
		if (!d->stream || pattern.size() > bufferSize())
			return -1;

		// The position in the file that the current buffer starts at.

		long bufferOffset = fromOffset;
		ByteVector buffer;

		// These variables are used to keep track of a partial match that happens at
		// the end of a buffer.

		int previousPartialMatch = -1;
		int beforePreviousPartialMatch = -1;

		// Save the location of the current read pointer.  We will restore the
		// position using seek() before all returns.

		long originalPosition = tell();

		// Start the search at the offset.

		seek(fromOffset);

		// This loop is the crux of the find method.  There are three cases that we
		// want to account for:
		//
		// (1) The previously searched buffer contained a partial match of the search
		// pattern and we want to see if the next one starts with the remainder of
		// that pattern.
		//
		// (2) The search pattern is wholly contained within the current buffer.
		//
		// (3) The current buffer ends with a partial match of the pattern.  We will
		// note this for use in the next itteration, where we will check for the rest
		// of the pattern.
		//
		// All three of these are done in two steps.  First we check for the pattern
		// and do things appropriately if a match (or partial match) is found.  We
		// then check for "before".  The order is important because it gives priority
		// to "real" matches.

		for (buffer = readBlock(bufferSize()); buffer.size() > 0; buffer = readBlock(bufferSize())) {

			// (1) previous partial match

			if (previousPartialMatch >= 0 && int(bufferSize()) > previousPartialMatch) {
				const int patternOffset = (bufferSize() - previousPartialMatch);
				if (buffer.containsAt(pattern, 0, patternOffset)) {
					seek(originalPosition);
					return bufferOffset - bufferSize() + previousPartialMatch;
				}
			}

			if (!before.isNull() && beforePreviousPartialMatch >= 0 && int(bufferSize()) > beforePreviousPartialMatch) {
				const int beforeOffset = (bufferSize() - beforePreviousPartialMatch);
				if (buffer.containsAt(before, 0, beforeOffset)) {
					seek(originalPosition);
					return -1;
				}
			}

			// (2) pattern contained in current buffer

			long location = buffer.find(pattern);
			if (location >= 0) {
				seek(originalPosition);
				return bufferOffset + location;
			}

			if (!before.isNull() && buffer.find(before) >= 0) {
				seek(originalPosition);
				return -1;
			}

			// (3) partial match

			previousPartialMatch = buffer.endsWithPartialMatch(pattern);

			if (!before.isNull())
				beforePreviousPartialMatch = buffer.endsWithPartialMatch(before);

			bufferOffset += bufferSize();
		}

		// Since we hit the end of the file, reset the status before continuing.

		clear();

		seek(originalPosition);

		return -1;
	}
Example #2
0
/*
 * logger -- read and log utility
 * Reads from an input and arranges to write the result on the system log.
 */
int main (int argc, char **argv) {
    int ch, logflags, pri;
    char *tag, buf[MAX_LINE];
    char *usock = NULL;
    long timeout_ms = 100;
    long timeout_sec;
    long timeout_usec;
    int indent_mode = 0;
    char *udpserver = NULL;
    int LogSock = -1;
    long tmpport;

    static const struct option longopts[] = {
        { "id",       no_argument,        0, 'i' },
        { "stderr",   no_argument,        0, 's' },
        { "file",     required_argument,  0, 'f' },
        { "priority", required_argument,  0, 'p' },
        { "tag",      required_argument,  0, 't' },
        { "socket",   required_argument,  0, 'u' },
        { "udp",      no_argument,        0, 'd' },
        { "server",   required_argument,  0, 'n' },
        { "port",     required_argument,  0, 'P' },
        { "indent",   required_argument,  0, 'I' },
        { "version",  no_argument,        0, 'V' },
        { "help",     no_argument,        0, 'h' },
        { NULL,       0,                  0, 0   }
    };

    tag = NULL;
    pri = LOG_NOTICE;
    logflags = 0;

    while ((ch = getopt_long(argc, argv, "f:ip:st:u:dI:n:P:Vh", longopts, NULL)) != -1) {
        switch((char) ch) {
            case 'f': /* file to log */
                if (freopen(optarg, "r", stdin) == NULL)
                    err(EXIT_FAILURE, "file %s", optarg);
                break;

            case 'i': /* log process id also */
                logflags |= LOG_PID;
                break;

            case 'p': /* priority */
                pri = pencode(optarg);
                break;

            case 's': /* log to standard error */
                logflags |= LOG_PERROR;
                break;

            case 't': /* tag */
                tag = optarg;
                break;

            case 'u': /* unix socket */
                usock = optarg;
                break;

            case 'd':
                optd = 1; /* use datagrams */
                break;

            case 'n': /* udp socket */
                optd = 1; /* use datagrams because udp */
                udpserver = optarg;
                break;

            case 'P': /* change udp port */
                tmpport = strtol_or_err(optarg, "failed to parse port number");

                if (tmpport < 0 || 65535 < tmpport) {
                    errx(EXIT_FAILURE, "port `%ld' out of range", tmpport);
                }

                udpport = (int) tmpport;
                break;

            case 'V':
                printf("%s %s\n", PROGRAM_NAME, PROGRAM_VERSION);
                exit(EXIT_SUCCESS);

            case 'I':
                indent_mode = 1;
                timeout_ms = strtol_or_err(optarg, "failed to parse timeout number");

                if (timeout_ms < 1) {
                    errx(EXIT_FAILURE, "Invalid value for timeout %li", timeout_ms);
                }

                break;

            case 'h':
                usage(stdout);

            case '?':
            default:
                usage(stderr);
        }
    }

    argc -= optind;
    argv += optind;

    /* setup for logging */
    if (!usock && !udpserver) {
        openlog(tag ? tag : getlogin(), logflags, 0);
    } else if (udpserver) {
        LogSock = udpopenlog(udpserver, udpport);
    } else {
        LogSock = myopenlog(usock);
    }

    (void) fclose(stdout);

    /* log input line if appropriate */
    if (argc > 0) {
        char *p, *endp;
        size_t len;

        for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
            len = strlen(*argv);
            if (p + len > endp && p > buf) {
                if (!usock && !udpserver) {
                    syslog(pri, "%s", buf);
                } else {
                    mysyslog(LogSock, logflags, pri, tag, buf);
                }

                p = buf;
            }

            if (len > sizeof(buf) - 1) {
                if (!usock && !udpserver) {
                    syslog(pri, "%s", *argv++);
                } else {
                    mysyslog(LogSock, logflags, pri, tag, *argv++);
                }

            } else {
                if (p != buf) {
                    *p++ = ' ';
                }

                memmove(p, *argv++, len);
                *(p += len) = '\0';
            }
        }

        if (p != buf) {
            if (!usock && !udpserver) {
                syslog(pri, "%s", buf);
            } else {
                mysyslog(LogSock, logflags, pri, tag, buf);
            }
        }

    } else if (indent_mode) {
        int len;

        timeout_sec = timeout_ms / 1000;
        timeout_usec = (timeout_ms % 1000) * 1000;

        while ((len = readBlock(buf, MAX_LINE, timeout_sec, timeout_usec)) != EOF) {
            //fprintf(stderr, "Got buf %i\n", len);
            if (len > 0 && buf[len - 1] == '\n') {
                buf[len - 1] = '\0';
            }

            if (!usock && !udpserver) {
                syslog(pri, "%s", buf);
            } else {
                mysyslog(LogSock, logflags, pri, tag, buf);
            }
        }
    } else {
        while (fgets(buf, sizeof(buf), stdin) != NULL) {
            /* glibc is buggy and adds an additional newline, so we have to remove it here until glibc is fixed */
            int len = strlen(buf);

            if (len > 0 && buf[len - 1] == '\n') {
                buf[len - 1] = '\0';
            }

            if (!usock && !udpserver) {
                syslog(pri, "%s", buf);
            } else {
                mysyslog(LogSock, logflags, pri, tag, buf);
            }
        }
    }

    if (!usock && !udpserver) {
        closelog();
    } else {
        close(LogSock);
    }

    return EXIT_SUCCESS;
}
bool LoggerContestLog::GJVload( void )
{
   std::string temp;
   logCount = 0;

   // load the LoggerContestLog details from file_desc
   clearBuffer();
   readBlock( 0 );
   buffpt = 0;
   buftostr( temp );
   if ( atoi( temp.c_str() ) != 0 )
   {
      MinosParameters::getMinosParameters() ->mshowMessage( "Invalid block 0 in LoggerContestLog file" );
      return false;
   }

   buftostr( temp );
   if ( strnicmp( temp, GJVVERSION, VERLENGTH ) != 0 )
   {
      MinosParameters::getMinosParameters() ->mshowMessage( String( "Invalid LoggerContestLog file format (" ) + temp.c_str() + ", " + GJVVERSION + " expected)" );
      return false;
   }

   buftostr( band );
   buftostr( name );
   buftostr( temp );
   mycall = callsign( strupr( temp ) );
   buftostr( myloc.loc );
   buftostr( location );

   otherExchange.setValue( inyn() );
   /*bool CC_mult =*/
   inyn();
   /*
   if ( CC_mult )
   {
      Country_mult = true;
      County_mult = true;
      //      District_mult = true;
   }
   */
   locMult.setValue( inyn() );

   buftostr( temp );
   logCount = atoi( temp.c_str() );

   buftostr( power );

   buftostr( temp );
   //   buftostr( mainOpNow );

   buftostr( temp );
   //   buftostr( secondOpNow );

   buftostr( mode );
   inyn();
   scoreMode.setValue( PPKM );			// don't take any notice of radial flag!

   setPostEntry( inyn() );
   if ( inyn() )
   {
      scoreMode.setValue( PPQSO );
   }
   countryMult.setValue( inyn() );
   if ( inyn() )
   {
      //      County_mult = true;
      //      District_mult = true;
   }
   districtMult.setValue( inyn() );

   powerWatts.setValue( inyn() );

   if ( inyn() )
   {
      //      scoreMode = GSPECIAL;
   }

   allowLoc8.setValue( inyn() );
   RSTField.setValue( !inyn() );
   serialField.setValue( !inyn() );
   locatorField.setValue( !inyn() );

   QTHField.setValue( !inyn() );

   return true;

}
Example #4
0
void MergeJoin (char *infile1, char *infile2, unsigned char field, block_t *buffer, unsigned int nmem_blocks, char *outfile, unsigned int *nres, unsigned int *nios){
    //memSize of buffer is -2 cause 2 last blocks is used for, one reading block from big file and two writing to output
    int memSize = nmem_blocks - 2;
    //get sizes of files
    int infile1Size = getSize(infile1);
    int infile2Size = getSize(infile2);

    unsigned int noneed1=0, noneed2=0, ios=0;
    *nres = 0;
    *nios = 0;

    FILE *out = fopen(outfile, "ab");

    char outfile1[] = "outfile1.bin";
    char outfile2[] = "outfile2.bin";



    MergeSort(infile1, 1, buffer, nmem_blocks, outfile1, &noneed1, &noneed2, &ios);
    (*nios) += ios;

    MergeSort(infile2, 1, buffer, nmem_blocks, outfile2, &noneed1, &noneed2, &ios);
    (*nios) += ios;

    //if file1 is bigger switch files cause next we assume that file1 is the small one.
    if(infile1Size > infile2Size){
        char temp0 = outfile1[7];
        outfile1[7] = outfile2[7];
        outfile2[7] = temp0;
        int temp = infile1Size;
        infile1Size = infile2Size;
        infile2Size = temp;
    }
    FILE *input1 = fopen(outfile1, "rb");
    FILE *input2 = fopen(outfile2, "rb");
    ////printfile(outfile1);
    ////printfile(outfile2);

    block_t *bigFileBlock = buffer + memSize;
    block_t *outputBlock = buffer + memSize + 1;
    (*outputBlock).blockid = 0;
    //offset that changes every time we need new block from big file to check
    int bigFileBlockOffset=0;
    //counts the records of buffer that has same value of a record of bigFileBlock
    int countSameBufferEntries=0;
    //number of blocks in big file. Useful to end the main loop.
    int blocks = infile2Size - 1;
    //printf("%d", blocks);
    //this will represent the id of the first block of the buffer
    int firstBlockId = 0;
    int lastBlockId = memSize-1;
    //at first we read first blocks of small and big file so we have something to compare in first loop
    (*nios) += readBuffer(buffer, input1, 0, memSize);
    (*nios) += readBlock(bigFileBlock, input2, 0);
    recordPos bufferRecPos = getRecordPos(0);
    record_t tempBufferRec;
    int tempBufferBlock=0;
    record_t bigFileBlockRec;
    while(blocks>0){
        /*
        General:
            if(bufferRecPos.block%memSize==firstBlockId%memSize)
                this condition checks if we have reached i circle in the buffer.

            firstBlockId%memSize
                defines the first block of the buffer and makes it easy to replace it with another block if necessary with:
                    buffer + firstBlockId%memSize



        */

        for(int blockEntrie=0; blockEntrie<MAX_RECORDS_PER_BLOCK; blockEntrie++){

            bigFileBlockRec = (*bigFileBlock).entries[blockEntrie];

            if(compareRecords(tempBufferRec, bigFileBlockRec, field)==0){
                //Here we  have to go back to the block and record of tempBufferRec.
                for(int i=0; i<countSameBufferEntries; i++){
                    decr(bufferRecPos);
                    ////printf("%d\n", bufferRecPos.block);
                }
                int i=0;//keeps i for load2
                int load, load2;
                if(countSameBufferEntries/memSize > memSize){
                    load = memSize;
                    load2=0;
                }else{
                    load = countSameBufferEntries/memSize;
                    load2 = countSameBufferEntries%memSize;
                }
                for( ;i<load; i++){
                    (*nios) += readBlock(buffer + (tempBufferBlock + i) % memSize, input1, tempBufferBlock + i);
                }
                if(load2!=0){//we have to read one more block.
                    (*nios) += readBlock(buffer + (tempBufferBlock + i) % memSize, input1, tempBufferBlock + i);
                }
                //return to firstBlockID and lastBlockID their previous values
                firstBlockId = tempBufferBlock ;
                lastBlockId = firstBlockId + memSize - 1;
            }

            while(compareRecords(getRecord(buffer, bufferRecPos), bigFileBlockRec, field) < 0){
                //Here we have to pass the records in the small file that are smaller than the bigFileRec
                incr(bufferRecPos);
                if (bufferRecPos.record == 0) {
                     if(bufferRecPos.block%memSize==firstBlockId%memSize){
                        if (lastBlockId < infile1Size - 1) {
                            (*nios) += readBlock(buffer + firstBlockId%memSize, input1, lastBlockId + 1);
                            firstBlockId += 1;
                            lastBlockId += 1;
                        }else{
                            blocks=0;//No point to continue merging as all next records are greater than the last of buffer.
                            break;
                        }
                    }
                }
            }

            if(compareRecords(getRecord(buffer, bufferRecPos), bigFileBlockRec, field) > 0){
                    continue;//...
            }


            tempBufferRec = getRecord(buffer, bufferRecPos);
            tempBufferBlock = bufferRecPos.block;
            countSameBufferEntries=0;

            while(compareRecords(getRecord(buffer, bufferRecPos), bigFileBlockRec, field)==0){
                //Here we add in output the merges.
                (*outputBlock).entries[(*outputBlock).nreserved++] = bigFileBlockRec;
                (*outputBlock).entries[(*outputBlock).nreserved++] = getRecord(buffer, bufferRecPos);
                (*nres)++;
                if ((*outputBlock).nreserved == MAX_RECORDS_PER_BLOCK) {
                    (*nios) += writeBlock(out, outputBlock);
                    emptyBlock(outputBlock);
                    (*outputBlock).blockid += 1;
                }

                countSameBufferEntries++;
                incr(bufferRecPos);

                if(bufferRecPos.record==0){
                    if(bufferRecPos.block%memSize==firstBlockId%memSize){
                        if (lastBlockId < infile1Size - 1) {
                            (*nios) += readBlock(buffer + firstBlockId%memSize, input1, lastBlockId + 1);
                            firstBlockId++;
                            lastBlockId++;
                        }else {//take always the same value because it is the last value to compare with last big file's blocks
                            if (bufferRecPos.block == 0) {
                                bufferRecPos.block = memSize - 1;
                            } else {
                                bufferRecPos.block -= 1;
                            }
                            bufferRecPos.record = MAX_RECORDS_PER_BLOCK - 1;
                            break;
                        }
                    }
                }
                ////printf("fsdfds");
            }


        }
        //records in bigFileBlock are over and we read the next one.
        bigFileBlockOffset++;
        blocks--;
        (*nios) += readBlock(bigFileBlock, input2, bigFileBlockOffset);
    }








}
QgsRasterBlock * QgsRasterDataProvider::block( int theBandNo, QgsRectangle  const & theExtent, int theWidth, int theHeight )
{
  QgsDebugMsg( QString( "theBandNo = %1 theWidth = %2 theHeight = %3" ).arg( theBandNo ).arg( theWidth ).arg( theHeight ) );
  QgsDebugMsg( QString( "theExtent = %1" ).arg( theExtent.toString() ) );

  QgsRasterBlock *block;
  if ( srcHasNoDataValue( theBandNo ) && useSrcNoDataValue( theBandNo ) )
  {
    block = new QgsRasterBlock( dataType( theBandNo ), theWidth, theHeight, srcNoDataValue( theBandNo ) );
  }
  else
  {
    block = new QgsRasterBlock( dataType( theBandNo ), theWidth, theHeight );
  }

  if ( block->isEmpty() )
  {
    QgsDebugMsg( "Couldn't create raster block" );
    return block;
  }

  // Read necessary extent only
  QgsRectangle tmpExtent = extent().intersect( &theExtent );

  if ( tmpExtent.isEmpty() )
  {
    QgsDebugMsg( "Extent outside provider extent" );
    block->setIsNoData();
    return block;
  }

  double xRes = theExtent.width() / theWidth;
  double yRes = theExtent.height() / theHeight;
  double tmpXRes, tmpYRes;
  double providerXRes = 0;
  double providerYRes = 0;
  if ( capabilities() & Size )
  {
    providerXRes = extent().width() / xSize();
    providerYRes = extent().height() / ySize();
    tmpXRes = qMax( providerXRes, xRes );
    tmpYRes = qMax( providerYRes, yRes );
    if ( qgsDoubleNear( tmpXRes, xRes ) ) tmpXRes = xRes;
    if ( qgsDoubleNear( tmpYRes, yRes ) ) tmpYRes = yRes;
  }
  else
  {
    tmpXRes = xRes;
    tmpYRes = yRes;
  }

  if ( tmpExtent != theExtent ||
       tmpXRes > xRes || tmpYRes > yRes )
  {
    // Read smaller extent or lower resolution

    if ( !extent().contains( theExtent ) )
    {
      QRect subRect = QgsRasterBlock::subRect( theExtent, theWidth, theHeight, extent() );
      block->setIsNoDataExcept( subRect );
    }

    // Calculate row/col limits (before tmpExtent is aligned)
    int fromRow = qRound(( theExtent.yMaximum() - tmpExtent.yMaximum() ) / yRes );
    int toRow = qRound(( theExtent.yMaximum() - tmpExtent.yMinimum() ) / yRes ) - 1;
    int fromCol = qRound(( tmpExtent.xMinimum() - theExtent.xMinimum() ) / xRes );
    int toCol = qRound(( tmpExtent.xMaximum() - theExtent.xMinimum() ) / xRes ) - 1;

    QgsDebugMsg( QString( "fromRow = %1 toRow = %2 fromCol = %3 toCol = %4" ).arg( fromRow ).arg( toRow ).arg( fromCol ).arg( toCol ) );

    if ( fromRow < 0 || fromRow >= theHeight || toRow < 0 || toRow >= theHeight ||
         fromCol < 0 || fromCol >= theWidth || toCol < 0 || toCol >= theWidth )
    {
      // Should not happen
      QgsDebugMsg( "Row or column limits out of range" );
      return block;
    }

    // If lower source resolution is used, the extent must beS aligned to original
    // resolution to avoid possible shift due to resampling
    if ( tmpXRes > xRes )
    {
      int col = floor(( tmpExtent.xMinimum() - extent().xMinimum() ) / providerXRes );
      tmpExtent.setXMinimum( extent().xMinimum() + col * providerXRes );
      col = ceil(( tmpExtent.xMaximum() - extent().xMinimum() ) / providerXRes );
      tmpExtent.setXMaximum( extent().xMinimum() + col * providerXRes );
    }
    if ( tmpYRes > yRes )
    {
      int row = floor(( extent().yMaximum() - tmpExtent.yMaximum() ) / providerYRes );
      tmpExtent.setYMaximum( extent().yMaximum() - row * providerYRes );
      row = ceil(( extent().yMaximum() - tmpExtent.yMinimum() ) / providerYRes );
      tmpExtent.setYMinimum( extent().yMaximum() - row * providerYRes );
    }
    int tmpWidth = qRound( tmpExtent.width() / tmpXRes );
    int tmpHeight = qRound( tmpExtent.height() / tmpYRes );
    tmpXRes = tmpExtent.width() / tmpWidth;
    tmpYRes = tmpExtent.height() / tmpHeight;

    QgsDebugMsg( QString( "Reading smaller block tmpWidth = %1 theHeight = %2" ).arg( tmpWidth ).arg( tmpHeight ) );
    QgsDebugMsg( QString( "tmpExtent = %1" ).arg( tmpExtent.toString() ) );

    QgsRasterBlock *tmpBlock;
    if ( srcHasNoDataValue( theBandNo ) && useSrcNoDataValue( theBandNo ) )
    {
      tmpBlock = new QgsRasterBlock( dataType( theBandNo ), tmpWidth, tmpHeight, srcNoDataValue( theBandNo ) );
    }
    else
    {
      tmpBlock = new QgsRasterBlock( dataType( theBandNo ), tmpWidth, tmpHeight );
    }

    readBlock( theBandNo, tmpExtent, tmpWidth, tmpHeight, tmpBlock->bits() );

    int pixelSize = dataTypeSize( theBandNo );

    double xMin = theExtent.xMinimum();
    double yMax = theExtent.yMaximum();
    double tmpXMin = tmpExtent.xMinimum();
    double tmpYMax = tmpExtent.yMaximum();

    for ( int row = fromRow; row <= toRow; row++ )
    {
      double y = yMax - ( row + 0.5 ) * yRes;
      int tmpRow = floor(( tmpYMax - y ) / tmpYRes );

      for ( int col = fromCol; col <= toCol; col++ )
      {
        double x = xMin + ( col + 0.5 ) * xRes;
        int tmpCol = floor(( x - tmpXMin ) / tmpXRes );

        if ( tmpRow < 0 || tmpRow >= tmpHeight || tmpCol < 0 || tmpCol >= tmpWidth )
        {
          QgsDebugMsg( "Source row or column limits out of range" );
          block->setIsNoData(); // so that the problem becomes obvious and fixed
          delete tmpBlock;
          return block;
        }

        qgssize tmpIndex = ( qgssize )tmpRow * ( qgssize )tmpWidth + tmpCol;
        qgssize index = row * ( qgssize )theWidth + col;

        char *tmpBits = tmpBlock->bits( tmpIndex );
        char *bits = block->bits( index );
        if ( !tmpBits )
        {
          QgsDebugMsg( QString( "Cannot get input block data tmpRow = %1 tmpCol = %2 tmpIndex = %3." ).arg( tmpRow ).arg( tmpCol ).arg( tmpIndex ) );
          continue;
        }
        if ( !bits )
        {
          QgsDebugMsg( "Cannot set output block data." );
          continue;
        }
        memcpy( bits, tmpBits, pixelSize );
      }
    }

    delete tmpBlock;
  }
  else
  {
    readBlock( theBandNo, theExtent, theWidth, theHeight, block->bits() );
  }

  // apply scale and offset
  block->applyScaleOffset( bandScale( theBandNo ), bandOffset( theBandNo ) );
  // apply user no data values
  block->applyNoDataValues( userNoDataValues( theBandNo ) );
  return block;
}
Example #6
0
int tfs_deleteFile(fileDescriptor FD)
{
	int i, fd, size, firstBlock, numBlocks;
	int found = 0;
	char buff[BLOCKSIZE];
	char *fileName;
	drt_t *temp = dynamicResourceTable;
	
	if(disk_mount)
		fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;
	

	while(temp){
		if(temp->id == FD){
			break;
		}
		temp = temp->next;
	}
	
	if (!temp)
		return ERR_BADFILE;

	fileName = temp->fileName;

	/* looking for inode block */
	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE && !found; i++){
		if(readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;
		if(buff[0] == 2){
			if(!strcmp(fileName, buff+4)){
				if (buff[3] == 0)
					return ERR_READONLY;
				found = 1;
				firstBlock = buff[2];
				size = (buff[13] << 8) & 0xFF00;
				size = size | (buff[14] & 0x00FF); 
				numBlocks = size;
				break;
			}
		}
	}
	if(!found)
		return ERR_NOINODEFOUND;

	buff[0] = 4;

	/*deleting inode */
	writeBlock(fd, i,buff);

        if(DEBUG)
            printf("Deleted inode block at index %d\n",i);
	/*deleting file extents*/
	for(i = firstBlock; i < firstBlock + numBlocks; i++) {
		writeBlock(fd, i, buff);    
	}

        if(DEBUG)
            printf("Deleted file extents from index %d to %d\n",firstBlock, firstBlock+numBlocks);
	removeDrtNode(FD);
	close(fd);

	return 1;
}
/* Name: pinPage
 * Expected arguments: BM_BufferPool *const bm, BM_PageHandle *const page, covnst PageNumber pageNum
 * Behavior: Would perform the applicable page replacement strategy on the buffer pool and pin the page from
 * 			 file at the appropriate position.
 * Version: 1.0.0
 * Algorithm:   1) Check if the page is already present in buffer
 * 				2) Add a replcementWeight to each page.
 * 				2) Get the appropriate replacement page by invoking the corresponding strategy
 * 				2) Replace that page with the new page.
 * 				3) The newly added page should have the maximum weight. */
RC pinPage(BM_BufferPool * const bm, BM_PageHandle * const page,
		const PageNumber pageNum) {
	int replacementFrameNum, totalPages, i;
	int statusFlag;
	ReplacementStrategy strategy = bm->strategy;
	Buffer_page_Dtl *lowestPossiblePage;
	totalPages = bm->numPages;
	BufferPool_Node *bf_node;
//	pthread_mutex_lock(&work_mutex_pin);//Hold the mutex lock for thread in pin block.
	bf_node = search_data(BP_StNode_ptr, bm);
	Buffer_page_Dtl *pg_dtl;
	pg_dtl = bf_node->buffer_page_dtl;
	RC writeVal=1, readVal=1;
	int emptyFlag = 1;
	if (pg_dtl != NULL) {

		/*This for loop checks if the page is already present in the buffer*/
		for (i = 0; i < totalPages; i++) {
			/*In order to make sure that all the elements of the array are empty, I am not breaking the loop*/
			if ((pg_dtl + i)->pagenums > -1) { //I assume that pagenums will be a mandatory field for any pg_dtl.
				emptyFlag = 0;
				/*To check if the page is already present in buffer.*/
				if ((pg_dtl + i)->pagenums == pageNum) {
                    (pg_dtl + i)->timeStamp=univtime++;//timestamp used in LRU
                    page->pageNum = pageNum;
					page->data = (pg_dtl + i)->pageframes;
					(pg_dtl + i)->fixcounts+=1;
//					pthread_mutex_unlock(&work_mutex_pin);//Unlock the mutex lock for thread in pin block.
					return RC_OK; // If there is a hit, return OK. There is no need to pin it since it is already present.
				}

			}
		} //end of 1st for loop

		/*This loop gets the first lowestPossible page and assigns it.*/
		for (i = 0; i < totalPages; i++) {
			if ((pg_dtl + i)->pagenums == -1){
				lowestPossiblePage = ((pg_dtl + i)); //After the loop, lowestPossiblePage would be last page for which pg_dtl was empty
				emptyFlag = 1;
				break;
			}
		}//end of 2nd for loop

	} else { /*This also means that the page detail has not been initialized at all since the array itself is null. This block might never be reached*/
		lowestPossiblePage = (pg_dtl + 0); // I assign the first frame itself as the lowestPossiblePage which is fit to be replaced.
		lowestPossiblePage->replacementWeight =
				lowestPossiblePage->replacementWeight + 1;

		(pg_dtl + i)->timeStamp=univtime++;
		emptyFlag = 1;
	}

	/*Even if there is one empty frame, emptyFlag would be 1. And we could make use of that page.*/
	if (emptyFlag == 1) {
		page->pageNum = pageNum;
		page->data = lowestPossiblePage->pageframes;
		/*If you find a free frame, just write the contents to that frame*/
		writeVal = RC_OK;
		/*This if condition is not mandatory. Infact this might never become true*/
		if (lowestPossiblePage->isdirty == TRUE) {
			writeVal = writeBlock(pageNum, bm->mgmtData,
					lowestPossiblePage->pageframes);
					bf_node->numwriteIO++;
		}
		if(readBlock(pageNum, bm->mgmtData,lowestPossiblePage->pageframes)==RC_READ_NON_EXISTING_PAGE)
				{
				    readVal=appendEmptyBlock(bm->mgmtData);//If page requested in pin not available in the file , do append it.
				}
				else
                    readVal=RC_OK;
        bf_node->numreadIO++;
		lowestPossiblePage->fixcounts += 1;
		lowestPossiblePage->pagenums = pageNum;

	} else { /*If no frame is free, call the appropriate page replacement algorithm to do the job*/
		if (strategy == RS_FIFO) {
			statusFlag = applyFIFO(bm, page, pageNum);
		} else if (strategy == RS_LRU) {
			statusFlag = applyLRU(bm, page, pageNum);
		} else if (strategy == RS_CLOCK) {
//			statusFlag = applyCLOCK(bm, page, pageNum);
		} else if (strategy == RS_LRU_K) {
//			statusFlag = applyLRU_k(bm, page, pageNum);
		} else {
			return RC_WRITE_FAILED; // should probably return "Invalid strategy"
		}
	} //end of outer else block

	/*Status flag indicates the success of the page replacement algorithm when no frame is free
	 * writeVal indicates the success of writelock call when a frame is dirty*/
//   	 pthread_mutex_unlock(&work_mutex_pin);
	if (statusFlag == 1 || (writeVal == RC_OK && readVal == RC_OK)) {
		return RC_OK;
	} else {
		return RC_WRITE_FAILED;
	}
}
Example #8
0
// read up to size bytes from the file.  Stops reading if the end of file is reached.
// returns the actual number of bytes read, or -1 on error
int readFile(fileHandleType fh, char *buffer, unsigned int size, int relPosition, unsigned int *numRead, securityIdType securityID) {

int i;
int retval;
unsigned int leftToRead;
unsigned int bytesRead;
unsigned int readPos;
unsigned int blockIndexToRead;
unsigned int blockNumForRead;
void *blockToRead;
unsigned int *catalogBlock;
unsigned int dirEntry;
unsigned int readOffset;
unsigned int amountToRead;
unsigned int blockSize;
unsigned int remainingInFile;
unsigned int remainingInBlock;
int offset;
int retcode;

	*numRead = 0;

	if (fh > MAX_OPEN_FILES) {

		return ERROR_BAD_HANDLE;

	}

	if (fileCursors[fh].inUse == 0) {

		return ERROR_BAD_HANDLE;

	}

	if (buffer == 0) {

		return ERROR_BAD_VALUE;

	}

	if (size == 0) {

		return ERROR_BAD_VALUE;

	}

	if ( (securityID != fileCursors[fh].securityID) && securityID != ROOT_ID && (fileCursors[fh].othersPermissions&0x02) == 0) {

		return ERROR_NO_PERMISSION;
	}

	// treat a memory file specially 
	if (fileCursors[fh].fileType > 2 ) {

		retcode=readMemoryFile( fh, buffer, size, numRead );

		return retcode;

	}

	leftToRead = size;
	bytesRead = 0;

	if (relPosition != 0) {

		offset = relPosition;
	
		if (fileReadPosRelative(fh, offset) != 0 ) {

			return ERROR_BAD_VALUE;
	
		}		

	}

	// read the catalog of blocks for this file
	dirEntry = fileCursors[fh].dirEntryNum;
	
	readPos = fileCursors[fh].readPos;

	// get a local of the blocksize just to keep code more readable
	blockSize = masterBlocks->mblock0.blockSize;

	while(leftToRead) {

		// use the directory entry, not the fileCursor so that we get updated fileSize of writes occur.
		if (readPos == rootDir->entry[dirEntry].fileSize) {

			break;

		}

		// find what the relative (not the absolute) block number is
		// the later will be looked up in the block catalog for this file
		blockIndexToRead = readPos / blockSize;

		// read the catalog for this file
		// re-read it every time because it could also get written to, extending the EOF
		if (readBlock(rootDir->entry[dirEntry].firstCatalogBlock, (void **)&catalogBlock)!= 0) {

			return ERROR_READ_ERROR;

		}

		blockNumForRead = *(unsigned int *)(catalogBlock + blockIndexToRead);

		deallocate(catalogBlock, blockSize);

		if (readBlock(blockNumForRead, &blockToRead)!= 0) {

			return ERROR_READ_ERROR;

		}

		// now there are three cases on how much memory to copy--the smallest of:
		// 1) the remaining data in this block from the calculated offset
		// 2) the remaining data in this read request
		// 3) until the end of file is reached

		readOffset = readPos % blockSize;

		remainingInBlock = blockSize - readOffset;
		remainingInFile = rootDir->entry[dirEntry].fileSize - readPos;

		amountToRead = minimum( minimum(remainingInFile, remainingInBlock), leftToRead);

		memcpy(buffer+bytesRead, blockToRead+readOffset, amountToRead);

		// we are done with this block so release the memory
		deallocate((void *)blockToRead, blockSize);

		bytesRead += amountToRead;
		leftToRead -= amountToRead;
		readPos += amountToRead;


	} // while

	fileCursors[fh].readPos = readPos;
	*numRead = bytesRead;

	if (readPos == rootDir->entry[dirEntry].fileSize) {

		return ERROR_EOF;

	}
	else {

		return NO_ERROR;
	}
}
Example #9
0
int deleteFile( fileHandleType fh, securityIdType securityID ) {

unsigned int dirEntry;
unsigned int *catalogBlock;
unsigned int *entryPtr;
int retval;

struct memoryFileInfo { 
	unsigned int address;
	unsigned int size;
	char accessType;

	} *memoryInfo;

	if (fh > MAX_OPEN_FILES) {

		return ERROR_BAD_HANDLE;
	}

	if (fileCursors[fh].inUse == 0) {

		return ERROR_BAD_HANDLE;
	}

	if ( (securityID != fileCursors[fh].securityID) && securityID != ROOT_ID && (fileCursors[fh].othersPermissions&0x01) == 0) {

		return ERROR_NO_PERMISSION;

	}

	// read the catalog of blocks for this file
	dirEntry = fileCursors[fh].dirEntryNum;
	
	// read the catalog for this file
	retval = readBlock(rootDir->entry[dirEntry].firstCatalogBlock, (void **)&catalogBlock);

	if (retval != 0) {

		return ERROR_READ_ERROR;

	}

	if ( fileCursors[fh].fileType > 2 ) {

		memoryInfo = (struct memoryFileInfo *)catalogBlock;

		if (memoryInfo->address > 0 ) {

			free((void *)memoryInfo->address);

		}

	}
	else {
	entryPtr = catalogBlock;

		while(*entryPtr != 0) {

			eraseBlock(*entryPtr);
			setBlockAsFree(*entryPtr);
			++entryPtr;
		}
	}

	deallocate((void *)catalogBlock, masterBlocks->mblock0.blockSize);

	eraseBlock(rootDir->entry[dirEntry].firstCatalogBlock);

	setBlockAsFree(rootDir->entry[dirEntry].firstCatalogBlock);

	rootDir->entry[dirEntry].name[0] = 0;
	rootDir->entry[dirEntry].fileSize = 0;
	rootDir->entry[dirEntry].firstCatalogBlock = 0;
	rootDir->numEntries--;

	fileCursors[fh].dirEntryNum = 0;
	fileCursors[fh].inUse = 0;
	fileCursors[fh].writePos = 0;
	fileCursors[fh].readPos = 0;
	fileCursors[fh].fileSize = 0;
	fileCursors[fh].othersPermissions = 0;

	return NO_ERROR;
}
Example #10
0
int makeMemoryFile(char *name, unsigned int address, unsigned int length, char accessType, securityIdType securityID ) {

int i;
unsigned int catalogBlock;
struct { 
	unsigned int address;
	unsigned int size;
	char accessType;

	} *memoryFileInfo;

	// if no filename give, return error
	if ( name == 0 ) {

		return ERROR_BAD_VALUE;

	}

	// if the root directory is invalid, return error
	if ( rootDir == 0 ) {

		return ERROR_INIT;

	}

	// if the file already exists, return error
	if ( statusFile(name, 0) == 0 ) {

		return ERROR_FILE_EXISTS;

	}


	// find the first empty directory entry
	for ( i = 0; i < rootDir->maxEntries; ++i ) {

		if ( rootDir->entry[i].name[0] == 0) {

			strncpy(rootDir->entry[i].name, name, MAX_FILENAME_LEN);
			rootDir->entry[i].fileSize = length;
			rootDir->entry[i].fileType = 0x3 + accessType;
			rootDir->entry[i].securityID = securityID;
			rootDir->entry[i].othersPermissions = 0;

			// now allocate a block for its first catalog block

			if (findFreeBlock(&catalogBlock) == 0 ) {

				eraseBlock(catalogBlock);

				setBlockInUse(catalogBlock);

				rootDir->entry[i].firstCatalogBlock = catalogBlock;

			}
			else {

				return ERROR_NO_BLOCKS;
			
			}

			rootDir->numEntries++;

			readBlock(catalogBlock, (void **)&memoryFileInfo);

			memoryFileInfo->address = address;
			memoryFileInfo->size = length;
			memoryFileInfo->accessType = accessType;

			writeBlock(memoryFileInfo, catalogBlock);

			return NO_ERROR;

		} // if strcmp

	}  // for

	return ERROR_MAX_EXCEEDED;

}
Example #11
0
int writeFile(fileHandleType fh, char *buffer, unsigned int size, securityIdType securityID) {

unsigned int leftToWrite;
unsigned int writePtr;
unsigned int blockOffset;
unsigned int writePos;
unsigned char *blockBuffer;
unsigned int blockSize;
void *blockForWrite;
unsigned int blockNumForWrite;
unsigned int remainingBlockSpace;
unsigned int dirEntry;
unsigned int *catalogBlock;
unsigned int blockIndexForWrite;
int retval;
int writeLength;
char tmpbuffer[32];
int retcode;

	if (fh > MAX_OPEN_FILES) {

		return ERROR_BAD_HANDLE;

	}

	if (fileCursors[fh].inUse == 0) {

		return ERROR_BAD_HANDLE;

	}

	if (buffer == 0) {

		return ERROR_BAD_VALUE;

	}

	if (size == 0) {

		return ERROR_BAD_VALUE;

	}

	if ( (securityID != fileCursors[fh].securityID) && securityID != ROOT_ID && (fileCursors[fh].othersPermissions&0x01) == 0) {

		return ERROR_NO_PERMISSION;
	}


	// treat a memory file specially 
	if (fileCursors[fh].fileType > 2 ) {

		retcode=writeMemoryFile( fh, buffer, size );

		return retcode;

	}


	leftToWrite = size;
	writePos = fileCursors[fh].writePos;

	// get a local of the blocksize just to keep code more readable
	blockSize = masterBlocks->mblock0.blockSize;

	while(leftToWrite > 0) {

		blockOffset = writePos % blockSize;

		// if blockOffset is 0 then a new block for writing needs to be allocated, unless we 
		// aren't at the end of the file

		if (blockOffset == 0 && writePos == fileCursors[fh].fileSize) {

			if (addNewBlock(fh) == -1) {

				return ERROR_NO_BLOCKS;

			}

		}

		// read block where writing will happen and go from there
		dirEntry = fileCursors[fh].dirEntryNum;

		// read the catalog for this file
		if (readBlock(rootDir->entry[dirEntry].firstCatalogBlock, (void **)&catalogBlock) != 0) {

			return ERROR_READ_ERROR;

		}

		blockNumForWrite = *(unsigned int *)(catalogBlock + (writePos / blockSize));

		deallocate(catalogBlock, blockSize);

		writeLength = minimum(blockSize - blockOffset, leftToWrite);

		if (readBlock(blockNumForWrite, &blockForWrite)!=0) {

			return ERROR_READ_ERROR;

		}

		memcpy(blockForWrite+blockOffset, buffer, writeLength);

		writeBlock(blockForWrite, blockNumForWrite);

		// free the memory for the buffer
		deallocate(blockForWrite, blockSize);

		leftToWrite -= writeLength;

		fileCursors[fh].fileSize += writeLength - (fileCursors[fh].fileSize - writePos);

		writePos += writeLength;

	}

	fileCursors[fh].writePos = writePos;

	return NO_ERROR;

}
Example #12
0
unsigned int addNewBlock(fileHandleType fh) {

unsigned int newBlockNum;
void *catalogBlock;
unsigned int dirEntry;
int i;
int retval;
	
	if (fh > MAX_OPEN_FILES) {

		return ERROR_BAD_HANDLE;

	}

	if (fileCursors[fh].inUse == 0) {

		return ERROR_BAD_HANDLE;

	}

	dirEntry = fileCursors[fh].dirEntryNum;

	// read the catalog for this file
	retval = readBlock(rootDir->entry[dirEntry].firstCatalogBlock, &catalogBlock);

	if (retval != 0) {

		return ERROR_READ_ERROR;

	}

	for (i=0; i < masterBlocks->mblock0.blockEntriesPerCatalog; ++i) {

		if (*((unsigned int *)(catalogBlock)+i) == 0)
			break;
	}

	if (i == masterBlocks->mblock0.blockEntriesPerCatalog) {

		return -1;

	}

	if (findFreeBlock(&newBlockNum) != NO_ERROR) {

		return ERROR_NO_BLOCKS;

	}

	setBlockInUse(newBlockNum);

	// add this new block to the catalog for this file
	*((unsigned int *)catalogBlock+i) = newBlockNum;

	// and write the block back 
	writeBlock(catalogBlock, rootDir->entry[dirEntry].firstCatalogBlock);

	// release the memory allocated by readBlock
	deallocate(catalogBlock, masterBlocks->mblock0.blockSize);

	return newBlockNum;

}
Example #13
0
	/*
		Reads a single wstring line
	*/
	std::wstring readWTextLine(int length) { std::wstring ret; ret.resize(length*2);  readBlock(&ret[0], length*2); return ret; };
Example #14
0
	long File::rfind(const ByteVector &pattern, long fromOffset, const ByteVector &before)
	{
		if (!d->stream || pattern.size() > bufferSize())
			return -1;

		// The position in the file that the current buffer starts at.

		ByteVector buffer;

		// These variables are used to keep track of a partial match that happens at
		// the end of a buffer.

		/*
		int previousPartialMatch = -1;
		int beforePreviousPartialMatch = -1;
		*/

		// Save the location of the current read pointer.  We will restore the
		// position using seek() before all returns.

		long originalPosition = tell();

		// Start the search at the offset.

		long bufferOffset;
		if (fromOffset == 0) {
			seek(-1 * int(bufferSize()), End);
			bufferOffset = tell();
		}
		else {
			seek(fromOffset + -1 * int(bufferSize()), Beginning);
			bufferOffset = tell();
		}

		// See the notes in find() for an explanation of this algorithm.

		for (buffer = readBlock(bufferSize()); buffer.size() > 0; buffer = readBlock(bufferSize())) {

			// TODO: (1) previous partial match

			// (2) pattern contained in current buffer

			long location = buffer.rfind(pattern);
			if (location >= 0) {
				seek(originalPosition);
				return bufferOffset + location;
			}

			if (!before.isNull() && buffer.find(before) >= 0) {
				seek(originalPosition);
				return -1;
			}

			// TODO: (3) partial match

			bufferOffset -= bufferSize();
			seek(bufferOffset);
		}

		// Since we hit the end of the file, reset the status before continuing.

		clear();

		seek(originalPosition);

		return -1;
	}
Example #15
0
fileDescriptor tfs_openFile(char *name) {
	int fd = ERR_BADFILE;
	char buff[BLOCKSIZE];
	int i = 0;
	int found = 0;

	/* check if we have a mounted disk */
	if(disk_mount)
		if (checkIfAlreadyOpen(name) >= 0)
			return checkIfAlreadyOpen(name);
		else
			fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;

	/* Loop over inode blocks to see if file already exists */
	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE && !found; i++){
		if(readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;
		if(buff[0] == 2){
			if(!strcmp(name, buff+4)){
				found = 1;
				break;
			}
		}
	}

	time_t now = time(0);

	if (!found) {
		/* find first free location to place an inode block */
		for (i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE; i++) {
			if (readBlock(fd, i, buff) < 0)
				return ERR_NOMORESPACE;
			if (buff[0] == 4)
				break;
		}
		/* buff is now a template of the inode block
		 * i is the block number of the soon to be inode
		 * label it as an inode
		 * opy name to front end
		 */
		buff[0] = 2; // set as inode block
		buff[3] = 1; // set as RW by default
		memcpy(buff+4, name, strlen(name));

		memcpy(buff+15, &now, sizeof(time_t)); /* set creation time */
		memcpy(buff+19, &now, sizeof(time_t)); /* set access time */
		memcpy(buff+23, &now, sizeof(time_t)); /* set modification time */
                writeBlock(fd, i, buff);

		if(DEBUG)
			printf("Created inode block with filename %s\n",buff+4);
	} else {
		memcpy(buff+19, &now, sizeof(time_t)); // set access time
	}

	/* add a new entry in drt that refers to this filename
	 *     returns a fileDescriptor (temp->id) */
	drt_t *temp;
	if (dynamicResourceTable == NULL) {
		temp = calloc(1, sizeof(drt_t));
		temp->fileName = name;
		temp->access = 1;
		dynamicResourceTable = temp;

		if(DEBUG)
			printf("Created first dynamic resource table node with filename %s\n",temp->fileName);

	} else {
		temp = addDrtNode(name, dynamicResourceTable, buff[3]);
	}


	close(fd);

	return temp->id;
}
Example #16
0
int truncateFile( fileHandleType fh, securityIdType securityID ) {

unsigned int dirEntry;
unsigned int *catalogBlock;
unsigned int *entryPtr;
int retval;

	if (fh > MAX_OPEN_FILES) {

		return ERROR_BAD_HANDLE;
	}

	if (fileCursors[fh].inUse == 0) {

		return ERROR_BAD_HANDLE;
	}

	// if its a memory file, just return
	if (fileCursors[fh].fileType > 2 ) {

		return NO_ERROR;

	}

	if ( (securityID != fileCursors[fh].securityID) && securityID != ROOT_ID && (fileCursors[fh].othersPermissions&0x01) == 0) {

		return ERROR_NO_PERMISSION;

	}

	// read the catalog of blocks for this file
	dirEntry = fileCursors[fh].dirEntryNum;
	
	// read the catalog for this file
	retval = readBlock(rootDir->entry[dirEntry].firstCatalogBlock, (void **)&catalogBlock);

	if (retval != 0) {

		return ERROR_READ_ERROR;

	}

	entryPtr = catalogBlock;

	while(*entryPtr != 0) {

		eraseBlock(*entryPtr);

		setBlockAsFree(*entryPtr);

		++entryPtr;
	}

	deallocate((void *)catalogBlock, masterBlocks->mblock0.blockSize);

	eraseBlock(rootDir->entry[dirEntry].firstCatalogBlock);

	rootDir->entry[dirEntry].fileSize = 0;

	fileCursors[fh].writePos = 0;
	fileCursors[fh].readPos = 0;
	fileCursors[fh].readBlockNum = 0;
	fileCursors[fh].writeBlockNum = 0;
	fileCursors[fh].fileSize = 0;

	return NO_ERROR;
}
Example #17
0
int tfs_writeFile(fileDescriptor FD, char *buffer, int size)
{
	int i,startIndex, endIndex, j, fd, numBlocks, found = 1, inode;
	char buff[BLOCKSIZE];
	char *fileName;
	drt_t *temp = dynamicResourceTable;

	if(disk_mount)
		fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;

	numBlocks = ceil((double)size / (double)BLOCKSIZE);

	while(temp){
		if(temp->id == FD){
		    break;
		}
		temp = temp->next;
	}
	if (!temp)
		return ERR_BADFILE;

	if(!temp->access)
		return ERR_READONLY;
	
        fileName = temp->fileName;

	found = 0;
	/* find inode */
	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE && !found; i++){
		if(readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;
		if(buff[0] == 2){
			if(!strcmp(fileName, buff+4)){
				found = 1;
				inode = i;
                                break;
			}
		}
	}
	if(!found)
		return ERR_NOINODEFOUND;
	found = 1;

	/* find first free "numBlocks" block occurences to write to */
	for (i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE; i++) {
		if (readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;
		if (buff[0] == 4){
			for(j = i; j < i+numBlocks-1; j++){
				if (readBlock(fd, j, buff) < 0)
					return ERR_NOMORESPACE;  
				if (buff[0] != 4)
					found = 0;
			}
			if(found)
				break;
		}
	}
	if(!found)
		return ERR_NOFREEBLOCKFOUND;

	startIndex = i;
	endIndex = j;
	found = 0;

	/* setting template to make (write) file extents*/
	buff[0] = 3;
	buff[1] = 0x45;
	buff[3] = (char)inode;

	for(i = startIndex; i <= endIndex; i++){
		if(i != endIndex)
			buff[2] = i+1;
		else
			buff[2] = 0;

		strncpy(buff+4,buffer,BLOCKSIZE-4);
		writeBlock(fd, i, buff); 
	}

        if(DEBUG)
            printf("Write to free blocks starting at index %d and ending at %d. Wrote to the file extent -> %s\n",startIndex, endIndex,buff+4);

	/* inode update (1st block occurrence and size) */
	readBlock(fd, inode, buff);
	buff[2] = startIndex;
	buff[13] = (numBlocks & 0xFF00)>>8;
	buff[14] = numBlocks & 0x00FF;
	time_t now = time(0);
	memcpy(buff+19, &now, sizeof(time_t)); // set access time
	memcpy(buff+23, &now, sizeof(time_t)); // set modification time
	writeBlock(fd, inode, buff);
	
        if(DEBUG)
            printf("Updated inode block %d to point to index %d and have size %d %d\n",inode, buff[2],buff[13],buff[14]);
        
        close(fd);

	return 1;
}
Example #18
0
int main()
{
	FILE* fp;
	size_t i = 0;
	size_t offset;
	fp = fopen("src.ldb","r");
	if(fp == NULL){
		printf("error!\n");
		return 1;
	}
	unsigned char a[] = "src.ldb";
	
	unsigned char b[] = "1-959-250-4279";
	//"1-114-560-9305";
	
	Slice filename;
	Slice lastKey;/* 用于存储最后一次读取到的key */
	Slice srckey; /* 存储要查找、删除、修改、添加的key */
	DBhandle dbhandle;
	
	Footer footer;
	Block dataIndexBlock;
	Block* blockArray;
	BlockEntry blockEntry;
	
	initSlice(&filename,10);
	initSlice(&lastKey,20);
	initBlockEntry(&blockEntry);
	initDBhandle(&dbhandle);
	setSlice(&dbhandle.key_,b,22);

	
	setSlice(&filename,a,strlen((char*)a));
	sequentialFile* psFile = (sequentialFile*)malloc(sizeof(sequentialFile));
	setSequentialFile(psFile,fp,&filename);
	
	readFooter(psFile,&footer);
	
	showFooter(&footer);
	
	/* 读取data index block,存储在dataBlock中 */
	readBlock(psFile,&dataIndexBlock,footer.dataIndexHandle);
	
	blockArray = (Block*)malloc(sizeof(Block)*dataIndexBlock.restartNum);
	if(blockArray == NULL){
		printf("error:blockArray is NULL.\n");
		return 1;
	}
	readAllBlock(psFile,blockArray,&dataIndexBlock);

	/*设置操作为Read*/
	dbhandle.type = 1;	
	dbcmd(blockArray,&dataIndexBlock,&dbhandle);
	
	/* 
	 * 至此,所有的block都已经读取到内存中,data block的内容存储在blockarray中,
	 * data block的索引信息存储在dataIndexBlock中 
	 */
	
	/*
	 * 
	 */
	 //showBlokRestart(&dataIndexBlock);
	 
	
	for(i = 0;i < dataIndexBlock.restartNum;i++){
		//showBlockData(&(blockArray[i]));
	}
	
	
	freeBlockEntry(&blockEntry);
	freeDBhandle(&dbhandle);
	fclose(fp); 
	printf("Hello World!\n");

	return 0;
}
Example #19
0
int tfs_rename(fileDescriptor FD, char *name){
	int i, fd,inodeLoc;
	int found = 0;
	char buff[BLOCKSIZE];
	char *fileName;
	drt_t *temp = dynamicResourceTable;

	if(disk_mount)
		fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;

	while(temp){
		if(temp->id == FD){
			break;
		}
		temp = temp->next;
	}

	if (temp == NULL)
		return ERR_BADFILE;

	fileName = temp->fileName;

	/* change name in inode block */

	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE && !found; i++){
		if(readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;
		if(buff[0] == 2){
			if(!strcmp(fileName, buff+4)){
				found = 1;
				break;
			}
		}
	}
	inodeLoc = i;
	if(!found)
		return ERR_NOINODEFOUND;

	/* looking for inode block */
	if(DEBUG)
		printf("Inode block name changes from %s to ",buff+4);
	strcpy(buff+4,name);
	time_t now = time(0);
	memcpy(buff+19, &now, sizeof(time_t)); // set access time
	memcpy(buff+23, &now, sizeof(time_t)); // set modification time
	writeBlock(fd,inodeLoc,buff);
	if(DEBUG)
		printf("%s\n",buff+4);

	/* change name in drt */
	if(DEBUG)
		printf("Drt name changed from %s to ",temp->fileName);
	temp->fileName = name;
	if(DEBUG)
		printf("%s\n",temp->fileName);

	close(fd);
	return 1;
}
void testAppendEnsureCapMetaData()
{
	SM_FileHandle fh;
	SM_PageHandle ph;
	ph = (SM_PageHandle) malloc(PAGE_SIZE);

	TEST_CHECK(createPageFile (TESTPF));
	TEST_CHECK(openPageFile (TESTPF, &fh));

	//Append an empty block to the file.
	appendEmptyBlock(&fh);
	//Check whether the appended block has only 4096 '\0' in the currentBlock.
	readBlock(getBlockPos(&fh),&fh,ph);
	int i;
	for (i=0; i < PAGE_SIZE; i++)
		ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page");
	printf("Appended Block was empty\n");

	//Page File should contain only 2 blocks.first block during createPage and second during appendBlock
	ASSERT_TRUE((fh.totalNumPages == 2), "Number of Blocks : 2");

	 //Current Block postion should be 1
	ASSERT_TRUE((fh.curPagePos == 1), "Current Page Position is 1");

	//add 3 more blocks to the Page File.
	ensureCapacity(5,&fh);

	//Verify whether the freshly added 3 blocks are of '\0' characters
	//[START]
	readBlock(2,&fh,ph);
	for (i=0; i < PAGE_SIZE; i++)
		ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page");

	readBlock(3,&fh,ph);
	for (i=0; i < PAGE_SIZE; i++)
		ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page");

	readBlock(4,&fh,ph);
	for (i=0; i < PAGE_SIZE; i++)
		ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page");
	printf("Freshly appended 3 blocks are empty\n");
	//[END]

	//Page File should contain only 5 blocks, as we have called ensureCapacity(5)
	ASSERT_TRUE((fh.totalNumPages == 5), "Number of Blocks : 5");

	//Current Block postion should be 4
	ASSERT_TRUE((fh.curPagePos == 4), "Current Page Position is 4");

	//Store the metaData into the file and close the pagefile.
	int totalNoOfPages = fh.totalNumPages;
	char fileName[100];
	memset(fileName,'\0',100);
	strcpy(fileName,fh.fileName);
	char metaDataFromFile[100];
	memset(metaDataFromFile,'\0',100);
	closePageFile(&fh);

	//Verify whether the written  MetaData is correct or not
	//[START]
	char metaDataToBeVerified[100];
	memset(metaDataToBeVerified,'\0',100);
	char returnData[100];
	memset(returnData,'\0',100);
	metaDataToBeVerified[0]= 'P';metaDataToBeVerified[1]= 'S';metaDataToBeVerified[2]= ':';metaDataToBeVerified[3]= '\0';
	getString(PAGE_SIZE,returnData);
	strcat(metaDataToBeVerified,returnData);
	strcat(metaDataToBeVerified,";");
	memset(returnData,'\0',100);
	strcat(metaDataToBeVerified,"NP:");
	getString(totalNoOfPages,returnData);
	strcat(metaDataToBeVerified,returnData);
	strcat(metaDataToBeVerified,";");
	readMetaDataFromFile(fileName,metaDataFromFile);
	ASSERT_TRUE((strcmp(metaDataToBeVerified, metaDataFromFile) == 0), "MetaData read from file is correct");
	printf("Read Meta Data from file is :: %s\n",metaDataToBeVerified);
	//[END]
	TEST_CHECK(destroyPageFile(TESTPF));
	free(ph);
	TEST_DONE();
}
Example #21
0
void MergeFile::copyFormTo( MergeFile& out, dr_section sect,
                            uint_32& off, uint_32 form, uint_8 addrSize )
//-----------------------------------------------------------------------
{
    uint_32 num;
    char *  buffer;
    uint_32 bufLen;
    uint_8  buf8[ 8 ];

    switch( form ) {
            /* do all simple numeric forms */
    case DW_FORM_addr:
    case DW_FORM_flag:
    case DW_FORM_data1:
    case DW_FORM_ref1:
    case DW_FORM_data2:
    case DW_FORM_ref2:
    case DW_FORM_data4:
    case DW_FORM_ref4:
    case DW_FORM_sdata:
    case DW_FORM_udata:
    case DW_FORM_ref_udata:
    case DW_FORM_ref_addr:
        num = readForm( sect, off, form, addrSize );
        out.writeForm( form, num, addrSize );
        break;
    case DW_FORM_block1:
        bufLen = readByte( sect, off );
        out.writeByte( (uint_8) bufLen );
        if( bufLen ) {
            buffer = new char[ bufLen ];
            readBlock( sect, off, buffer, bufLen );
            out.writeBlock( buffer, bufLen );
            delete [] buffer;
        }
        break;
    case DW_FORM_block2:
        bufLen = readWord( sect, off );
        out.writeWord( (uint_16) bufLen );
        if( bufLen ) {
            buffer = new char[ bufLen ];
            readBlock( sect, off, buffer, bufLen );
            out.writeBlock( buffer, bufLen );
            delete [] buffer;
        }
        break;
    case DW_FORM_block4:
        bufLen = readDWord( sect, off );
        out.writeDWord( bufLen );
        if( bufLen ) {
            buffer = new char[ bufLen ];
            readBlock( sect, off, buffer, bufLen );
            out.writeBlock( buffer, bufLen );
            delete [] buffer;
        }
        break;
    case DW_FORM_block:
        bufLen = readULEB128( sect, off );
        if( bufLen ) {
            buffer = new char[ bufLen ];
            readBlock( sect, off, buffer, bufLen );
            out.writeBlock( buffer, bufLen );
            delete [] buffer;
        }
        break;
    case DW_FORM_data8:
        readBlock( sect, off, buf8, 8 );
        out.writeBlock( buf8, 8 );
        break;
    case DW_FORM_string:
        do {
            num = readByte( sect, off );
            out.writeByte( (uint_8) num );
        } while( num != 0 );
        break;
    case DW_FORM_indirect:
        num = readULEB128( sect, off );
        out.writeULEB128( num );
        copyFormTo( out, sect, off, num, addrSize );
        break;
    default:
        #if DEBUG
        printf( "ACK -- form %#x\n", form );
        #endif
        InternalAssert( 0 );
    }
}
Example #22
0
void modeltest::runP2(wxCommandEvent& event)
{
	string toinclude1 = "\nBEGIN PAUP;\nexecute \'";
	string toinclude2 = "\';\nEnd;";
	string toinclude3 = "\nBEGIN PAUP;\nquit warntsave=no;\nEnd;";
	wxArrayString output, errors;
	wxString directory;
	string temp;

	wxFileDialog* openFileDialog = new wxFileDialog ( this, "Select NEXUS file to test in PAUP", "", "", FILETYPES3, wxOPEN, wxDefaultPosition);
	if (openFileDialog->ShowModal() == wxID_OK)
	{
		readBlock();
		ofstream paupfile("paupfile.txt");
		wxString fileP = openFileDialog->GetPath();
		directory = openFileDialog->GetDirectory();
		string to = toinclude1;
		string to2 = "log file=\'";
		to2 += fileP;
		to2 += ".log\' replace;\n";
		to += + fileP;
		to += toinclude2;
		mrbl.insert(7, to);
		mrbl.insert(7+to.length()-4, to2);
		mrbl.insert(mrbl.length()-1, toinclude3);

		paupfile << mrbl;
		paupfile.close();

#ifdef __WXMSW__	
		ofstream pbat("p.bat");
		paupP.Prepend("\"");
		paupP.Append("\"");
		temp = paupP;
		pbat << paupP << " paupfile.txt";
		pbat.close();
#endif
		this->outputText->Clear();
		this->outputText->WriteText("PAUP is running on a separate process, please be patient.\n");
		this->outputText->WriteText("When it finishes this display will be updated.\n");
	//"C:\wxWidgets\projects\modelGUI\modeltest3.7\Modeltest3.7 folder\bin\Modeltest3.7.win.exe" < "C:\wxWidgets\projects\modelGUI\model.scores"
		
#ifdef __WXMSW__
		long exec = wxExecute("p.bat", wxEXEC_SYNC);
#else
		//long exec = wxExecute("ls", wxEXEC_SYNC);
		long exec = wxExecute("paup  paupfile.txt", wxEXEC_SYNC);
#endif
		remove("paupfile.txt");
		mrbl.clear();
		this->outputText->Clear();
		this->outputText->WriteText("PAUP run has finished.\nYou can save the scores file and automatically run MrModelTest.");
		wxMessageDialog dialog( NULL, _T("Your scores file is ready.\nDo you want to run MrModelTest? Click Yes to start the analysis\nor No to perform other actions (do not forget to save your scores file)."),_T("Run MrModelTest??"), wxYES_DEFAULT|wxYES_NO|wxICON_QUESTION);
		if(dialog.ShowModal() == wxID_YES)
		{
			fileSelectedMr =  paupDir + "mrmodel.scores";
			//fileSelected = fileP + "model.scores";
			modeltest::runTestMr(event);
		}
		else
		{
			fileToSave = fileP + ".scores";
		}
		this->saveScoresMr->Enable(true);
	}
}
Example #23
0
/*
 *  Raw file                                          DCT output
 *   -----                                              -----
 *  |     |  read+chop                         write   |     |
 *  |     |    ---->   DCT ---->  Quantization  ---->  |     |
 *  |     |                                            |     |
 *   -----                                              -----
 *  DCT output                                           IDCT result
 *   -----                                                  -----
 *  |     |  read+chop                             write   |     |
 *  |     |    ---->  Dequantization  ---->  IDCT   ---->  |     |
 *  |     |                                                |     |
 *   -----                                                  -----
 */
void test_release2(){
  CEXCEPTION_T error;
  Stream *inStream = NULL;
  Stream *outStream = NULL;
  float inputMatrix[8][8];
  int size = 8, count = 0;
  
  Try{
    inStream = openStream("test/Data/Water lilies.7z.010", "rb");
    outStream = openStream("test/Data/Water lilies_RE2.7z.010", "wb");
  }Catch(error){
    TEST_ASSERT_EQUAL(ERR_END_OF_FILE, error);
  }
  Try{
  while(1){
    readBlock(inStream, size, inputMatrix);
    //printf("%.3f", inputMatrix[0][0]);
    normalizeMatrix(size, inputMatrix);
    // dumpMatrix(size, inputMatrix);
    twoD_DCT(size, inputMatrix);
    // printf("%.3f", inputMatrix[0][0]);
    // dumpMatrix(size, inputMatrix);
    quantizationFunction50(size, inputMatrix);
    // dumpMatrix(size, inputMatrix);
    writeBlock11Bit(outStream, size, inputMatrix);
    
    if( feof(inStream->file) )
      { 
          break ;
      }
  }
  }Catch(error){
  TEST_ASSERT_EQUAL(ERR_END_OF_FILE, error);
  }
  closeStream(inStream);
  closeStream(outStream);
  // printf("\n");
  Stream *inStream2 = NULL;
  Stream *outStream2 = NULL;
  count = 0;
  short int inputMatrixIDCT[8][8];
  
  Try{
    inStream2 = openStream("test/Data/Water lilies_RE2.7z.010", "rb");
    outStream2 = openStream("test/Data/Water lilies_RE2Out.7z.010", "wb");
  }Catch(error){
    TEST_ASSERT_EQUAL(ERR_END_OF_FILE, error);
  }
  Try{
  while(1){
    readBlock11Bit(inStream2, size, inputMatrixIDCT);
    // dumpMatrixInt(size, inputMatrixIDCT);
    convertToFloat(inputMatrixIDCT, inputMatrix);
    dequantizationFunction50(size, inputMatrix);
    //dumpMatrix(size, inputMatrix);
    twoD_IDCT(size, inputMatrix);
    denormalizeMatrix(size, inputMatrix);
    // dumpMatrix(size, inputMatrix);
    
    writeBlock(outStream2, size, inputMatrix);
    
   if( feof(inStream2->file) )
      { 
          break ;
      }
  }
  }Catch(error){
  TEST_ASSERT_EQUAL(ERR_END_OF_FILE, error);
  }
  closeStream(inStream2);
  closeStream(outStream2);
}
/************************************************************
 *                    FIFO Strategy                         *
 ************************************************************/
RC pinPage_FIFO (BM_BufferPool *const bm, BM_PageHandle *const page,const PageNumber pageNum){
    BufferPageNode *temp;
    temp=g_queue->front;
    int find =0;    //set a flag to test if the given page is located in the buffer
    if(temp->PageNum==(int)pageNum&&!isEmpty(g_queue))
        find=1;
    for (int i = 1 ;i<bm->numPages&&find==0;i++)
    {
        temp=temp->next;
        if(temp->PageNum==pageNum)
            find=1;
    }
    if(find==1)
    {
        temp->fixCount++;
        page->data=temp->data;
        return RC_OK;
    }
    BufferPageNode *newNode=(BufferPageNode *) malloc (sizeof(BufferPageNode));
    page->pageNum=pageNum;
    int count=bm->numPages;
    BufferPageNode* tempPtr=g_queue->front;
    BufferPageNode* tempPtr_pre=g_queue->front;
    newNode->PageNum=pageNum;
    newNode->dirty=0;
    newNode->fixCount=1;
    newNode->next=NULL;
    
    // remove one page from the buffer (considering the dirty page and the page which is occupied)

    while(tempPtr->fixCount>=1)                          //if some one is occupying the page, move to the next
        {                                                   //until find the free page.
            if(count <=0)
                return RC_NO_FREE_BUFFER_ERROR;             //if all the buff is occupied, return the error
            tempPtr_pre=tempPtr;
            tempPtr=tempPtr->next;
            count--;
        }
    if(tempPtr==g_queue->front)                         //if the first page in the buff is free(fixCount==0)
            g_queue->front=tempPtr->next;                   // set the front pointer of queue to the second page
    else
           tempPtr_pre->next=tempPtr->next;                 //else let the previous page's next pointer points to
    newNode->data=tempPtr->data;
                                                            // the next one(skip the page between them)
    if(tempPtr==g_queue->rear)
            g_queue->rear=tempPtr_pre;
       
    if(tempPtr->dirty==1)                               // if the page is dirty, write it to the file
        {
            writeBlock(tempPtr->PageNum,fh, tempPtr->data);
            countWrite++;
        }
    newNode->frameNum=tempPtr->frameNum;
    free(tempPtr);                                      // free the node.
    g_queue->queueSize= g_queue->queueSize-1;           //decrease the size of queue by one
 
    
    // add the new page to the buffer(the rear of the queue)
    readBlock(pageNum, fh, newNode->data);
    page->data=newNode->data;
    
    countRead++;
    g_queue->rear->next=newNode;
    g_queue->rear=newNode;
    bufferInfo[newNode->frameNum]=newNode;
    g_queue->queueSize= g_queue->queueSize+1;           //increase the size of queue by one


    return RC_OK;
}
Example #25
0
int _tmain(int argc, NLchar **argv)
#endif
{
    NLbyte      buffer[NL_MAX_STRING_LENGTH];
    NLint       count = 0;
    NLbyte      b = (NLbyte)99;
    NLushort    s = 0x1122;
    NLulong     l = 0x11223344;
    NLfloat     f = 12.3141592651f;
    NLdouble    d = 123.12345678901234;
    NLchar      string[NL_MAX_STRING_LENGTH] = TEXT("Hello");
    NLbyte      block[] = {9,8,7,6,5,4,3,2,1,0};

    if(nlInit() == NL_FALSE)
        printErrorExit();

    nlEnable(NL_BIG_ENDIAN_DATA);
    _tprintf(TEXT("nl_big_endian_data = %d\n"), nlGetBoolean(NL_BIG_ENDIAN_DATA));
    _tprintf(TEXT("nlGetString(NL_VERSION) = %s\n\n"), nlGetString(NL_VERSION));
    _tprintf(TEXT("nlGetString(NL_NETWORK_TYPES) = %s\n\n"), nlGetString(NL_NETWORK_TYPES));

    if(nlSelectNetwork(NL_IP) == NL_FALSE)
        printErrorExit();

    _tprintf(TEXT("Short number: %#x, "), s);
    s = nlSwaps(s);
    _tprintf(TEXT("swapped: %#x, "), s);
    s = nlSwaps(s);
    _tprintf(TEXT("swapped back: %#x\n"), s);

    _tprintf(TEXT("Long number: %#lx, "), l);
    l = nlSwapl(l);
    _tprintf(TEXT("swapped: %#lx, "), l);
    l = nlSwapl(l);
    _tprintf(TEXT("swapped back: %#lx\n"), l);

    _tprintf(TEXT("Float number: %.10f, "), f);
    f = nlSwapf(f);
    _tprintf(TEXT("swapped: %.10f, "), f);
    f = nlSwapf(f);
    _tprintf(TEXT("swapped back: %.10f\n"), f);

    _tprintf(TEXT("Double number: %.14f, "), d);
    d = nlSwapd(d);
    _tprintf(TEXT("swapped: %.24f, "), d);
    d = nlSwapd(d);
    _tprintf(TEXT("swapped back: %.14f\n"), d);
    _tprintf(TEXT("\n"));

    _tprintf(TEXT("write byte %d to buffer\n"), b);
    _tprintf(TEXT("write short %#x to buffer\n"), s);
    _tprintf(TEXT("write long %#lx to buffer\n"), l);
    _tprintf(TEXT("write float %f to buffer\n"), f);
    _tprintf(TEXT("write double %.14f to buffer\n"), d);
    _tprintf(TEXT("write string %s to buffer\n"), string);
    _tprintf(TEXT("write block %d%d%d%d%d%d%d%d%d%d to buffer\n"), block[0]
            , block[1], block[2], block[3], block[4], block[5], block[6]
            , block[7], block[8], block[9]);
    _tprintf(TEXT("\n"));
    writeByte(buffer, count, b);
    writeShort(buffer, count, s);
    writeLong(buffer, count, l);
    writeFloat(buffer, count, f);
    writeDouble(buffer, count, d);
    writeString(buffer, count, string);
    writeBlock(buffer, count, block, 10);

    /* reset count to zero to read from start of buffer */
    count = 0;

    readByte(buffer, count, b);
    readShort(buffer, count, s);
    readLong(buffer, count, l);
    readFloat(buffer, count, f);
    readDouble(buffer, count, d);
    readString(buffer, count, string);
    readBlock(buffer, count, block, 10);
    _tprintf(TEXT("read byte %d from buffer\n"), b);
    _tprintf(TEXT("read short %#x from buffer\n"), s);
    _tprintf(TEXT("read long %#lx from buffer\n"), l);
    _tprintf(TEXT("read float %f from buffer\n"), f);
    _tprintf(TEXT("read double %.14f from buffer\n"), d);
    _tprintf(TEXT("read string %s from buffer\n"), string);
    _tprintf(TEXT("read block %d%d%d%d%d%d%d%d%d%d from buffer\n"), block[0]
            , block[1], block[2], block[3], block[4], block[5], block[6]
            , block[7], block[8], block[9]);

    nlShutdown();
    return 0;
}
Example #26
0
int main(void)
{
    extern char TESTDRIVER[];
    
    //LED_OFF();
    //DIRA |= LED_MASK;

    cache_line_mask = cacheStart(TESTDRIVER, &cache_mbox, cache, CACHE_CONFIG1, CACHE_CONFIG2, CACHE_CONFIG3, CACHE_CONFIG4);
    
    printf("mbox  %08x\n", (uint32_t)&cache_mbox);
    printf("cache %08x\n", (uint32_t)cache);
    printf("buf   %08x\n", (uint32_t)buf);
    
#ifdef FLASH_TEST

{
    int start, i, j;
    
    printf("Flash test\n");
    
    srand(CNT);
    start = rand();
    printf("Starting with %08x\n", start);
    for (i = 0, j = start; i < BUF_SIZE; ++i)
        buf[i] = j++;
    eraseFlashBlock(0);
    writeFlashBlock(0, buf, sizeof(buf));
    memset(buf, 0, sizeof(buf));
    readBlock(ROM_BASE, buf, sizeof(buf));
    for (i = 0, j = start; i < BUF_SIZE; ++i) {
        if (buf[i] != j++)
            printf("%08x: expected %08x, found %08x\n", i, j - 1, buf[i]);
    }

    printf("done\n");
}

#endif

#ifdef BIG_FLASH_TEST

{
    uint32_t addr, startValue, value;
    int i, j;
    
    printf("Big flash test\n");

    addr = ROM_BASE;
    srand(CNT);
    startValue = value = addr + rand();
    printf("Start value %08x\n", startValue);
    printf("Filling flash\n");
    for (j = 0; j < BUF_COUNT; ++j) {
        uint32_t startAddr = addr;
        for (i = 0; i < BUF_SIZE; ++i, addr += sizeof(uint32_t))
            buf[i] = value++;
        if ((startAddr & BLOCK_MASK_4K) == 0) {
            //printf("Erasing %08x\n", ROM_BASE + startAddr);
            eraseFlashBlock(startAddr - ROM_BASE);
        }
        //printf("Writing %08x\n", ROM_BASE + startAddr);
        writeFlashBlock(startAddr - ROM_BASE, buf, sizeof(buf));
    }
    
    printf("Checking flash\n");
    addr = ROM_BASE;
    value = startValue;
    for (j = 0; j < BUF_COUNT; ++j) {
        //printf("Reading %08x\n", addr);
        readBlock(addr, buf, sizeof(buf));
        for (i = 0; i < BUF_SIZE; ++i, addr += sizeof(uint32_t))
            if (buf[i] != value++)
                printf("%08x: expected %08x, found %08x\n", addr, value - 1, buf[i]);
    }

    addr = ROM_BASE;
    value = startValue;
    printf("Filling flash inverted\n");
    for (j = 0; j < BUF_COUNT; ++j) {
        uint32_t startAddr = addr;
        for (i = 0; i < BUF_SIZE; ++i, addr += sizeof(uint32_t))
            buf[i] = ~value++;
        if ((startAddr & BLOCK_MASK_4K) == 0) {
            //printf("Erasing %08x\n", ROM_BASE + startAddr);
            eraseFlashBlock(startAddr - ROM_BASE);
        }
        //printf("Writing %08x\n", ROM_BASE + startAddr);
        writeFlashBlock(startAddr - ROM_BASE, buf, sizeof(buf));
    }
    
    addr = ROM_BASE;
    value = startValue;
    printf("Checking flash inverted\n");
    for (j = 0; j < BUF_COUNT; ++j) {
        //printf("Reading %08x\n", addr);
        readBlock(addr, buf, sizeof(buf));
        for (i = 0; i < BUF_SIZE; ++i, addr += sizeof(uint32_t))
            if (buf[i] != ~value++)
                printf("%08x: expected %08x, found %08x\n", addr, ~(value - 1), buf[i]);
    }

    printf("done\n");
}

#endif

#ifdef RAM_TEST

{
    int i;

    printf("RAM test\n");
    
    for (i = 0; i < BUF_SIZE; ++i)
        buf[i] = 0xbeef0000 + i;
    writeBlock(RAM_BASE, buf, sizeof(buf));
    
    memset(buf, 0, sizeof(buf));
    
    readBlock(RAM_BASE, buf, sizeof(buf));
    for (i = 0; i < BUF_SIZE; ++i)
        printf("buf[%d] = %08x\n", i, buf[i]);

    printf("done\n");
}

#endif
        
#ifdef BIG_RAM_TEST

{
    uint32_t addr, startValue, value;
    int i, j;
    
    printf("Big RAM test\n");

    addr = RAM_BASE;
    srand(CNT);
    startValue = value = addr + rand();
    printf("Start value %08x\n", startValue);
    printf("Filling RAM\n");
    for (j = 0; j < BUF_COUNT; ++j) {
        uint32_t startAddr = addr;
        for (i = 0; i < BUF_SIZE; ++i, addr += sizeof(uint32_t))
            buf[i] = value++;
        //printf("Writing %08x\n", startAddr);
        writeBlock(startAddr, buf, sizeof(buf));
    }
    
    printf("Checking RAM\n");
    addr = RAM_BASE;
    value = startValue;
    for (j = 0; j < BUF_COUNT; ++j) {
        //printf("Reading %08x\n", addr);
        readBlock(addr, buf, sizeof(buf));
        for (i = 0; i < BUF_SIZE; ++i, addr += sizeof(uint32_t))
            if (buf[i] != value++)
                printf("%08x: expected %08x, found %08x\n", addr, value - 1, buf[i]);
    }

    printf("Filling RAM inverted\n");
    addr = RAM_BASE;
    value = startValue;
    for (j = 0; j < BUF_COUNT; ++j) {
        uint32_t startAddr = addr;
        for (i = 0; i < BUF_SIZE; ++i, addr += sizeof(uint32_t))
            buf[i] = ~value++;
        //printf("Writing %08x\n", startAddr);
        writeBlock(startAddr, buf, sizeof(buf));
    }
    
    printf("Checking RAM inverted\n");
    addr = RAM_BASE;
    value = startValue;
    for (j = 0; j < BUF_COUNT; ++j) {
        //printf("Reading %08x\n", addr);
        readBlock(addr, buf, sizeof(buf));
        for (i = 0; i < BUF_SIZE; ++i, addr += sizeof(uint32_t))
            if (buf[i] != ~value++)
                printf("%08x: expected %08x, found %08x\n", addr, ~(value - 1), buf[i]);
    }

    printf("done\n");
}

#endif

#ifdef CACHE_TEST

{
    int i;
    
    printf("cache test\n");
    
    for (i = 0; i < 32*1024; i += sizeof(uint32_t))
        writeLong(RAM_BASE + i, i);
        
    for (i = 0; i < 32*1024; i += sizeof(uint32_t)) {
        uint32_t data = readLong(RAM_BASE + i);
        if (data != i)
            printf("%08x: expected %08x, found %08x\n", RAM_BASE + i, i, data);
    }

    printf("done\n");
}
    
#endif

    return 0;
}
Example #27
0
RC pinPage (BM_BufferPool *const bm, BM_PageHandle *const page, const PageNumber pageNum)
{ 
  int i; 
  resetPages();
 
  int spotFound = -1;
  for(i = 0; i < bm->numPages; i++){
    if(pages->pageNum == -1){
      spotFound = i;
      break;
    }

    if(pages->pageNum == pageNum){
      spotFound = i;
      break;
    }
    
    if(i < bm->numPages-1){
      pages = pages->nextPage;
    }
  }

  int min = 999999;
  if (spotFound == -1) {
    resetPages();
    for (i = 0; i < bm->numPages; i++) {
      if (pages->fixCount == 0 && pages->repStrat < min) {
	min = pages->repStrat;
	spotFound = i;
      }

      if(i < bm->numPages-1){
	pages = pages->nextPage;
      }
    }
  }
  resetPages();
  for(i = 0; i < bm->numPages; i++){
    if(i == spotFound)
      break;
    if(i < bm->numPages-1){
      pages = pages->nextPage;
    }
  }
  
  SM_FileHandle fH;
  openPageFile(bm->pageFile, &fH);
 
  if(pages->pageNum == pageNum){
    if(bm->strategy == RS_LRU)
      pages->repStrat = ++(metaData.totalRepStrat);
    pages->fixCount++;
    page->pageNum = pageNum;
    strcpy(page->data, pages->pageData);
  }else {
    page->data = malloc(PAGE_SIZE);
    page->pageNum = pageNum;
    readBlock(pageNum, &fH, page->data);
    metaData.numReadIO++;
          
    pages->pageNum = pageNum;

    pages->fixCount = 1;
    pages->repStrat = ++(metaData.totalRepStrat);
    pages->isMark = false;
  }
  closePageFile(&fH);
  
  return RC_OK; 
}
Example #28
0
// Returns:
// OK - if new record retrieved
// ERROR - if there was an interface error
// ERROR_ABORT - if there is no new record (WH1080 generates new records at 
//               best once a minute)
static int readStationData (WVIEWD_WORK *work)
{
    WH1080_IF_DATA*     ifWorkData = (WH1080_IF_DATA*)work->stationData;
    int                 currentPosition, readPosition, index, retVal;

    if ((*(work->medium.usbhidInit))(&work->medium) != OK)
    {
        return ERROR;
    }

    // Read the WH1080 fixed block:
    retVal = readFixedBlock(work, &wh1080Work.controlBlock[0]);
    if (retVal == ERROR_ABORT)
    {
        // Try again later (bad magic number):
        (*(work->medium.usbhidExit))(&work->medium);
        return ERROR_ABORT;
    }
    else if (retVal == ERROR)
    {
        // USB interface error:
        (*(work->medium.usbhidExit))(&work->medium);
        return ERROR;
    }

    // Get the current record position; the WH1080 reports the record it is 
    // building, thus if it changes we need the prior just finished record:
    currentPosition = (int)getUSHORT(&wh1080Work.controlBlock[WH1080_CURRENT_POS]);

    // Make sure the index is aligned on 16-byte boundary:
    if ((currentPosition % 16) != 0)
    {
        // bogus, try again later:
        (*(work->medium.usbhidExit))(&work->medium);
        return ERROR_ABORT;
    }

    // Is this the first time?
    if (wh1080Work.lastRecord == -1)
    {
        // Yes.
        wh1080Work.lastRecord = currentPosition;
        (*(work->medium.usbhidExit))(&work->medium);
        return ERROR_ABORT;
    }

    // Is there a new record?
    if (currentPosition == wh1080Work.lastRecord)
    {
        // No, wait till it is finished.
        (*(work->medium.usbhidExit))(&work->medium);
        return ERROR_ABORT;
    }

    // Read last record that is now complete:
    if (readBlock(work, wh1080Work.lastRecord, &wh1080Work.recordBlock[0]) == ERROR)
    {
        radMsgLog (PRI_HIGH, "WH1080: read data block at index %d failed!",
                   wh1080Work.lastRecord);
        (*(work->medium.usbhidExit))(&work->medium);
        return ERROR;
    }

    (*(work->medium.usbhidExit))(&work->medium);

    readPosition = wh1080Work.lastRecord;
    wh1080Work.lastRecord = currentPosition;

//radMsgLogData(wh1080Work.recordBlock, 32);

    // Is the record valid? Check for unpopulated record or no sensor data
    // received status bit:
    if ((wh1080Work.recordBlock[WH1080_STATUS] & 0x40) != 0)
    {
        // No!
        radMsgLog (PRI_HIGH, 
                   "WH1080: data block at index %d has bad status, ignoring the record",
                   readPosition);
        return ERROR_ABORT;
    }

    // Parse the data received:
    for (index = 0; index < WH1080_NUM_SENSORS; index ++)
    {
        if (decodeSensor(&wh1080Work.recordBlock[decodeVals[index].pos],
                         decodeVals[index].ws_type,
                         decodeVals[index].scale,
                         decodeVals[index].var)
            != OK)
        {
            // Bad sensor data, abort this cycle:
            radMsgLog (PRI_HIGH, 
                       "WH1080: data block at index %d has bad sensor value, ignoring the record",
                       readPosition);
            return ERROR_ABORT;
        }
    }

    // Convert to Imperial units:
    wh1080Work.sensorData.intemp        = wvutilsConvertCToF(wh1080Work.sensorData.intemp);
    wh1080Work.sensorData.outtemp       = wvutilsConvertCToF(wh1080Work.sensorData.outtemp);
    wh1080Work.sensorData.pressure      = wvutilsConvertHPAToINHG(wh1080Work.sensorData.pressure);
    wh1080Work.sensorData.windAvgSpeed  = wvutilsConvertMPSToMPH(wh1080Work.sensorData.windAvgSpeed);
    wh1080Work.sensorData.windGustSpeed = wvutilsConvertMPSToMPH(wh1080Work.sensorData.windGustSpeed);
    wh1080Work.sensorData.rain          = wvutilsConvertMMToIN(wh1080Work.sensorData.rain);

    return OK;
}
Example #29
0
/* Tar file extraction
 * gzFile in, handle of input tarball opened with gzopen
 * int cm, compressionMethod
 * int junkPaths, nonzero indicates to ignore stored path (don't create directories)
 * enum KeepMode keep, indicates to perform if file exists
 * int iCnt, char *iList[], argv style list of files to extract, {0,NULL} for all
 * int xCnt, char *xList[], argv style list of files NOT to extract, {0,NULL} for none
 * int failOnHardLinks, if nonzero then will treat failure to create a hard link same as
 *   failure to create a regular file, 0 prints a warning if fails - note that hardlinks
 *   will always fail on Windows prior to NT 5 (Win 2000) or later and non NTFS file systems.
 *
 * returns 0 (or positive value) on success
 * returns negative value on error, where
 *   -1 means error reading from tarball
 *   -2 means error extracting file from tarball
 *   -3 means error creating hard link
 */
int tgz_extract(gzFile in, int cm, int junkPaths, enum KeepMode keep, int iCnt, TCHAR *iList[], int xCnt, TCHAR *xList[], int failOnHardLinks)
{
  int           getheader = 1;    /* assume initial input has a tar header */
  HANDLE        outfile = INVALID_HANDLE_VALUE;

  union         tar_buffer buffer;
  unsigned long remaining;
  TCHAR          fname[BLOCKSIZE]; /* must be >= BLOCKSIZE bytes */
  time_t        tartime;

  /* do any prep work for extracting from compressed TAR file */
  if (cm_init(in, cm))
  {
    PrintMessage(_T("tgz_extract: unable to initialize decompression method."));
    cm_cleanup(cm);
    return -1;
  }
  
  while (1)
  {
    if (readBlock(cm, &buffer) < 0) return -1;
      
    /*
     * If we have to get a tar header
     */
    if (getheader >= 1)
    {
      /*
       * if we met the end of the tar
       * or the end-of-tar block,
       * we are done
       */
      if (/* (len == 0)  || */ (buffer.header.name[0]== 0)) break;

      /* compute and check header checksum, support signed or unsigned */
      if (!valid_checksum(&(buffer.header)))
      {
        PrintMessage(_T("tgz_extract: bad header checksum"));
        cm_cleanup(cm);
        return -1;
      }

      /* store time, so we can set the timestamp on files */
      tartime = (time_t)getoct(buffer.header.mtime,12);

      /* copy over filename chunk from header, avoiding overruns */
      if (getheader == 1) /* use normal (short or posix long) filename from header */
      {
        /* NOTE: prepends any prefix, including separator, and ensures terminated */
        memset(fname, 0, sizeof(fname));
		getFullName(&buffer, fname);
      }
      else /* use (GNU) long filename that preceeded this header */
      {
#if 0
        /* if (strncmp(fname,buffer.header.name,SHORTNAMESIZE-1) != 0) */
        char fs[SHORTNAMESIZE];   /* force strings to same max len, then compare */
        lstrcpyn(fs, fname, SHORTNAMESIZE);
        fs[SHORTNAMESIZE-1] = '\0';
        buffer.header.name[SHORTNAMESIZE-1] = '\0';
        if (lstrcmp(fs, buffer.header.name) != 0)
        {
          PrintMessage(_T("tgz_extract: mismatched long filename"));
          cm_cleanup(cm);
          return -1;
        }
#else
		PrintMessage(_T("tgz_extract: using GNU long filename [%s]"), fname);
#endif
      }
      /* LogMessage("buffer.header.name is:");  LogMessage(fname); */


      switch (buffer.header.typeflag)
      {
        case DIRTYPE:
		  dirEntry:
          if (!junkPaths)
          {
            safetyStrip(fname);
            makedir(fname);
          }
	      break;
		case LNKTYPE:   /* hard link */ 
		case CONTTYPE:  /* contiguous file, for compatibility treat as normal */
        case REGTYPE:
        case AREGTYPE:
	      /* Note: a file ending with a / may actually be a BSD tar directory entry */
	      if (fname[strlen(fname)-1] == '/')
	        goto dirEntry;

	      remaining = getoct(buffer.header.size,12);
	      if ( /* add (remaining > 0) && to ignore 0 zero byte files */
               ( (iList == NULL) || (matchname(fname, iCnt, iList, junkPaths)) ) &&
               (!matchname(fname, xCnt, xList, junkPaths))
             )
	      {
			  if (!junkPaths) /* if we want to use paths as stored */
			  {
	              /* try creating directory */
	              TCHAR *p = tstrrchr(fname, '/');
	              if (p != NULL) 
	              {
	                *p = '\0';
	                makedir(fname);
	                *p = '/';
	              }
			  }
			  else
			  {
	              /* try ignoring directory */
	              TCHAR *p = tstrrchr(fname, '/');
	              if (p != NULL) 
	              {
	                /* be sure terminating '\0' is copied and */
	                /* use ansi memcpy equivalent that handles overlapping regions */
	                MoveMemory(fname, p+1, (strlen(p+1) + 1) * sizeof(TCHAR) );
	              }
	          }
	          if (*fname) /* if after stripping path a fname still exists */
	          {
	            /* Attempt to open the output file and report action taken to user */
	            const TCHAR szERRMsg[] = _T("Error: Could not create file "),
	                        szSUCMsg[] = _T("Writing "),
	                        szSKPMsg[] = _T("Skipping ");
	            const TCHAR * szMsg = szSUCMsg;

	            safetyStrip(fname);

				if (buffer.header.typeflag == LNKTYPE)
				{
					outfile = INVALID_HANDLE_VALUE;
					/* create a hardlink if possible, else produce just a warning unless failOnHardLinks is true */
					if (!MakeHardLink(fname, buffer.header.linkname))
					{
						PrintMessage(_T("Warning: unable to create hard link %s [%d]"), fname, GetLastError());
						if (failOnHardLinks) 
						{
							cm_cleanup(cm);
							return -3;
						}
					}
					else
					{
						outfile = CreateFile(fname,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
						goto setTimeAndCloseFile;
					}
				} else 
				{
	            /* Open the file for writing mode, creating if doesn't exist and truncating if exists and overwrite mode */
	            outfile = CreateFile(fname,GENERIC_WRITE,FILE_SHARE_READ,NULL,(keep==OVERWRITE)?CREATE_ALWAYS:CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);

	            /* failed to open file, either valid error (like open) or it already exists and in a keep mode */
	            if (outfile == INVALID_HANDLE_VALUE)
	            {
	              /* if skip existing or only update existing and failed to open becauses exists */
	              if ((keep!=OVERWRITE) && (GetLastError()==ERROR_FILE_EXISTS))
	              {
	                /* assume skipping initially (mode==SKIP or ==UPDATE with existing file newer) */
	                szMsg = szSKPMsg; /* and update output message accordingly */

					/* if in update mode, check filetimes and reopen in overwrite mode */
	                if (keep == UPDATE)
	                {
	                  FILETIME ftm_a;
                      HANDLE h;
                      WIN32_FIND_DATA ffData;
 
	                  cnv_tar2win_time(tartime, &ftm_a); /* archive file time */
	                  h = FindFirstFile(fname, &ffData); /* existing file time */

                      if (h!=INVALID_HANDLE_VALUE)
                        FindClose(h);  /* cleanup search handle */
                      else
                        goto ERR_OPENING;

                      /* compare date+times, is one in tarball newer? */
                      if (*((LONGLONG *)&ftm_a) > *((LONGLONG *)&(ffData.ftLastWriteTime)))
                      {
                        outfile = CreateFile(fname,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
                        if (outfile == INVALID_HANDLE_VALUE) goto ERR_OPENING;
                        szMsg = szSUCMsg;
                      }
	                }
	              }
	              else /* in overwrite mode or failed for some other error than exists */
	              {
                    ERR_OPENING:
	                PrintMessage(_T("%s%s [%d]"), szERRMsg, fname, GetLastError());
	                cm_cleanup(cm);
	                return -2;
	              }
	            }

 	            /* Inform user of current extraction action (writing, skipping file XYZ) */
	            PrintMessage(_T("%s%s"), szMsg, fname);
				}
	          }
	      }
	      else
	          outfile = INVALID_HANDLE_VALUE;

	      /*
	       * could have no contents, in which case we close the file and set the times
	       */
	      if (remaining > 0)
	          getheader = 0;
		  else
	      {
	          setTimeAndCloseFile:
	          getheader = 1;
	          if (outfile != INVALID_HANDLE_VALUE)
	          {
	              FILETIME ftm;
 
	              cnv_tar2win_time(tartime, &ftm);
	              SetFileTime(outfile,&ftm,NULL,&ftm);
	              CloseHandle(outfile);
	              outfile = INVALID_HANDLE_VALUE;
	          }
		  }

	      break;
		case GNUTYPE_LONGLINK:
		case GNUTYPE_LONGNAME:
		{
	      remaining = getoct(buffer.header.size,12);
	      if (readBlock(cm, fname) < 0) return -1;
	      fname[BLOCKSIZE-1] = '\0';
	      if ((remaining >= BLOCKSIZE) || ((unsigned)strlen(fname) > remaining))
	      {
	          PrintMessage(_T("tgz_extract: invalid long name"));
	          cm_cleanup(cm);
	          return -1;
	      }
	      getheader = 2;
	      break;
		}
        default:
/*
	      if (action == TGZ_LIST)
	          printf(" %s     <---> %s\n",strtime(&tartime),fname);
*/
	      break;
      }
    }
    else  /* (getheader == 0) */
    {
      unsigned int bytes = (remaining > BLOCKSIZE) ? BLOCKSIZE : remaining;
	  unsigned long bwritten;

      if (outfile != INVALID_HANDLE_VALUE)
      {
          WriteFile(outfile,buffer.buffer,bytes,&bwritten,NULL);
		  if (bwritten != bytes)
          {
			  PrintMessage(_T("Error: write failed for %s"), fname);
              CloseHandle(outfile);
              DeleteFile(fname);

              cm_cleanup(cm);
              return -2;
          }
      }
      remaining -= bytes;
      if (remaining == 0) goto setTimeAndCloseFile;
    }
  } /* while(1) */
  
  cm_cleanup(cm);

  return 0;
}
Example #30
0
void
CloudStream::precache()
{
    // For reading the tags of an MP3, TagLib tends to request:
    // 1. The first 1024 bytes
    // 2. Somewhere between the first 2KB and first 60KB
    // 3. The last KB or two.
    // 4. Somewhere in the first 64KB again
    //
    // OGG Vorbis may read the last 4KB.
    //
    // So, if we precache the first 64KB and the last 8KB we should be sorted :-)
    // Ideally, we would use bytes=0-655364,-8096 but Google Drive does not seem
    // to support multipart byte ranges yet so we have to make do with two
    // requests.
    tDebug( LOGINFO ) << "#### CloudStream : Precaching from :" << m_filename;

    switch( m_cacheState )
    {
        case CloudStream::BeginningCache :
        {
            seek( 0, TagLib::IOStream::Beginning );
            readBlock( kTaglibPrefixCacheBytes );
            m_cacheState = CloudStream::EndCache;
            break;
        }

        case CloudStream::EndCache :
        {
            seek( kTaglibSuffixCacheBytes, TagLib::IOStream::End );
            readBlock( kTaglibSuffixCacheBytes );
            m_cacheState = CloudStream::EndCacheDone;
            break;
        }

        case CloudStream::EndCacheDone :
        {
            clear();
            // construct the tag map
            QString mimeType = m_tags["mimetype"].toString();
            boost::scoped_ptr<TagLib::File> tag;

            if ( mimeType == "audio/mpeg" ) // && title.endsWith(".mp3"))
            {
                tag.reset(new TagLib::MPEG::File(
                              this,  // Takes ownership.
                              TagLib::ID3v2::FrameFactory::instance(),
                              TagLib::AudioProperties::Accurate));
            }
            else if ( mimeType == "audio/mp4" || ( mimeType == "audio/mpeg" ) ) //  && title.endsWith(".m4a")))
            {
                tag.reset( new TagLib::MP4::File( this, true, TagLib::AudioProperties::Accurate ) );
            }
            else if ( mimeType == "application/ogg" || mimeType == "audio/ogg" )
            {
                tag.reset( new TagLib::Ogg::Vorbis::File( this, true, TagLib::AudioProperties::Accurate ) );
            }
        #ifdef TAGLIB_HAS_OPUS
            else if ( mimeType == "application/opus" || mimeType == "audio/opus" )
            {
                tag.reset( new TagLib::Ogg::Opus::File( this, true, TagLib::AudioProperties::Accurate ) );
            }
        #endif
            else if ( mimeType == "application/x-flac" || mimeType == "audio/flac" )
            {
                tag.reset( new TagLib::FLAC::File( this, TagLib::ID3v2::FrameFactory::instance(), true,
                                                  TagLib::AudioProperties::Accurate ) );
            }
            else if ( mimeType == "audio/x-ms-wma" )
            {
                tag.reset( new TagLib::ASF::File( this, true, TagLib::AudioProperties::Accurate ) );
            }
            else
            {
                tDebug( LOGINFO ) << "Unknown mime type for tagging:" << mimeType;
            }

            if (this->num_requests() > 2) {
                // Warn if pre-caching failed.
                tDebug( LOGINFO ) << "Total requests for file:" << m_tags["fileId"]
                                  << " : " << this->num_requests() << " with "
                                  << this->cached_bytes() << " bytes cached";
            }

            //construction of the tag's map
            if ( tag->tag() && !tag->tag()->isEmpty() )
            {
                m_tags["track"] = tag->tag()->title().toCString( true );
                m_tags["artist"] = tag->tag()->artist().toCString( true );
                m_tags["album"] = tag->tag()->album().toCString( true );
                m_tags["size"] = QString::number( m_length );

                if ( tag->tag()->track() != 0 )
                {
                    m_tags["albumpos"] = tag->tag()->track();
                }
                if ( tag->tag()->year() != 0 )
                {
                    m_tags["year"] = tag->tag()->year();
                }

                if ( tag->audioProperties() )
                {
                    m_tags["duration"] = tag->audioProperties()->length();
                    m_tags["bitrate"] = tag->audioProperties()->bitrate();
                }
            }
            emit tagsReady( m_tags, m_javascriptCallbackFunction );
            break;
        }
    }

}