Infinite loop?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Infinite loop?

rgo-2
Hi all,
my program probably goes into infinite loop... But i cannot understand where and why.

code:

import System.Directory

data MyFile = MyDir {
                dir_name :: String,
                dir_files :: [MyFile]
        }
        | MyFile {
                file_name :: String
        }

read_dir_entries :: [FilePath] -> IO [MyFile]
read_dir_entries [] = return []
read_dir_entries (name:names) = do
        isdir <- doesDirectoryExist name;
        entry <- if isdir
                        then read_dir name
                        else return (MyFile {file_name = name});
        entries <- read_dir_entries names;
        return (entry:entries)
       

read_dir :: FilePath -> IO MyFile
read_dir name = do
        content <- getDirectoryContents name;
        files <- read_dir_entries content;
        return MyDir {
                dir_name = name,
                dir_files = files
        }

instance Show MyFile where
        show (MyDir {dir_name = name, dir_files = files}) =
                "\n" ++ name ++ ":\n" ++ show files
        show (MyFile {file_name = name}) = "\t" ++ name ++ "\n"

main = do
        cwd <- getCurrentDirectory;
        dir <- read_dir cwd;
        print dir;
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Infinite loop?

Jon Fairbairn
On 2006-02-17 at 20:12GMT rgo wrote:
> Hi all,
> my program probably goes into infinite loop... But i cannot understand where and why.

getDirectoryContents will include "." and "..", so if you
follow those, you're bound to loop.

--
Jón Fairbairn                              Jon.Fairbairn at cl.cam.ac.uk


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Infinite loop?

Jared Updike
Yep. change one line to:

   entry <- if isdir && name /= "." && name /= ".."

and it does in fact work.

  Jared.

On 2/17/06, Jon Fairbairn <[hidden email]> wrote:

> On 2006-02-17 at 20:12GMT rgo wrote:
> > Hi all,
> > my program probably goes into infinite loop... But i cannot understand where and why.
>
> getDirectoryContents will include "." and "..", so if you
> follow those, you're bound to loop.
>
> --
> Jón Fairbairn                              Jon.Fairbairn at cl.cam.ac.uk
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


--
http://www.updike.org/~jared/
reverse ")-:"
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Infinite loop?

Ralf Hinze
In reply to this post by rgo-2
Am Freitag 17 Februar 2006 21:12 schrieb rgo:

> Hi all,
> my program probably goes into infinite loop... But i cannot understand where and why.
>
> code:
>
> import System.Directory
>
> data MyFile = MyDir {
>                 dir_name :: String,
>                 dir_files :: [MyFile]
>         }
>         | MyFile {
>                 file_name :: String
>         }
>
> read_dir_entries :: [FilePath] -> IO [MyFile]
> read_dir_entries [] = return []
> read_dir_entries (name:names) = do
>         isdir <- doesDirectoryExist name;
>         entry <- if isdir
>                         then read_dir name
>                         else return (MyFile {file_name = name});
>         entries <- read_dir_entries names;
>         return (entry:entries)
>        
>
> read_dir :: FilePath -> IO MyFile
> read_dir name = do
>         content <- getDirectoryContents name;
>         files <- read_dir_entries content;
>         return MyDir {
>                 dir_name = name,
>                 dir_files = files
>         }
>
> instance Show MyFile where
>         show (MyDir {dir_name = name, dir_files = files}) =
>                 "\n" ++ name ++ ":\n" ++ show files
>         show (MyFile {file_name = name}) = "\t" ++ name ++ "\n"
>
> main = do
>         cwd <- getCurrentDirectory;
>         dir <- read_dir cwd;
>         print dir;

Maybe because the file system is a graph not a tree (. and ..)?
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Infinite loop?

Jon Fairbairn
In reply to this post by Jared Updike
On 2006-02-17 at 09:22PST Jared Updike wrote:
> Yep. change one line to:
>
>    entry <- if isdir && name /= "." && name /= ".."
>
> and it does in fact work.

Only if no-one has been tricky with symbolic links.

--
Jón Fairbairn                              Jon.Fairbairn at cl.cam.ac.uk


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Infinite loop?

rgo-2
In reply to this post by Jon Fairbairn
On Fri, 17 Feb 2006 17:19:53 +0000
Jon Fairbairn <[hidden email]> wrote:

> On 2006-02-17 at 20:12GMT rgo wrote:
> > Hi all,
> > my program probably goes into infinite loop... But i cannot understand where and why.
>
> getDirectoryContents will include "." and "..", so if you
> follow those, you're bound to loop.
>
> --
> Jón Fairbairn                              Jon.Fairbairn at cl.cam.ac.uk
>
>

Thanks.
It's the third time in my life when do this error. :-[

here working code:

import System.Directory

data MyFile = MyDir {
                dir_name :: FilePath,
                dir_files :: [MyFile]
        }
        | MyFile {
                file_name :: FilePath
        }

read_dir_entries :: FilePath -> [FilePath] -> IO [MyFile]
read_dir_entries _ [] = return []
read_dir_entries dirname (name:names) = do
        isdir <- doesDirectoryExist name;
        entry <- if (isdir && (name /= ".") && (name /= ".."))
                        then read_dir (dirname ++ "/" ++ name)
                        else return (MyFile {file_name = name});
        entries <- read_dir_entries dirname names;
        return (entry:entries)

read_dir :: FilePath -> IO MyFile
read_dir name = do
        content <- getDirectoryContents name;
        files <- read_dir_entries name content;
        return MyDir {
                dir_name = name,
                dir_files = files
        }

instance Show MyFile where
        show (MyDir {dir_name = name, dir_files = files}) =
                "\n" ++ name ++ ":\n" ++ show files
        show (MyFile {file_name = name}) = "\t" ++ name ++ "\n"

main = do
        cwd <- getCurrentDirectory;
        dir <- read_dir cwd;
        print dir;
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Infinite loop?

Pierre Barbier de Reuille
In reply to this post by Jon Fairbairn
On Fri, 17 Feb 2006 17:26:08 +0000
Jon Fairbairn <[hidden email]> wrote:

> On 2006-02-17 at 09:22PST Jared Updike wrote:
> > Yep. change one line to:
> >
> >    entry <- if isdir && name /= "." && name /= ".."
> >
> > and it does in fact work.
>
> Only if no-one has been tricky with symbolic links.
>

And how can you, in Haskell, detect symbolic links ?

--
You have an unusual understanding of the problems of human
relationships.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Infinite loop?

Donn Cave-2
In reply to this post by rgo-2
Quoth Barbier de Reuille Pierre <[hidden email]>:
| On Fri, 17 Feb 2006 17:26:08 +0000
| Jon Fairbairn <[hidden email]> wrote:
|
| > On 2006-02-17 at 09:22PST Jared Updike wrote:
| > > Yep. change one line to:
| > >
| > >    entry <- if isdir && name /= "." && name /= ".."
| > >
| > > and it does in fact work.
| >
| > Only if no-one has been tricky with symbolic links.
| >
|
| And how can you, in Haskell, detect symbolic links ?

I refer you to the documentation in System.Posix.Files, especially
getFileStatus :: FilePath -> IO FileStatus, and
isSymbolicLink :: FileStatus -> Bool.  I don't think I've used this
function, but it looks promising.

        Donn Cave, [hidden email]
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Infinite loop?

Dmitry V'yal
In reply to this post by Pierre Barbier de Reuille
Barbier de Reuille Pierre wrote:
> And how can you, in Haskell, detect symbolic links ?
>
One can use System.Posix.Files.isSymbolicLink function for that purpose.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Infinite loop?

Mark T.B. Carroll-2
In reply to this post by Pierre Barbier de Reuille
Barbier de Reuille Pierre <[hidden email]> writes:
(snip)
> And how can you, in Haskell, detect symbolic links ?

I use System.Posix.Files.
getSymbolicLinkStatus is like lstat(2) under Unix.
It returns a value to which you can apply the
isSymbolicLink predicate.

Of course, you can't be sure the file won't change in between you
checking and you acting on that result.

-- Mark

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe