LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2010, 11:51 PM   #1
lesca
Member
 
Registered: Sep 2010
Posts: 58

Rep: Reputation: 0
Smile Is it possible to open a symlink (won't follow) and read data from it?


I try to use open(2) system call, and I find one relevant flag: O_NOFOLLOW. But it will only return -1 rather than the symlink's file descriptor.

Can anyone give me some tips?

Thanks!
 
Old 11-10-2010, 12:31 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
You can use lstat to determine if it is a symbolic link. If it is, then you can use readlink to read the contents.

The O_NOFOLLOW flag on open causes a deliberate failure (which is useful for programs that need to recurse through subdirectories).

Last edited by neonsignal; 11-10-2010 at 12:34 AM.
 
1 members found this post helpful.
Old 11-10-2010, 02:29 AM   #3
lesca
Member
 
Registered: Sep 2010
Posts: 58

Original Poster
Rep: Reputation: 0
But how can I read data from symlink itself?
 
Old 11-10-2010, 03:09 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
A symlink doesn't contain any data. It consists only of meta-data and is theoretically of size 0 bytes. Anything you need to know about the link can be gotten using 'lstat'.
 
1 members found this post helpful.
Old 11-10-2010, 04:40 AM   #5
lesca
Member
 
Registered: Sep 2010
Posts: 58

Original Poster
Rep: Reputation: 0
I see. But lstat only provide me with its state data.

And how can I know which file it links to?
I think I need to read its metadata, but which function can do this?

Last edited by lesca; 11-10-2010 at 04:42 AM.
 
Old 11-10-2010, 06:08 AM   #6
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by lesca View Post
But how can I read data from symlink itself?
See my first reply above, use readlink.

Quote:
Originally Posted by man 2 readlink
Name

readlink - read value of a symbolic link

Synopsis

#include <unistd.h>

ssize_t readlink(const char *path, char *buf, size_t bufsiz);

Description

readlink() places the contents of the symbolic link path in the buffer buf, which has size bufsiz. readlink() does not append a null byte to buf. It will truncate the contents (to a length of bufsiz characters), in case the buffer is too small to hold all of the contents.

Return Value

The call returns the count of characters placed in the buffer if it succeeds, or a -1 if an error occurs, placing the error code in errno.

...

Last edited by neonsignal; 11-10-2010 at 06:16 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
the get data from serial port function read() blocks when data not available DEF. Programming 3 11-17-2014 07:11 AM
[SOLVED] Serial port : Read data problem, not reading complete data anujmehta Linux - Networking 5 09-06-2010 06:10 AM
LXer: Open Data, Open Society: a research project about openness of public data in EU LXer Syndicated Linux News 0 06-02-2010 12:30 PM
Serial programming - binary data using open() and read() neutron001 Programming 5 04-07-2008 03:04 PM
Burn Data DVD... Read Data in Linux and Windows SaintStrive Linux - Newbie 3 09-18-2004 05:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration