Solved

questions

Posted on 2011-03-10
13
307 Views
Last Modified: 2012-05-11
Hi all,
I was asked to write a directory traversal program in c (using unix). I know directory traversal is something like a recursive directory tree that finds the path and name, am i right? Can someone explain to me in more details as in what is this used for and what other system calls (other than opendir, readdir, chdir) should i include in my program?
Any help will be greatly appreciated.
0
Comment
Question by:crazy4s
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 14

Expert Comment

by:sjm_ee
Comment Utility
I would interpret "directory traversal" to mean to "visiting" a named directory and each sub-directory recursively and perform some function on each directory. The function may be to print the name of the directory but it could be something else. For example, consider the command "find DIR -type d -exec COMMAND {} \;" which will execute COMMAND against every directory.under DIR.

From this perspective, you have the routines that you need as long as the called "function" can operate on the data passed to it.
0
 

Author Comment

by:crazy4s
Comment Utility
so do you meant that we can print in the tabbed format names of all files in specified directories all subdir tabbed by depth?
and i remembered that we should include getcwd in our program too, if we're to use this system call in my prog is this able to print the current directory and sub directories in it and also check whether the sub directories is a directory instead a file and if is a directory we'll continue to print out the subdir of the particular directory?
and one more thing what does the macro IS_DIR used in this prog? do we need this?
and in your given example, is that a command line, i'm not really understand?
sorry for asking so many questions but i'm really slow in catching up stuffs, hope you understand.
thank you.
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
Comment Utility
>> Can someone explain to me in more details as in what is this used for
The  ls -lR  command is an example command which lists all the entities in all folders under the current directory

What order are you expected to traverse the directory? If optional, you could consider emulating the  ls -R  command, but it may be easier for you to write a recursive function that produces a different ordering.

A directory has a number of entities in them, files, folders, links. You need to distinguish between a folder and a file. See Listing 5 in this link:
     http://www.ibm.com/developerworks/aix/library/au-unix-readdir.html

>> I know directory traversal is something like a recursive directory tree that finds the path and name, am i right?
   I think of path as a full path that includes all the folders down to the entity being examined. You could display this tree this way, but I would think that you could leave out the path and just indicate the entity name with the path being implicit from the parent node. Including an indicator at the end of the name if it represents a folder would be a nice touch.

   If you need to list more than folders, then your nodes probably should have an attribute indicating the type of entity (e.g., folder, file, symbolic link).
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
>> and one more thing what does the macro IS_DIR used in this prog? do we need this?
    In the listing 5 is the macro S_ISDIR.

Printing the tree in the form of a tree can get complicated and should be asked in a different question. Printing the tree like ls -R prints it is a bit easier.
0
 

Author Comment

by:crazy4s
Comment Utility
well at first i'm not sure what do but now i think he wants us to do recursive as i remembered he told us to put something like chdir("..") at the end of the loop, and that means back to the parent directory, am i correct?
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
.. is the parent folder.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:crazy4s
Comment Utility
i read the link that you provided earlier, i roughly understand what it is about but i'm not quite sure for some of them.
what is a symbolic links, can you provide some example? and for the lstat, is this used to determine whether it is a dir or a reg file and hence collect the information from it and the print it out?
0
 

Author Comment

by:crazy4s
Comment Utility
so a recursive directory traversal prog is an equivalent for the ls -R command,  they'll have the same output?
but i don't really understand why do we need to back to the parent folder (the chdir(".."))? Is it because the files in this directory might be a directory that's why we need to do that again and print out the files in this subdir?
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 500 total points
Comment Utility
Firstly, if this question is related to security, as inferred by this link
     http://en.wikipedia.org/wiki/Directory_traversal
then I misunderstood your assignment.

Assuming not related to security and you just want to be able to list all entities that exist under a folder, I'll continue.

If you do not know about symbolic links, then apparently it is not part of your assignment, so you can replace lstat with stat. Let's not try to build Rome in a day. Just test for folders vs. regular files.

You have been advised to go back up the to the parent folder using .. This suggests that if your tree looks like:
             A
   B         C         D
E    F             G   H   I

Open in new window

then, after you complete processing the B tree, then you need to process the C and D trees. But to get to these two trees, you need to go back up to the parent folder A, and then drop into C and the up again to A and then down to D.

What I think you should take out of Listing 5 in this link:
     http://www.ibm.com/developerworks/aix/library/au-unix-readdir.html
is the usage of the S_ISDIR and S_ISREG macros. Bear in mind that your system may have other techniques for this functionality. You should start coding and see whether these macros are defined.

===

As you know, there are different ways to traverse and print the nodes in a tree.
     http://en.wikipedia.org/wiki/Tree_traversal
This link uses binary trees as an example, which is useful for understanding orderings. Just apply the ideas to your needs.

There are three separate aspects to this question.
1) How do I recursively collect the data distinguishing between files and folders?
2) How do I save this data in a data structure?
3) How do I traverse the data structure to produce the output.

I am only dealing with question (1). Once you can collect the data, then try to figure out what kind of data structure you need knowing the end-goal is question (3).

Create a simple directory tree where each folder has three subfolders to, say, a max depth of three, and go through it by hand using what you know. Then try to write the program to do what you do by hand.
0
 
LVL 8

Expert Comment

by:ssnkumar
Comment Utility
Are you sure, the word directory in "Directory Traversal" refers to a directory or a folder?
I feel that, it could be something like searching a "Telephone Directory".
If so, then you should use the "Divide and Conquer" strategy.
0
 
LVL 2

Expert Comment

by:preraksheth
Comment Utility
directory traversal means going thru each file/dir in the given directory, up to the leaf level. That is, if there is a directory within a directory, traverse recursively in that.

You should be doing a recursive program, with a template similar to this

function to go thru each file in the directory
    if this is a file, print its name (or anything else that you need to do)
    if this is a directory, call the same function again with the directory

The above will recursively go thru each file within the given directory and act as you require
0
 

Author Comment

by:crazy4s
Comment Utility
i'm not too sure what's symbolic links but during his lecture he did used lstat to check the mode so i assumed that we've to use lstat instead of stat.
okay i get the recursive meaning here.
i'll start my codings now thanks for all the help!
0
 
LVL 8

Expert Comment

by:ssnkumar
Comment Utility
lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.
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

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

763 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

12 Experts available now in Live!

Get 1:1 Help Now