Ejemplo n.º 1
0
// --------------------------------------------------------------
// Funkcja, ktora czysci cala liste
void Wyczysc(SLista *lista)
{
	__PRINTF("DEBUG: Wyczysc(%p)\n", lista);
	
	SLista *ptr = lista;
	SLista *to_del;
	
	while (ptr)
	{
		to_del = ptr;
		ptr = ptr -> nastepny;
		
		__PRINTF("DEBUG: Wyczysc: usuwam element (%p, %d)\n", to_del, to_del->element);
		free (to_del);	
	}
}
Ejemplo n.º 2
0
// --------------------------------------------------------------
// Funkcja wypelnia losowymi liczbami liste oraz zwraca nowy 
// poczatek listy
SLista* WypelnijLosowymi(SLista *lista, int ilosc)
{
	__PRINTF("DEBUG: WypelnijLosowymi(%p, %d)\n", lista, ilosc);

	// Ustawianie zarodka dla funkcji losujacej
	srand(time(NULL));
	
	int i, liczba;
	for (i=0; i<ilosc; i++)
	{
		__PRINTF("DEBUG:   losuje liczbe (%d)\n", liczba);
		
		liczba = rand()%100;
		lista = Wstaw(lista, liczba);
	}
	return lista;
}
Ejemplo n.º 3
0
// --------------------------------------------------------------
// Funkcja, ktora wstawia liczbe na poczatek listy
// oraz zwraca nowy poczatek listy
SLista* Wstaw(SLista *lista, int liczba)
{
	__PRINTF("DEBUG: Wstaw(%p, %d)\n", lista, liczba);
	
	SLista *nowy = (SLista *)malloc( sizeof(SLista) );
	if (nowy)
	{
		nowy->element = liczba;
		nowy->nastepny = lista;	
	}
	else
	{
		__PRINTF("DEBUG: Wstaw(%p, %d): Nie powiodlo sie, brak pamieci!\n", lista, liczba);
	}
	
	
	return nowy;
}
Ejemplo n.º 4
0
// --------------------------------------------------------------
// Funkcja, ktora szuka liczby na liscie 
// zwraca element listy badz NULL w przypadku braku
SLista* Szukaj(SLista *lista, int liczba)
{
	
	__PRINTF("DEBUG: Szukaj(%p, %d)\n", lista, liczba);
	SLista *ptr = lista;
	
	while (ptr != NULL)
	{
		__PRINTF("DEBUG:   jestem w elemencie %d \n", ptr->element);
		
		if (ptr->element == liczba)
		{
			return ptr;
		}
		
		ptr = ptr->nastepny;
	}
	
	return NULL;
}
Ejemplo n.º 5
0
/**
    Open the device and do some initalisation work.
    
    @param  aParams device parameters
    @return Epoc error code, KErrNone if everything is OK
*/
TInt CWinImgFileDevice::Connect(const TMediaDeviceParams& aParams)
{
    __PRINT(_L("#-- CWinImgFileDevice::Connect()"));    
    
    if(!aParams.ipDevName)
    {
        __LOG(_L("#-- CWinImgFileDevice::Connect() device name is not set!"));    
        return KErrBadName;
    }
    __PRINTF(aParams.ipDevName);
    ASSERT(!HandleValid());

    //-- 1. try to locate an image file by given name.
    WIN32_FIND_DATAA wfd;
    iDevHandle = FindFirstFileA(aParams.ipDevName, &wfd);

    const TBool ImgFileAlreadyExists = HandleValid(iDevHandle);
    
    FindClose(iDevHandle);
    iDevHandle = NULL;
    
    //-- sector size we will use within image file
    const TUint32   sectorSizeToUse = (aParams.iDrvGeometry.iBytesPerSector == 0) ? KDefaultSectorSz : aParams.iDrvGeometry.iBytesPerSector; 
          TUint32   fileSzInSectorsToUse = 0;

    const TUint32   reqSizeSec = aParams.iDrvGeometry.iSizeInSectors; //-- required size in sectors
    const DWORD     dwAccessMode = (aParams.iReadOnly) ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE;  

    if(ImgFileAlreadyExists)
    {//-- if the image file already exists, try to open it and optionally adjust its size
        const TInt64    ImgFileSize = MAKE_TINT64(wfd.nFileSizeHigh, wfd.nFileSizeLow);
        const TUint32   ImgFileSectors = (TUint32)(ImgFileSize / sectorSizeToUse);
        const TBool     ImgFileIsRO = wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY;
        
        DWORD dwFileCreationMode = 0;
        TBool bNeedToAdjustFileSize = EFalse;

        if(reqSizeSec == 0 || reqSizeSec == ImgFileSectors)
        {//-- the required size is either not specified (auto) or the same as the existing file has.
         //-- we can just open this file
         dwFileCreationMode = OPEN_EXISTING;
         fileSzInSectorsToUse = ImgFileSectors;
        }
        else
        {//-- we will have to overwrite the image file
            if(ImgFileIsRO)
            {//-- we won't be able to overwrite existing file.
                __LOG(_L("#-- CWinImgFileDevice::Connect() unable to adjust image file size!"));    
                return KErrAccessDenied;
            }

         fileSzInSectorsToUse = reqSizeSec;
         dwFileCreationMode = CREATE_ALWAYS;
         bNeedToAdjustFileSize = ETrue;
        }

        iDevHandle = CreateFileA(aParams.ipDevName,
                                dwAccessMode, 
                                FILE_SHARE_READ,
                                (LPSECURITY_ATTRIBUTES)NULL,
                                dwFileCreationMode,
                                FILE_ATTRIBUTE_NORMAL,
                                NULL);

        if(!HandleValid(iDevHandle))
        {
            const DWORD winErr = GetLastError();
            __LOG1(_L("#-- CWinImgFileDevice::Connect() Error opening/creating file! WinErr:%d"), winErr);
            return MapWinError(winErr);
        }     

        //-- adjust file size if we need
        if(bNeedToAdjustFileSize)
        {
            const TInt64 newFileSize = (TInt64)reqSizeSec * sectorSizeToUse;
            ASSERT(newFileSize);

            LONG  newFSzHi = I64HIGH(newFileSize);
            DWORD dwRes = SetFilePointer(iDevHandle, I64LOW(newFileSize), &newFSzHi, FILE_BEGIN);
            if(dwRes == INVALID_SET_FILE_POINTER || !SetEndOfFile(iDevHandle))
            {
                const DWORD winErr = GetLastError();
                Disconnect();
                __LOG1(_L("#-- CWinImgFileDevice::Connect() unable to set file size! WinErr:%d"), winErr);
                return MapWinError(winErr);
            }
        }

    }
    else //if(ImgFileAlreadyExists)
    {//-- if the image file does not exist or its size differs from required. try to create it
       
        if(reqSizeSec == 0)
        {
            __LOG(_L("#-- CWinImgFileDevice::Connect() The image file doesn't exist ant its size isn't specified!"));    
            return KErrArgument;
        }
       
        fileSzInSectorsToUse = reqSizeSec;

        //-- create a new image file
        iDevHandle = CreateFileA(aParams.ipDevName,
                                GENERIC_READ | GENERIC_WRITE, 
                                FILE_SHARE_READ,
                                (LPSECURITY_ATTRIBUTES)NULL,
                                CREATE_ALWAYS,
                                FILE_ATTRIBUTE_NORMAL,
                                NULL);

        if(!HandleValid(iDevHandle))
        {
            const DWORD winErr = GetLastError();
            __LOG1(_L("#-- CWinImgFileDevice::Connect() can not create file! WinErr:%d"), winErr);
            return MapWinError(winErr);
        }     

        //-- set its size
        const TInt64 newFileSize = (TInt64)reqSizeSec * sectorSizeToUse;
        ASSERT(newFileSize);

        LONG  newFSzHi = I64HIGH(newFileSize);
        DWORD dwRes = SetFilePointer(iDevHandle, I64LOW(newFileSize), &newFSzHi, FILE_BEGIN);
        if(dwRes == INVALID_SET_FILE_POINTER || !SetEndOfFile(iDevHandle))
        {
            const DWORD winErr = GetLastError();
            Disconnect();
            __LOG1(_L("#-- CWinImgFileDevice::Connect() unable to set file size! WinErr:%d"), winErr);
            return MapWinError(winErr);
        }

        //-- if parametrs require a read-only file, reopen it in RO mode, it doesn't make a lot of sense though...
        if(aParams.iReadOnly)
        {
            CloseHandle(iDevHandle);
            iDevHandle = NULL;

            iDevHandle = CreateFileA(aParams.ipDevName,
                                GENERIC_READ , 
                                FILE_SHARE_READ,
                                (LPSECURITY_ATTRIBUTES)NULL,
                                OPEN_EXISTING,
                                FILE_ATTRIBUTE_NORMAL,
                                NULL);

            if(!HandleValid(iDevHandle))
            {
                const DWORD winErr = GetLastError();
                __LOG1(_L("#-- CWinImgFileDevice::Connect() Can't reopen a file in RO mode! WinErr:%d"), winErr);
                return MapWinError(winErr);
            }     
            
        }//if(aParams.iReadOnly)

    }//else if(ImgFileAlreadyExists)
    
    //-- here we must have the image file created/opened and with correct size
    ASSERT(HandleValid());
    ASSERT(sectorSizeToUse);

    if(fileSzInSectorsToUse < KMinMediaSizeInSectors)
    {
        __LOG1(_L("#-- CWinImgFileDevice::Connect() Image file is too small!  sectors:%d"), fileSzInSectorsToUse);
        Disconnect();
        return KErrGeneral;     
    }

    iDrvGeometry.iBytesPerSector = sectorSizeToUse;
    iDrvGeometry.iSizeInSectors  = fileSzInSectorsToUse;
    
    //-- map the image file into memory.
    ASSERT(!HandleValid(ihFileMapping));
    ASSERT(!ipImageFile);
    
    /*
    don't map image file, because it can be > 4G.
    ihFileMapping = CreateFileMapping(Handle(), NULL,
                                      aParams.iReadOnly ? PAGE_READONLY : PAGE_READWRITE,
                                      0, 0, NULL);
    if(HandleValid(ihFileMapping))
    {
    ipImageFile = (TUint8*)MapViewOfFile(ihFileMapping, 
                                         aParams.iReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE,
                                         0,0,0);
    }

    if(!ipImageFile)
    {
        __PRINT1(_L("#-- CWinImgFileDevice::Connect() Error mapping file! WinErr:%d"), GetLastError());
        Disconnect();
        return KErrGeneral;
    }
    */

    return KErrNone;
}
Ejemplo n.º 6
0
/**
    Open the device and do some initalisation work.
    
    @param  aParams device parameters
    @return Epoc error code, KErrNone if everything is OK
*/
TInt CWinVolumeDevice::Connect(const TMediaDeviceParams& aParams)
{
    
    __PRINT(_L("#-- CWinVolumeDevice::Connect()"));    
    
    if(!aParams.ipDevName)
    {
        __LOG(_L("#-- CWinVolumeDevice::Connect() device name is not set!"));    
        return KErrBadName;
    }

    __PRINTF(aParams.ipDevName);
    
    ASSERT(!HandleValid() && ipScratchBuf);

    //-- open the device
    DWORD dwAccess = GENERIC_READ;
    
    if(!aParams.iReadOnly)
        dwAccess |= GENERIC_WRITE;  
    
    iDevHandle = CreateFileA(aParams.ipDevName,
                             dwAccess, 
                             FILE_SHARE_READ,
                             (LPSECURITY_ATTRIBUTES)NULL,
                             OPEN_EXISTING,
                             FILE_ATTRIBUTE_NORMAL,
                             NULL);

    if(!HandleValid())
    {
        __LOG1(_L("#-- CWinVolumeDevice::Connect() Error creating device handle! WinErr:%d"), GetLastError());
        return KErrGeneral;
    }     
    
    //-- find out device geometry
    iMediaType = Unknown;
    iDrvGeometry.iBytesPerSector = KDefaultSectorSz;

    DWORD junk; 

    //-- 1. try to query disk geometry, but it can produce wrong results for partitioned media
    BOOL bResult = DeviceIoControl(Handle(),
                                   IOCTL_DISK_GET_DRIVE_GEOMETRY,
                                   NULL, 0,
                                   ipScratchBuf, KScratchBufSz,
                                   &junk, (LPOVERLAPPED)NULL);

    if(bResult)
    {
        const DISK_GEOMETRY& dg = (const DISK_GEOMETRY&)*ipScratchBuf;
        
        iDrvGeometry.iBytesPerSector = dg.BytesPerSector;
        iMediaType = dg.MediaType;

        __PRINT3(_L("#-- dev geometry: Cyl:%d Heads:%d Sectors:%d"), dg.Cylinders.LowPart, dg.TracksPerCylinder, dg.SectorsPerTrack);    
        __PRINT2(_L("#-- dev geometry: MediaType:%d, bps:%d"), dg.MediaType, dg.BytesPerSector);    

    }
    else
    {
        iMediaType = Unknown;
        iDrvGeometry.iBytesPerSector = KDefaultSectorSz;

        __LOG1(_L("#-- CWinVolumeDevice::Connect() IOCTL_DISK_GET_DRIVE_GEOMETRY WinError:%d !"), GetLastError());
    }

    //-- 1.1 check "bytes per sector" value and how it corresponds to the request from parameters
    if(aParams.iDrvGeometry.iBytesPerSector == 0)
    {//-- do nothing, this parameter is not set in config file, use media's
    } 
    else if(aParams.iDrvGeometry.iBytesPerSector != iDrvGeometry.iBytesPerSector)
    {//-- we can't set "SectorSize" value for the physical media
        __LOG1(_L("#-- CWinVolumeDevice::Connect() can not use 'Sec. Size' value from config:%d !"), aParams.iDrvGeometry.iBytesPerSector);
        Disconnect();
        return KErrArgument;
    }


    ASSERT(IsPowerOf2(BytesPerSector()) && BytesPerSector() >= KDefaultSectorSz && BytesPerSector() < 4096);

    //-- find out partition information in order to determine volume size. 
    bResult = DeviceIoControl(Handle(),
                              IOCTL_DISK_GET_PARTITION_INFO,
                              NULL, 0,
                              ipScratchBuf, KScratchBufSz,
                              &junk, (LPOVERLAPPED)NULL);

    if(!bResult)
    {//-- this is a fatal error
        __LOG1(_L("#-- CWinVolumeDevice::Connect() IOCTL_DISK_GET_PARTITION_INFO WinError:%d !"), GetLastError());
        Disconnect();
        return KErrBadHandle;    
    }

    //-- get partition informaton
    const PARTITION_INFORMATION& pi = (const PARTITION_INFORMATION&)*ipScratchBuf;
    TInt64 volSz = MAKE_TINT64(pi.PartitionLength.HighPart, pi.PartitionLength.LowPart);
    iDrvGeometry.iSizeInSectors = (TUint32)(volSz / iDrvGeometry.iBytesPerSector);
            
    __LOG3(_L("#-- partition size, bytes:%LU (%uMB), sectors:%u"), volSz, (TUint32)(volSz>>20), iDrvGeometry.iSizeInSectors);
   
    //-- check if the media size is set in coonfig and if we can use this setting.
    if(aParams.iDrvGeometry.iSizeInSectors == 0)
    {//-- do nothing, the media size is not set in the ini file, use existing media parameters
    }
    else if(aParams.iDrvGeometry.iSizeInSectors > iDrvGeometry.iSizeInSectors)
    {//-- requested media size in ini file is bigger than physical media, error.
     //-- we can't increase physical media size
    __LOG2(_L("#-- CWinVolumeDevice::Connect() 'MediaSizeSectors' value from config:%d > than physical:%d !"), aParams.iDrvGeometry.iSizeInSectors, iDrvGeometry.iSizeInSectors);
    Disconnect();
    return KErrArgument;
    }
    else if(aParams.iDrvGeometry.iSizeInSectors < iDrvGeometry.iSizeInSectors)
    {//-- settings specify smaller media than physical one, adjust the size
    __PRINT1(_L("#-- reducing media size to %d sectors"), aParams.iDrvGeometry.iSizeInSectors);
    iDrvGeometry.iSizeInSectors = aParams.iDrvGeometry.iSizeInSectors;
    }


    ASSERT(iDrvGeometry.iSizeInSectors > KMinMediaSizeInSectors);
    return KErrNone;
}