Ejemplo n.º 1
0
void SqlApiBase::SplitConnectionString(const char *conn, std::string &user, std::string &pwd, std::string &server, std::string &db, std::string &port)
{
	if(conn == NULL)
		return;

	std::string db_full;

	SplitConnectionString(conn, user, pwd, db_full);

	const char *start = db_full.c_str();

	// Find : and , that denote the server port and the database name
	const char *semi = strchr(start, ':');
	const char *comma = strchr(start, ',');

	const char *end = (semi != NULL) ? semi :comma;

	// Define server name
	if(end != NULL)
		server.assign(start, (size_t)(end - start));
	else
		server = start;

	// Define port
	if(semi != NULL)
	{
		if(comma != NULL && comma > semi)
			port.assign(semi + 1, (size_t)(comma - semi - 1));
		else
			port = semi + 1;
	}

	if(comma != NULL)
		db = Str::SkipSpaces(comma + 1);
}
//---------------------------------------------------------------------------
// Open an image store - this determines the type of the store and whether
// or not NT open functions are available and then decides the best way
// to open the target.
//
bool __fastcall TImageStore::Open(void) {
    FGeometry.Bytes = (unsigned long long)0;
    FGeometry.BytesPerSector = (unsigned long long)0;
    bool bSuccess;

    switch (FType) {
    case isFile: {
        LARGE_INTEGER nnFileSize;

        DWORD ShareMode  = FILE_SHARE_READ;//(bWriteAccess?0:FILE_SHARE_READ);
        DWORD CreateMode = (bWriteAccess?CREATE_ALWAYS:OPEN_EXISTING);
        DWORD Access     = GENERIC_READ | (bWriteAccess?GENERIC_WRITE:0);

        hHandle = CreateFile(FFileName.c_str(), Access, ShareMode, NULL, CreateMode, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hHandle != INVALID_HANDLE_VALUE) {
            if (!bWriteAccess) {
                nnFileSize.LowPart = GetFileSize(hHandle, (unsigned long *)&nnFileSize.HighPart);
                FGeometry.Bytes = nnFileSize.QuadPart;
                CheckCompression();
            } else
                FGeometry.Bytes = 0; //-1;
            FGeometry.BytesPerSector = 0;
            FGeometry.BytesPerCluster = 0;
        } else
            return false;
        break;
    }  // case isFile:

    case isDrive: {
        PARTITION_INFORMATION PartitionInfo;
        DISK_GEOMETRY DiskGeometry;
        DWORD Dummy;



        // A example for a valid file name is: \\.\C

        DWORD Access     = GENERIC_READ | (bWriteAccess?GENERIC_WRITE:0);
        DWORD ShareMode  = (bWriteAccess?0:FILE_SHARE_READ|FILE_SHARE_WRITE);

        if (HaveNTCalls ) {
            wchar_t wFileName[255];
            int pos = 0;
            for (pos = 0; pos < FFileName.size(); pos++) {
                wFileName[pos] = FFileName[pos];
                wFileName[pos+1] = L'\0';
            }

            hHandle = NTOpen(wFileName, SYNCHRONIZE | Access, FILE_ATTRIBUTE_NORMAL, ShareMode, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY);
        } else {
            if (strncmp ("\\Device\\Floppy", FFileName.c_str(), 14) == 0) {
                printf ("Make open floppy fail\n");
                return false;
            }
            printf ("%s line %i: Try to open %s\n", __FILE__, __LINE__, FFileName.c_str());
            hHandle = CreateFile(FFileName.c_str(), GENERIC_READ, ShareMode, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN | FILE_FLAG_BACKUP_SEMANTICS, NULL);
        }

        if (hHandle != INVALID_HANDLE_VALUE) {
            bSuccess = DeviceIoControl(hHandle, IOCTL_DISK_GET_PARTITION_INFO, NULL, 0, &PartitionInfo, sizeof(PartitionInfo), &Dummy, NULL);
            if (!bSuccess) {
                Close();
                return false;
            }
            bSuccess = DeviceIoControl(hHandle, IOCTL_DISK_GET_DRIVE_GEOMETRY	, NULL, 0, &DiskGeometry, sizeof(DiskGeometry), &Dummy, NULL);
            if (!bSuccess) {
                Close();
                return false;
            }
            FGeometry.Bytes = (unsigned long long)PartitionInfo.PartitionLength.QuadPart;
            FGeometry.BytesPerSector = DiskGeometry.BytesPerSector;
#ifdef DEBUG_PARTSIZE
            printf("Bytes: %llu\n", FGeometry.Bytes);
            if ( FGeometry.Bytes == (__uint64)0)
                printf ("Hallo\n");
#endif

#if 0
            // Check to see if we're dealing with a floppy.  Does anyone have one any more?
            if (FFileName.Pos("Floppy") || FileName.Pos("A:") || FileName.Pos("B:"))
                FGeometry.Bytes = DiskGeometry.Cylinders.QuadPart * DiskGeometry.TracksPerCylinder * DiskGeometry.SectorsPerTrack * DiskGeometry.BytesPerSector;
#endif
        } else {
            printf ("invalid file handle\n");
            return false;
        }
#if 0
        CheckFileSystem();
#endif

        break;
    }  // case isDrive:

    case isNBD: {
        printf ("%s line %i: Here\n", __FILE__, __LINE__);
        return false;
#ifdef _WIN32
#if 0
        AnsiString Host;
        int nPort = 0;

        SplitConnectionString(FFileName, Host, nPort);

        if (NBDConnection)
            delete NBDConnection;
        NBDConnection = new TNBDClient(NULL);
        NBDConnection->Host = Host;
        NBDConnection->Port = nPort;
        NBDConnection->Timeout = SelfImageConfig->Values["NBDTimeout"];
        NBDConnection->TransactionSize = SelfImageConfig->Values["NBDTransactionSize"] * 1024;
        NBDConnection->Open();
        FGeometry.Bytes = NBDConnection->Size;
        CheckFileSystem();
#endif
#endif
    }  // case isNBD:

    default:
        // ERROR
        return false;
        break;
    }  // switch (Type)

    return (hHandle != INVALID_HANDLE_VALUE);
}  // bool __fastcall TImageStore::Open(void)