Possible RTFM question: is there something like an IsRootPath() function?

classic Classic list List threaded Threaded
23 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Possible RTFM question: is there something like an IsRootPath() function?

Bart-48
Hi,

Does fpc have a function that determines if a given paths is the root
(e.g. '/' in Linux, 'F:\' in Windows)?
I could not find such a thing.

The Delphi converter from Lazarus needs such a function, and currently
I wrote a crude one which is ifdef-ed in the source code, which we
would like to avoid if possible.
(See: http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/converter/convertdelphi.pas?root=lazarus&r1=38276&r2=38275&pathrev=38276
)

Bart
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Possible RTFM question: is there something like an IsRootPath() function?

michael.vancanneyt


On Mon, 20 Aug 2012, Bart wrote:

> Hi,
>
> Does fpc have a function that determines if a given paths is the root
> (e.g. '/' in Linux, 'F:\' in Windows)?
> I could not find such a thing.

There is no such function.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Possible RTFM question: is there something like an IsRootPath() function?

Bart-48
On 8/20/12, [hidden email] <[hidden email]> wrote:

> There is no such function.

Thanks for the quick response.

I ended up with this:

  function IsRootPath(APath: String): Boolean;
  //crude function, it maybe needs support for UNC drives
  var
    D: String;
    Len: Integer;
  begin
    D := ExtractFileDrive(APath);
    Len := Length(D);
    System.Delete(APath, 1, Len);
    Result := (Length(APath) = 1) and (APath[1] in AllowDirectorySeparators);
  end;

Bart
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Possible RTFM question: is there something like an IsRootPath() function?

Jürgen Hestermann
In reply to this post by Bart-48
Am 2012-08-20 00:17, schrieb Bart:
 > Does fpc have a function that determines if a given paths is the root
 > (e.g. '/' in Linux, 'F:\' in Windows)?

The question is: For what reason is this function needed? If you use "subst" on Windows you can make every path to a root path if you want.


 > The Delphi converter from Lazarus needs such a function,

Why?

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Possible RTFM question: is there something like anIsRootPath() function?

Ludo Brands
In reply to this post by Bart-48
> I ended up with this:
>
>   function IsRootPath(APath: String): Boolean;
>   //crude function, it maybe needs support for UNC drives
>   var
>     D: String;
>     Len: Integer;
>   begin
>     D := ExtractFileDrive(APath);
>     Len := Length(D);
>     System.Delete(APath, 1, Len);
>     Result := (Length(APath) = 1) and (APath[1] in
> AllowDirectorySeparators);
>   end;
>

That doesn't work for // and c:\.\ and many more combinations that all end
up at the root. It doesn't work for relative paths either.

Ludo

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Possible RTFM question: is there something like an IsRootPath() function?

Sven Barth-2
In reply to this post by Jürgen Hestermann
Am 20.08.2012 14:05, schrieb Jürgen Hestermann:
> Am 2012-08-20 00:17, schrieb Bart:
>  > Does fpc have a function that determines if a given paths is the root
>  > (e.g. '/' in Linux, 'F:\' in Windows)?
>
> The question is: For what reason is this function needed? If you use
> "subst" on Windows you can make every path to a root path if you want.

Maybe because he wants to determine whether a path is absolute or not?
So that he can e.g. convert an absolute path to a relative one so he can
save this in a configuration in a more "portable" way. "subst" (or
adding the path to DosDevices in the registry which is the way I prefer
for persistent substing ;) ) might be a completely wrong solution or a
solution where you shoot cannonballs on little birds...

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Possible RTFM question: is there something like an IsRootPath() function?

Jürgen Hestermann
Am 2012-08-20 14:38, schrieb Sven Barth:
 > Am 20.08.2012 14:05, schrieb Jürgen Hestermann:
 >> The question is: For what reason is this function needed? If you use
 >> "subst" on Windows you can make every path to a root path if you want.
 >
 > Maybe because he wants to determine whether a path is absolute or not?

Absolute? You mean in contrast to relative? Then you only need to look for . or .. in parts of the path.

If you mean that it is the root of a disk then you cannot be sure by looking at the path because as mentioned with subst you can make any path to be a root path. If Z:\ points to c:\data\whatever then of what use is it to know that Z:\ is a root path?


 > So that he can e.g. convert an absolute path to a relative one so he can save this in a configuration in a more "portable" way.

For this purpose functions already exist (ExpandPath). But there is no need to know about root or not if you use them.


 > "subst" (or adding the path to DosDevices in the registry which is the way I prefer for persistent substing ;) ) might be a completely wrong solution or a > solution where you shoot cannonballs on little birds...

I did not write that subst should be a solution to anything. It was just an example that any path can be made a root path with subst.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Possible RTFM question: is there something like an IsRootPath() function?

Mattias Gaertner
In reply to this post by Sven Barth-2
On Mon, 20 Aug 2012 14:38:05 +0200
Sven Barth <[hidden email]> wrote:

> Am 20.08.2012 14:05, schrieb Jürgen Hestermann:
> > Am 2012-08-20 00:17, schrieb Bart:
> >  > Does fpc have a function that determines if a given paths is the root
> >  > (e.g. '/' in Linux, 'F:\' in Windows)?
> >
> > The question is: For what reason is this function needed? If you use
> > "subst" on Windows you can make every path to a root path if you want.
>
> Maybe because he wants to determine whether a path is absolute or not?

No, for this there is FilenameIsAbsolute in LazUtils.

It is about scanning the directory. Juha needs a simple
heuristic if the converter should search the parent directory for
related files.
Apparently it is a bad idea to scan a root directory.

See
http://bugs.freepascal.org/view.php?id=22630


> So that he can e.g. convert an absolute path to a relative one so he can
> save this in a configuration in a more "portable" way. "subst" (or
> adding the path to DosDevices in the registry which is the way I prefer
> for persistent substing ;) ) might be a completely wrong solution or a
> solution where you shoot cannonballs on little birds...


Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Searching for files all over the disk

Jürgen Hestermann

Am 2012-08-20 16:28, schrieb Mattias Gaertner:
> Apparently it is a bad idea to scan a root directory. See http://bugs.freepascal.org/view.php?id=22630

Well, it is a bad idea to wildly scan directory branches "just in case" at all.
That's fooling so many people and there is no good reason for doing so.
Only paths specified by the user (i.e. in config) should be searched.
This bug reports makes it totaly obvious.
Beeing too smart is never a good idea (for a program).
The only outcome of such things is that users don't understand what is happening.
I would not want any program to heavily scan my disk(s) and pick up any file that seems
to fit for whatever then doing things to (or with) it I cannot control anymore.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Searching for files all over the disk

Reinier Olislagers
On 20-8-2012 16:47, Jürgen Hestermann wrote:

>
> Am 2012-08-20 16:28, schrieb Mattias Gaertner:
>> Apparently it is a bad idea to scan a root directory. See
>> http://bugs.freepascal.org/view.php?id=22630
>
> Well, it is a bad idea to wildly scan directory branches "just in case"
> at all.
> That's fooling so many people and there is no good reason for doing so.
> Only paths specified by the user (i.e. in config) should be searched.
> This bug reports makes it totaly obvious.
> Beeing too smart is never a good idea (for a program).
> The only outcome of such things is that users don't understand what is
> happening.
> I would not want any program to heavily scan my disk(s) and pick up any
> file that seems
> to fit for whatever then doing things to (or with) it I cannot control
> anymore.

Then it is perhaps time for you to put your money where your mouth is
and submit a "proper" patch?
I myself have been quite happy with the Delphi converter... and I would
love to see it improved.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Possible RTFM question: is there something like an IsRootPath() function?

Tomas Hajny-2
In reply to this post by Mattias Gaertner
On Mon, August 20, 2012 16:28, Mattias Gaertner wrote:

> On Mon, 20 Aug 2012 14:38:05 +0200
> Sven Barth <[hidden email]> wrote:
>
>> Am 20.08.2012 14:05, schrieb JĂźrgen Hestermann:
>> > Am 2012-08-20 00:17, schrieb Bart:
>> >  > Does fpc have a function that determines if a given paths is the
>> root
>> >  > (e.g. '/' in Linux, 'F:\' in Windows)?
>> >
>> > The question is: For what reason is this function needed? If you use
>> > "subst" on Windows you can make every path to a root path if you want.
>>
>> Maybe because he wants to determine whether a path is absolute or not?
>
> No, for this there is FilenameIsAbsolute in LazUtils.
>
> It is about scanning the directory. Juha needs a simple
> heuristic if the converter should search the parent directory for
> related files.
> Apparently it is a bad idea to scan a root directory.
>
> See
> http://bugs.freepascal.org/view.php?id=22630

If that is the original problem, you could use the following check:

if ExpandFileName (APath+'..'+DirectorySeparator) <> ExpandFileName
(APath) then ...

This is because MS Windows API happily accepts paths containing references
to supposed parent directory of a root directory and silently ignores that
part of the path specification. We do the same in ExpandFileName in order
to remain Delphi compatible (because Delphi apparently relies on a Win32
API call for ExpandFileName whereas FPC has its own cross-platform
implementation). DirectoryExists for 'c:\..\' returns true under Win32 for
the same reason.

Tomas


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Possible RTFM question: is there something like an IsRootPath() function?

Bart-48
On 8/20/12, Tomas Hajny <[hidden email]> wrote:

> If that is the original problem, you could use the following check:
>
> if ExpandFileName (APath+'..'+DirectorySeparator) <> ExpandFileName
> (APath) then ...
[snip]
> implementation). DirectoryExists for 'c:\..\' returns true under Win32 for
> the same reason.

The supplied path in this scenario (Delphi converter) already is
processed for any .. in it by TrimFileName() (FileProcs unit from
Lazarus CodeTools).

And yes, it will not detect a substituted drive or a network drive,
but in the context of the original problem, this is not really a
problem.

[quote]
Your program is in directory C:\Bug4, right?
Could you try moving it one level down in directory hierarchy, for
example to C:\SW\Bug4 or whatever.

The converter scans in a background thread all directories under the
converted project's parent directory, searching for pascal source
files. Many projects have library sources organized that way.
Obviously it creates a problem if your project is just under C:\ root.
[/quote]

For this the current solution will do for now.
Both Juha and me were just wondering if any such function already existed.

Given your comments, I can see it's unlikely we will have such a
general function, given all the possible exceptions mentioned above.

Thanks everybody for your input.

Bart
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Searching for files all over the disk

Bart-48
In reply to this post by Jürgen Hestermann
On 8/20/12, Jürgen Hestermann <[hidden email]> wrote:

> Well, it is a bad idea to wildly scan directory branches "just in case" at
> all.

In the context of the converter it is not a "just in case" scenario.

My Delphi projects are located like this:

F:\Delphi Projecten //all my projects reside in a subfolder of this
F:\Delphi Projecten\Mijn Lib   //general prupose units
F:\Delphi Projecten\PasParser
F:\Delphi Projecten\Endurance Calculator
F:\Delphi Projecten\HtmlEdit
F:\Delphi Projecten\etc.

So in order to find the nldlg32 unit that the PasParser project uses,
the converter has to look in my "Mijn Lib" folder.

As Juha has pointed out, this is a general way projects are organized.
Hence this particular search strategy.
Obviously it needed some limitation, and we have implemented that
(crude as it may be).

> That's fooling so many people and there is no good reason for doing so.

As explained above, there is.

> Only paths specified by the user (i.e. in config) should be searched.

A valid suggestion on how to improve the converter!

Bart
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Searching for files all over the disk

Martin Frb
On 20/08/2012 23:00, Bart wrote:

> On 8/20/12, Jürgen Hestermann <[hidden email]> wrote:
>
>> Well, it is a bad idea to wildly scan directory branches "just in case" at
>> all.
> In the context of the converter it is not a "just in case" scenario.
>
> My Delphi projects are located like this:
>
> F:\Delphi Projecten //all my projects reside in a subfolder of this
> F:\Delphi Projecten\Mijn Lib   //general prupose units
> F:\Delphi Projecten\PasParser
> F:\Delphi Projecten\Endurance Calculator
> F:\Delphi Projecten\HtmlEdit
> F:\Delphi Projecten\etc.
>
> So in order to find the nldlg32 unit that the PasParser project uses,
> the converter has to look in my "Mijn Lib" folder.
>
> As Juha has pointed out, this is a general way projects are organized.
> Hence this particular search strategy.
>

Well I am not myself a user of the converter.
But even if files are organized like this, then there may be several
versions of the  "Mijn Lib" (or one of the units that should be found
there, is also in another folder). Then the converter can't know...

IMHO, if should ask which additional path to scan, for required units.
The parent dir can be set in the options, then it can be configured once
and for all.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Searching for files all over the disk

Juha Manninen
In reply to this post by Jürgen Hestermann
On Mon, Aug 20, 2012 at 5:47 PM, Jürgen Hestermann <[hidden email]> wrote:
Well, it is a bad idea to wildly scan directory branches "just in case" at all.
That's fooling so many people and there is no good reason for doing so.
Only paths specified by the user (i.e. in config) should be searched.
This bug reports makes it totaly obvious.
Beeing too smart is never a good idea (for a program).
The only outcome of such things is that users don't understand what is happening.
I would not want any program to heavily scan my disk(s) and pick up any file that seems
to fit for whatever then doing things to (or with) it I cannot control anymore.

Which people exactly are getting fooled?

Lots of heuristics and guessing is involved when converting a random Delphi project.
Most projects still cannot be fully converted but the success rate can be increased with many tricks.
Scanning the parent directory improves it a lot!
The converter is not "too smart", it is rather still "too dummy" as it cannot convert all projects.

About scanning "only paths specified by the user", do you really mean the user should search for pascal sources in a to-be-converted Delphi project directories, then type the directory names into a config file and then feed that file to the converter's GUI?
IMO that is a job for the converter because its whole purpose is to automate the task. You can still do it manually if you like of course.

Did you ever try converting a non-trivial Delphi project? Try it and you get an idea of the challenges.


Regards,
Juha


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Searching for files all over the disk

Juha Manninen
In reply to this post by Martin Frb
On Tue, Aug 21, 2012 at 1:28 AM, Martin <[hidden email]> wrote:
Well I am not myself a user of the converter.
But even if files are organized like this, then there may be several versions of the  "Mijn Lib" (or one of the units that should be found there, is also in another folder). Then the converter can't know...

IMHO, if should ask which additional path to scan, for required units. The parent dir can be set in the options, then it can be configured once and for all.

The converter is clever enough to ask from user what to do with an unknown unit name.
It can be commented out or the user can search and select its location.
However, scanning the parent directory makes the number of "stupid" questions lower as the obvious locations are already found.

Lazarus source editor and debugger must work accurately but Delphi conversion is all about heuristics. :)


Juha


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Searching for files all over the disk

Martin Frb
In reply to this post by Juha Manninen
On 20/08/2012 23:55, Juha Manninen wrote:
> About scanning "only paths specified by the user", do you really mean
> the user should search for pascal sources in a to-be-converted Delphi
> project directories, then type the directory names into a config file
> and then feed that file to the converter's GUI?
> IMO that is a job for the converter because its whole purpose is to
> automate the task. You can still do it manually if you like of course.

But it could ask about certain assumptions. Like "Should I scan the
parent dir", No, yes this time, yes always

Checking the root path may not be good enough. On linux you have
symbolic links. On Win you have NTFS mount-points: Any partition can
have zero or more drive letters, and zero or more mount points. That
means that C:\ may also be found under N:\Projects\DriveC\. And then
Projects could be in N:\Projects. Searching N:\Projects would obviously
be to much again.

So it is very hard to tell how data is organized. Further, I have not
tested, but it may well be possible to create circular mounts....

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Searching for files all over the disk

Martin Frb
In reply to this post by Juha Manninen
On 21/08/2012 00:09, Juha Manninen wrote:
> The converter is clever enough to ask from user what to do with an
> unknown unit name.
> It can be commented out or the user can search and select its location.
> However, scanning the parent directory makes the number of "stupid"
> questions lower as the obvious locations are already found.

If the user can answer "Yes always" then this is a one time ask question.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Searching for files all over the disk

Juha Manninen
On Tue, Aug 21, 2012 at 2:11 AM, Martin <[hidden email]> wrote:
On 21/08/2012 00:09, Juha Manninen wrote:
The converter is clever enough to ask from user what to do with an unknown unit name.
It can be commented out or the user can search and select its location.
However, scanning the parent directory makes the number of "stupid" questions lower as the obvious locations are already found.

If the user can answer "Yes always" then this is a one time ask question.

True. It can also be a persistent option (checkbox) in the settings form which always opens before conversion.
I will add it.

Juha


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Searching for files all over the disk

Jürgen Hestermann
In reply to this post by Bart-48
Am 2012-08-21 00:00, schrieb Bart:
 > My Delphi projects are located like this:
 > ...
 > As Juha has pointed out, this is a general way projects are organized.

Realy? How do you know? That's just an assumption.


But my complain was more general about an increasing use of branch scanning (not only in the Delphi converter).
It has become common practice to ransack each and every directory on disk in many programs based
on more or less justified assumptions.

It already starts with the Windows explorer which opens all zip files in a directory to present
them as directories. If you only come along this directory while navigating to a
deeper level you get a long delay.

The same happens when programs look in all directories of the path environment.
This has annoyed me since decades because each and every program adds directories to this variable
and all these programs then search *all* these paths in case something is not found.
This is not only time consuming but it also happens that *some* version
of a needed file is found and used but this may be the wrong one (i.e. config files).

Why not ask the user where to find a file (or many files) if they are not found where expected?
I think it is not a good idea to scan through branches that no user has said to scan.

If a file is found (and used) that was just a copy in some subdirectory with wrong data in it
then the results can be wrong but the user does not know the reason because he does not know
which files were used. If the user has to specify a (starting) directory he knows that
1.) a scan is done
2.) what is scanned

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
12