2

I have a question regarding the link() function in C.

I am using it to create a hard link to a file on a Unix system.

First, in my home directory (the same problem happens in other directories) I create a test directory and go into it (cd). Now inside it, I execute a program that creates a hard link to a file in the .. directory (the parent directory). But for some reason that fails with the errno set on 2 (ENOENT). The file exists -> its name is ../Exercise3.c (the name is Exercise3.c but it resides in the .. directory). And for some reason creating links to files like ../scr.c and others works, but I cannot create a link to that file. It has all the same privileges as other text files in that directory.

I have tried debugging with deleting the file and recreating it (it did not work), printing the name of the file in the program to see if the code failed somehow, but it does not.

The strange thing is that if I stay in the parent directory, I can create the link to that file, but as soon as I go to the test directory I created and try to create a link to it (the file in question) with the path name ../Exercise3.c it fails. Again ../scr.c creates a link.

int status = link(dest, name);
if (status == 0)
{
    exitStatus = 0; 
}
else
{
    exitStatus = errno;
    printf("Failed: %s\n", dest);
    perror("Creating hard link: "); 
}

This is the function that creates the hard link. char *dest is the ../Exercise.c file and char *name is the hard link name. And the hard link name or the dest doesn't point to an empty string.

Rachid K.
  • 4,490
  • 3
  • 11
  • 30
  • It's not clear from your excerpt, but you may have the arguments to `link` reversed. The first argument should be the old, existing path (the one with `..` in it). The second argument should be the path of the new link you wish to create. – Tom Karzes Jan 16 '16 at 10:44
  • What seems to be the case is that it has something to do with the length of a file name. Trying to create a link to a file "../tttttttt.c" fail, but "../ttttt.c" succeds. – Mysterious Man Jan 16 '16 at 10:45
  • Thank you for your answer Tom. But the arguments are passed in right, dest is the old file and name is the link name. – Mysterious Man Jan 16 '16 at 10:46
  • Maybe your dest or name have garbage values. You should probide a complete example. – hyde Jan 16 '16 at 10:46
  • Thanks for the answer hyde. I have tryed printing the names and both seem to be fine and normal. – Mysterious Man Jan 16 '16 at 10:47
  • Please consider writing point-wise description. Long paragraph brings readability issue - Unless until it is a theoretical question. – Shivendra Mishra Jan 16 '16 at 11:52

0 Answers0