Skip to content

Commit d239c62

Browse files
committed
Use File.SetUnixFileMode to set the execute permission first. If that fails, use chmod to set the execute permission.
2dust#7416
1 parent 984b36d commit d239c62

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

v2rayN/ServiceLib/Common/Utils.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ public static Dictionary<string, string> GetSystemHosts()
582582
var result = await cmd.ExecuteBufferedAsync();
583583
if (result.IsSuccess)
584584
{
585-
return result.StandardOutput.ToString();
585+
return result.StandardOutput ?? "";
586586
}
587587

588588
Logging.SaveLog(result.ToString() ?? "");
@@ -839,14 +839,46 @@ public static bool IsAdministrator()
839839
public static async Task<string?> SetLinuxChmod(string? fileName)
840840
{
841841
if (fileName.IsNullOrEmpty())
842+
{
842843
return null;
844+
}
845+
if (SetUnixFileMode(fileName))
846+
{
847+
Logging.SaveLog($"Successfully set the file execution permission, {fileName}");
848+
return "";
849+
}
850+
843851
if (fileName.Contains(' '))
852+
{
844853
fileName = fileName.AppendQuotes();
845-
//File.SetUnixFileMode(fileName, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
854+
}
846855
var arg = new List<string>() { "-c", $"chmod +x {fileName}" };
847856
return await GetCliWrapOutput(Global.LinuxBash, arg);
848857
}
849858

859+
public static bool SetUnixFileMode(string? fileName)
860+
{
861+
try
862+
{
863+
if (fileName.IsNullOrEmpty())
864+
{
865+
return false;
866+
}
867+
868+
if (File.Exists(fileName))
869+
{
870+
var currentMode = File.GetUnixFileMode(fileName);
871+
File.SetUnixFileMode(fileName, currentMode | UnixFileMode.UserExecute | UnixFileMode.GroupExecute | UnixFileMode.OtherExecute);
872+
return true;
873+
}
874+
}
875+
catch (Exception ex)
876+
{
877+
Logging.SaveLog("SetUnixFileMode", ex);
878+
}
879+
return false;
880+
}
881+
850882
public static async Task<string?> GetLinuxFontFamily(string lang)
851883
{
852884
// var arg = new List<string>() { "-c", $"fc-list :lang={lang} family" };

0 commit comments

Comments
 (0)