link field for directories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers link field for directories
# 1  
Old 09-30-2006
link field for directories

Hi everybody .
I am a newbie in UNIX.

I understood the concept of unix system files .

For normal files , the link field is the number of hard links that points to this file.

But there is no hard links for directories so what is the meaning of link field tor those directories?

Thanks
# 2  
Old 09-30-2006
Directories do have hard links. Directories are required to contain a subdirectory called "." which is a link to itself. A directory may also contain a subdirectory. If it does, the subdirectory is required to contain, well, a "subdirectory" called ".." which is actually a link to the parent. Example:
Code:
$ mkdir -p a/b/c
$ ls -id a/b a/b/. a/b/c/..
30768 a/b       30768 a/b/.     30768 a/b/c/..

So a/b and a/b/. and a/b/c/.. are all the same directory.
# 3  
Old 09-30-2006
so the link field is the number of hard links that is the number of directories that points on it?
# 4  
Old 10-01-2006
No, the number of links is the number of links. You seem to understand that if I do:
touch file1
ln file1 file2
that I have one file with 2 links. Now extend that to directories. In the example I show above, a/b and a/b/. and a/b/c/.. are not three directories. They are three hard links to one directory.
# 5  
Old 10-01-2006
I am a little confused.

I got labs to prepare and the question is

5. Normal links cannot be done on directories. What is the meaning of the "link field" for directories ?

I would answer , the same than for normal files ..


But anyway , I understood what you said .
# 6  
Old 10-01-2006
Well first, it seems you breaking the rules which state:
(6) Do not post classroom or homework problems.

But this is either a trick question, or a case of ignorance on the part of the person who prepared the lab. So I am going to bend the rules a bit, and explain further.

To say "Normal links cannot be done on directories." comes close to be being true but it doesn't make it all the way. The link() system call usually will work on directories, but only for root. The Posix Standard says: "If path1 names a directory, link() shall fail unless the process has appropriate privileges and the implementation supports using link() on directories." So a Posix compliant OS may or may not choose to allow root to perform the operation, but it must prohibit ordinary users from linking to directories. In my opinion, the only valid reason that root would have to do this would be to restore a missing . or .. entry thus repairing some filesystem damage.

In the old days, mkdir was a suid executable which used the mknod() system call to create the requested directory and then immediately did 2 link() calls to populate it with the needed . and .. links. Later the mkdir() system call was born and now it should be used to create directories. It does all three operations atomically so the directory never appears to be in an invalid state to other programs. So it is now very rare and very unusual to perform a link() on a directory. And your OS may even prohibit it.

But every OS allows mkdir() and this system call is more or less required to create . and .. entries each directory that it makes. What is explicitly required is that opening . and .. will behave as if those links were present. That is, opening . must open the directory that contains it. And opening .. must open the parent directory. A link is a link whether it was made by link() or mkdir(). I could use mkdir() to create a normal directory. On at least some versions of Unix, I could then use unlink() to remove .. and then restore it via link(). Assuming I get it right, you could not examine the resulting directory to see if I did that or not. And many versions of Unix supply root-only commands called link and unlink that should be able to pull this off. I'm not going to test this though... I think it is very dangerous to fiddle around with a filesystem like this.

So I really must argue that a statement like "Normal links cannot be done on directories." is, at best, carelessly phrased.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

/var/adm/messages (insterface turned off/restored) and link up & link down message.

Hi All I am facing an issue with our new solaris machine. in /var/adm/messages Apr 22 16:43:05 Prod-App1 in.routed: interface net0 to 172.16.101.1 turned off Apr 22 16:43:33 Prod-App1 mac: NOTICE: nxge0 link up, 1000 Mbps, full duplex Apr 22 16:43:34 Prod-App1 mac: NOTICE: nxge0 link... (2 Replies)
Discussion started by: javeedkaleem
2 Replies

2. Solaris

/var/adm/messages (interface turned off/restored) and link up & link down message.

Hi All I am facing an issue with our new solaris machine. in /var/adm/messages root@Prod-App1:/var/tmp# root@Prod-App1:/var/tmp# root@Prod-App1:/var/tmp# cat /var/adm//messages Apr 20 03:10:01 Prod-App1 syslogd: line 25: WARNING: loghost could not be resolved Apr 20 08:24:18 Prod-App1... (0 Replies)
Discussion started by: javeedkaleem
0 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Symbolic link not allowed or link target not accessible

Hi everybody, I read about treads realted to this issue but they did not resovle issue given below. Please help me resolve issue given below I have html file under /srv/www/htdocs/actual_folder ls actual_folder/ test.html and following link works... (0 Replies)
Discussion started by: newbielgn
0 Replies

4. Programming

g++ fails to link to static library when compilation and link in single command

Hello All, I've encountered a strange behaviour from g++ that doesn't make sense to me. Maybe you can shed some light on it: I have a bunch of source files and want to compile them and link them with a static library liba.a located in /usr/local/lib64 into an executable Approach 1 works... (0 Replies)
Discussion started by: magelord
0 Replies

5. HP-UX

Symbolic link creation between 2 directories on different machines

I am trying to link 2 directories using: ln -s /net/<hostname>/srcdir/ /net/<desthostname>/dstdir/ I get the following error: ln: /net/<desthostname>/dstdir: Function is not available What is it that i am trying to do wrongly in the above operation? Please use next time code tags (3 Replies)
Discussion started by: Amit Kulkarni
3 Replies

6. AIX

How to Link FS to 2 directories?

Hi Experts, How do i link FS /lhome to 2 directories /u and /home . Please advise. Many Thanks, RG (6 Replies)
Discussion started by: EngnrRG
6 Replies

7. UNIX for Dummies Questions & Answers

How two directories are linked (not symbolic link)

Hi, I'm trying to understand a "strange" setup: on one of the systems, when I cd into a directory, /data/logs, it takes me into /data/uat/logs (instead of /data/logs). These two directories are not symbolicly linked. Here's what I have: In /data, "ls -li" shows (among other things): ... (6 Replies)
Discussion started by: chenghui
6 Replies

8. UNIX for Dummies Questions & Answers

Difference between hard link and soft link

Hi Experts, Please help me out to find out difference between a hard link and a soft link. I am new in unix plz help me with some example commands ( for creating such links). Regards S.Kamakshi :) (2 Replies)
Discussion started by: kamakshi s
2 Replies

9. UNIX for Dummies Questions & Answers

Difference between hard link and soft link in unix

Hi All, Can any one please explain me what is the difference between hard link and soft link in UNIX. Thanks in advance Raja Chokalingam. (2 Replies)
Discussion started by: RAJACHOKALINGAM
2 Replies

10. UNIX for Advanced & Expert Users

Differences between hard link and soft link

Hi all! I'd like to know the differences between hard links and soft links. I've already read the ln manpage, but i'm not quite sure of what i understood. Does a hard link sort of copy the file to a new name, give it the same inode number and same rights? What exactly should I do to do this:... (3 Replies)
Discussion started by: penguin-friend
3 Replies
Login or Register to Ask a Question