mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Clean up CreateSymLink()
This commit is contained in:
@@ -280,10 +280,10 @@ namespace System.Management.Automation
|
||||
return null;
|
||||
}
|
||||
|
||||
internal static bool NonWindowsCreateSymbolicLink(string path, string strTargetPath, bool isDirectory)
|
||||
internal static bool NonWindowsCreateSymbolicLink(string path, string target)
|
||||
{
|
||||
// Linux doesn't care if target is a directory or not
|
||||
return Unix.CreateSymbolicLink(path, strTargetPath);
|
||||
return Unix.NativeMethods.CreateSymLink(path, target);
|
||||
}
|
||||
|
||||
internal static bool NonWindowsCreateHardLink(string path, string strTargetPath)
|
||||
@@ -536,12 +536,6 @@ namespace System.Management.Automation
|
||||
}
|
||||
}
|
||||
|
||||
public static bool CreateSymbolicLink(string path, string strTargetPath)
|
||||
{
|
||||
int ret = NativeMethods.CreateSymLink(path, strTargetPath);
|
||||
return ret == 1 ? true : false;
|
||||
}
|
||||
|
||||
public static bool CreateHardLink(string path, string strTargetPath)
|
||||
{
|
||||
int ret = NativeMethods.CreateHardLink(path, strTargetPath);
|
||||
@@ -620,7 +614,8 @@ namespace System.Management.Automation
|
||||
internal static extern int SetDate(SetDateInfoInternal info);
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
internal static extern int CreateSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath,
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static extern bool CreateSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath,
|
||||
[MarshalAs(UnmanagedType.LPStr)]string target);
|
||||
|
||||
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
||||
|
||||
@@ -2220,7 +2220,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
else
|
||||
{
|
||||
success = Platform.NonWindowsCreateSymbolicLink(path,strTargetPath,isDirectory);
|
||||
success = Platform.NonWindowsCreateSymbolicLink(path,strTargetPath);
|
||||
}
|
||||
}
|
||||
else if(itemType == ItemType.HardLink)
|
||||
|
||||
@@ -39,26 +39,25 @@
|
||||
//! - ERROR_INVALID_FUNCTION: incorrect function
|
||||
//! - ERROR_BAD_PATH_NAME: pathname is too long, or contains invalid characters
|
||||
//!
|
||||
//! @retval 1 if creation is successful
|
||||
//! @retval 0 if createion failed
|
||||
//! @retval boolean successful
|
||||
//!
|
||||
|
||||
int32_t CreateSymLink(const char *link, const char *target)
|
||||
bool CreateSymLink(const char *link, const char *target)
|
||||
{
|
||||
errno = 0;
|
||||
errno = 0;
|
||||
|
||||
// Check parameters
|
||||
if (!link || !target)
|
||||
{
|
||||
errno = ERROR_INVALID_PARAMETER;
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
int returnCode = symlink(target, link);
|
||||
int ret = symlink(target, link);
|
||||
|
||||
if (returnCode == 0)
|
||||
if (ret == 0)
|
||||
{
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
switch(errno)
|
||||
@@ -102,5 +101,6 @@ int32_t CreateSymLink(const char *link, const char *target)
|
||||
default:
|
||||
errno = ERROR_INVALID_FUNCTION;
|
||||
}
|
||||
return 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "pal.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
PAL_BEGIN_EXTERNC
|
||||
|
||||
int32_t CreateSymLink(const char *link, const char *target);
|
||||
bool CreateSymLink(const char *link, const char *target);
|
||||
|
||||
PAL_END_EXTERNC
|
||||
|
||||
@@ -39,36 +39,36 @@ protected:
|
||||
EXPECT_TRUE(dir != NULL);
|
||||
|
||||
// Create symbolic link to file
|
||||
int ret1 = CreateSymLink(fileSymLink.c_str(), file);
|
||||
EXPECT_EQ(ret1, 1);
|
||||
bool ret1 = CreateSymLink(fileSymLink.c_str(), file);
|
||||
EXPECT_TRUE(ret1);
|
||||
|
||||
// Create symbolic link to directory
|
||||
int ret2 = CreateSymLink(dirSymLink.c_str(), dir);
|
||||
EXPECT_EQ(ret2, 1);
|
||||
bool ret2 = CreateSymLink(dirSymLink.c_str(), dir);
|
||||
EXPECT_TRUE(ret2);
|
||||
}
|
||||
|
||||
~CreateSymLinkTest()
|
||||
{
|
||||
int ret;
|
||||
bool ret;
|
||||
|
||||
ret = unlink(fileSymLink.c_str());
|
||||
EXPECT_EQ(0, ret);
|
||||
EXPECT_FALSE(ret);
|
||||
|
||||
ret = unlink(dirSymLink.c_str());
|
||||
EXPECT_EQ(0, ret);
|
||||
EXPECT_FALSE(ret);
|
||||
|
||||
ret = unlink(file);
|
||||
EXPECT_EQ(0, ret);
|
||||
EXPECT_FALSE(ret);
|
||||
|
||||
ret = rmdir(dir);
|
||||
EXPECT_EQ(0, ret);
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(CreateSymLinkTest, FilePathNameIsNull)
|
||||
{
|
||||
int retVal = CreateSymLink(NULL, NULL);
|
||||
EXPECT_EQ(retVal, 0);
|
||||
bool retVal = CreateSymLink(NULL, NULL);
|
||||
EXPECT_FALSE(retVal);
|
||||
EXPECT_EQ(ERROR_INVALID_PARAMETER, errno);
|
||||
}
|
||||
|
||||
@@ -82,8 +82,8 @@ TEST_F(CreateSymLinkTest, FilePathNameDoesNotExist)
|
||||
unlink(invalidLink.c_str());
|
||||
|
||||
// Linux allows creation of symbolic link that points to an invalid file
|
||||
int retVal = CreateSymLink(invalidLink.c_str(), invalidFile.c_str());
|
||||
EXPECT_EQ(retVal, 1);
|
||||
bool retVal = CreateSymLink(invalidLink.c_str(), invalidFile.c_str());
|
||||
EXPECT_TRUE(retVal);
|
||||
|
||||
std::string target = FollowSymLink(invalidLink.c_str());
|
||||
EXPECT_EQ(target, invalidFile);
|
||||
@@ -93,8 +93,8 @@ TEST_F(CreateSymLinkTest, FilePathNameDoesNotExist)
|
||||
|
||||
TEST_F(CreateSymLinkTest, SymLinkToFile)
|
||||
{
|
||||
int retVal = IsSymLink(fileSymLink.c_str());
|
||||
EXPECT_EQ(1, retVal);
|
||||
bool retVal = IsSymLink(fileSymLink.c_str());
|
||||
EXPECT_TRUE(retVal);
|
||||
|
||||
std::string target = FollowSymLink(fileSymLink.c_str());
|
||||
char buffer[PATH_MAX];
|
||||
@@ -104,8 +104,8 @@ TEST_F(CreateSymLinkTest, SymLinkToFile)
|
||||
|
||||
TEST_F(CreateSymLinkTest, SymLinkToDirectory)
|
||||
{
|
||||
int retVal = IsSymLink(dirSymLink.c_str());
|
||||
EXPECT_EQ(1, retVal);
|
||||
bool retVal = IsSymLink(dirSymLink.c_str());
|
||||
EXPECT_TRUE(retVal);
|
||||
|
||||
std::string target = FollowSymLink(dirSymLink.c_str());
|
||||
char buffer[PATH_MAX];
|
||||
@@ -115,7 +115,7 @@ TEST_F(CreateSymLinkTest, SymLinkToDirectory)
|
||||
|
||||
TEST_F(CreateSymLinkTest, SymLinkAgain)
|
||||
{
|
||||
int retVal = CreateSymLink(fileSymLink.c_str(), file);
|
||||
EXPECT_EQ(0, retVal);
|
||||
bool retVal = CreateSymLink(fileSymLink.c_str(), file);
|
||||
EXPECT_FALSE(retVal);
|
||||
EXPECT_EQ(ERROR_FILE_EXISTS, errno);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user