Solved

Question regarding the Symbollink Unit created by Rlibby for the virtual drive manager

Posted on 2006-07-20
4
280 Views
Last Modified: 2013-12-03
Hi

Im working with the virtual drive manager, for the most part created by and maintained by the symbollink unit, and I am trying to prevent the following situation, since this situation does not seem logical or of any use, but can be done

Someone can add a new drive, which points to a virtual drive, this does not seem to cause problems, but I cannot find any use for this, so Im trying to prevent this from being done, by comparing the new drive letter to the strings which represent the present and current virtual drives

procedure TMainForm.btnAddClick(Sender: TObject);
begin

  AddForm.Startup;
  if (AddForm.ShowModal = mrOK) and (AddForm.Drive > #0) then
  begin
   
>>>if pos(SymbolicLinks[AddForm.Drive], SymbolicLinks[cIndex].DriveLetter) = 0 then
{where the first is the new drive we are trying to add, and the second should be the list of the present virtual drives}

     SymbolicLinks[AddForm.Drive].DevicePath:=AddForm.txtPath.Text;
     SymbolicLinks[AddForm.Drive].Persist(True);
  end;
  LoadVirtualDrives;

end;

I have looked through the symbollink unit, but really cannot make much sense of it, at least not so much that i can perform the comparison, the above code fails and i know why, im comparing different types, plus i need to declare a variable [i think?] but since i even do not know if im comparing the right things i got stuck here

Frank
0
Comment
Question by:fdehell
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17145616
Frank,

You just need to check the new path being added to get the drive letter, eg:

path = "X:\system32" // eg X was mapped to "c:\windows"

drive would be "X". And check to see if that drive maps to a path by using IsPath, eg:

if SymbolicLinks['X'].IsPath then
 // Dont allow a mapping to it

If IsPath is false, then its a real device or it hasn't been mapped. If you need an example let me know.

Regards,
Russell


 
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 17146365
Frank,
You can also update your version of the SymbolicLink unit to include the 2 following utility functions (should make your life easier).

---

// Insert after the TSymbolicLinks declaration

////////////////////////////////////////////////////////////////////////////////
//   Utility functions
////////////////////////////////////////////////////////////////////////////////
function   IsVirtualPath(PathName: String): Boolean;
function   GetPhysicalPath(PathName: String): String;

////////////////////////////////////////////////////////////////////////////////
//   Global variable
////////////////////////////////////////////////////////////////////////////////
var
  SymbolicLinks:    TSymbolicLinks;

implementation

function GetPhysicalPath(PathName: String): String;
var  szPath:        String;
     cDrive:        Char;
begin

  // Defaul result return
  result:=PathName;

  // Check virtual
  if (Length(PathName) > 0) and IsVirtualPath(PathName) then
  begin
     // Save path to local variable
     szPath:=PathName;
     // Recurse the mappings
     while (Length(szPath) > 0) and (szPath[1] in  ['A'..'Z', 'a'..'z']) do
     begin
        // Get drive letter
        cDrive:=szPath[1];
        // Check drive mapping
        if SymbolicLinks[cDrive].IsPath then
        begin
           // Get device path
           szPath:=SymbolicLinks[cDrive].DevicePath;
           // Delete the X: in the result with the actual path
           Delete(result, 1, 2);
           // Update result
           if (Length(result) = 0) then
              result:=ExcludeTrailingBackslash(szPath)
           else if (result[1] = '\') then
              result:=ExcludeTrailingBackslash(szPath)+result
           else
              result:=IncludeTrailingBackslash(szPath)+result;
        end
        else
           // No more paths to recurse
           SetLength(szPath, 0);
     end;
     // Update result so it looks correct
     result:=ExcludeTrailingBackslash(result);
  end;

end;

function IsVirtualPath(PathName: String): Boolean;
begin

  // Check file path
  if (Length(PathName) > 0) and ((GetFileAttributes(PChar(PathName)) and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY) then
  begin
     // Now extract the drive letter from the path
     if (PathName[1] in  ['A'..'Z', 'a'..'z']) then
        // Determine if there is a virtual path for the drive
        result:=SymbolicLinks[PathName[1]].IsPath
     else
        // Not a valid dive letter
        result:=False;
  end
  else
     // Not a valid path
     result:=False;

end;

The function IsVirtualPath takes a path and will tell you if it is a "virtual" path. The function GetPhysicalPath takes a path name and returns the physical path location for the passed virtual path. It is also able to iterate ALL mappings, take for example:

  // Create a bunch of links to "c:\windows" and below
  SymbolicLinks['p'].DevicePath:='c:\windows';
  SymbolicLinks['q'].DevicePath:='p:\system32';
  SymbolicLinks['r'].DevicePath:='q:\drivers';

  // Test that path to see if virtual
  if IsVirtualPath('r:\') then
  begin
     // Show message
     ShowMessage('Path is virtual');
     // Show real path
     ShowMessage(GetPhysicalPath('r:\'));
  end;

  // Get rid of mappings
  SymbolicLinks['p'].DevicePath:='';
  SymbolicLinks['q'].DevicePath:='';
  SymbolicLinks['r'].DevicePath:='';

This way, you can decide how you want to handle a case where a new virtual drive is being added, and the user is passing a path on another virtual drive.

eg

- Check IsVirtualPath and disallow the add
- Get the "real" path using GetPhysicalPath and allow the add (mapping it the real path)

-----

Hope this helps some,
Russell




0
 
LVL 1

Author Comment

by:fdehell
ID: 17152153
Hey Russel perfect, I wonder why Borland has not hired you yet for writing their helpfiles lol, they could use your help Im sure of that haha

Thanks...


Frank
0
 
LVL 1

Author Comment

by:fdehell
ID: 17152472
For completeness here is the procedure after I had altered the symbollink unit like Russel told me

procedure TMainForm.btnAddClick(Sender: TObject);
begin

  AddForm.Startup;
  if (AddForm.ShowModal = mrOK) and (AddForm.Drive > #0) then
    begin
      if IsVirtualPath(AddForm.txtPath.Text) then
      ShowMessage('There is little use pointing a virtual drive to a virtual drive') end
      else begin
    SymbolicLinks[AddForm.Drive].DevicePath:=AddForm.txtPath.Text;
     SymbolicLinks[AddForm.Drive].Persist(True);
     end;
 
  LoadVirtualDrives;

end;

again, thank you sooo much Russel
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now