How to Tell If a Symbolic Link Exists at a Certain Path

How can I tell if a symbolic link exists at a certain path?

Here's a simpler way: Fondation/FileManger/FileWrapper

let node = try FileWrapper(url: URL(fileURLWithPath: "/PATH/file.link"), options: .immediate)

node.isDirectory >> false
node.isRegularFile >> false
node.isSymbolicLink >> true

Bash: Find which symbolic links exists to a specific file

There is no reverse search. You have to do the work yourself. Get a list of all the symbolic links in your system and check which of them point to the file you are interested in.

How to check if a given parameter is a symlink/soft link? in bash

According to docs Bash-Beginners-Guide:

[ -h FILE ] True if FILE exists and is a symbolic link.

How to check if a symlink exists

-L returns true if the "file" exists and is a symbolic link (the linked file may or may not exist). You want -f (returns true if file exists and is a regular file) or maybe just -e (returns true if file exists regardless of type).

According to the GNU manpage, -h is identical to -L, but according to the BSD manpage, it should not be used:

-h file True if file exists and is a symbolic link. This operator is retained for compatibility with previous versions of this program. Do not rely on its existence; use -L instead.

How to check if soft link exists or not

Use lstat - get symbolic link status:

The lstat() function shall be equivalent to stat(), except when path refers to a symbolic link. In that case lstat() shall return information about the link, while stat() shall return information about the file the link references.

(Emphasis mine.)

lstat will return non-zero, and errno will be set to ENOENT if the link (or any other part of the path) does not exist.

Example:

#include <stdio.h>
#include <stdbool.h>
#include <sys/stat.h>

bool symlink_exists(const char* path)
{
struct stat buf;
int result;

result = lstat(path, &buf);

return (result == 0);
}

void test(const char* path)
{
bool exists = symlink_exists(path);

printf("%s does%s exist.\n", path, exists ? "" : " not");
}

int main(int argc, char** argv)
{
test("/bin/sh");
test("/etc/no_such_thing");
return 0;
}

Output:

/bin/sh does exist.
/etc/no_such_thing does not exist.

How to check whether the target of a directory symbolic link exists?

I could use CreateFile for symbolic file to get the file handle and
then check the file is exist or not.

If FILE_FLAG_OPEN_REPARSE_POINT is not specified in call CreateFile and you get file handle this mean that symbolic link/mount point target is exist. so already not need check something. if call fail because target not exist last error will be ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND (may be also ERROR_BAD_PATHNAME)

about FILE_FLAG_BACKUP_SEMANTICS - this is very bad design of CreateFile api. this api internal call NtCreateFile. the FILE_FLAG_BACKUP_SEMANTICS mapped to FILE_OPEN_FOR_BACKUP_INTENT CreateOptions flag. this flag checked inside IopCheckBackupRestorePrivilege

This function will determine if the caller is asking for any accesses
that may be satisfied by Backup or Restore privileges, and if so,
perform the privilege checks. If the privilege checks succeed, then
the appropriate bits will be moved out of the RemainingDesiredAccess
field in the AccessState structure and placed into the
PreviouslyGrantedAccess field.

Note that access is not denied if the caller does not have either or both of the privileges, since he may be granted the desired access via
the security descriptor on the object.

so even if caller have not either or both of the Backup or Restore privileges this not create problems.

but NtCreateFile have the next 2 options: FILE_DIRECTORY_FILE and FILE_NON_DIRECTORY_FILE - this let specify are we want create/open file or directory. if we (potential) create new item - we need specify are we want create directory (FILE_DIRECTORY_FILE must be set) or not directory (FILE_NON_DIRECTORY_FILE, but by default assume this case - so optional). when we open file - both this flags is optional - if we not specify both - this mean that we not care are we open file or directory. if we care about this - need specify one of this flags.

but if look to CreateFile visible that not exist option which explicity mapped to FILE_DIRECTORY_FILE or FILE_NON_DIRECTORY_FILE. the CreateFile use for this .. FILE_FLAG_BACKUP_SEMANTICS option. this is very not logical from my view, but as is. when FILE_FLAG_BACKUP_SEMANTICS not set CreateFile pass FILE_NON_DIRECTORY_FILE option for NtCreateFile. when it set - pass FILE_OPEN_FOR_BACKUP_INTENT and not pass FILE_NON_DIRECTORY_FILE. this allow you open file or directory. and no option for set FILE_DIRECTORY_FILE - because this CreatrFile can not create new directory.

so instead have separate option for FILE_DIRECTORY_FILE and FILE_NON_DIRECTORY_FILE, CreateFile abuse FILE_FLAG_BACKUP_SEMANTICS wich have double sense here

how to check if a path is actual or symbolic link

Check out the lstat() function , you need to use S_ISLNK on the st_mode field.

How to determine if sym link exists

According to the TEST(1) manpage:

   -h FILE
FILE exists and is a symbolic link (same as -L)
-L FILE
FILE exists and is a symbolic link (same as -h)

-h or -L should do the trick. However, you are not testing against the variable $VHOST but the literal VHOST. That's the error.

So, I suppose you meant to say:

if [ ! -L "$VHOST" ]; then
...

Additionally you're forgetting a $ in ln -s:

ln -s /home/user/Ubuntu\ One/htdocs/vhosts/vhost.local "$VHOST";

How to test whether a path is a symbolic link?

unix('test -L slowlink.m')

Returns 1 if the file either does not exist or is not a link, 0 if it is a link (backwards, I know).



Related Topics



Leave a reply



Submit