mirror of
https://bitbucket.org/anguist/ntpa
synced 2025-11-22 19:39:26 +00:00
Avoid faulty log messages
This commit is contained in:
@ -19,6 +19,7 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System;
|
||||
using Mono.Unix.Native;
|
||||
|
||||
namespace Ntp.Common.IO
|
||||
@ -27,32 +28,52 @@ namespace Ntp.Common.IO
|
||||
{
|
||||
public static bool ChangeFileMode(string file, uint mode)
|
||||
{
|
||||
// Changing file mode is only supported on Unix platforms
|
||||
if (Environment.OSVersion.Platform != PlatformID.Unix)
|
||||
return true;
|
||||
|
||||
var permissions = NativeConvert.FromOctalPermissionString(mode.ToString());
|
||||
return Syscall.chmod(file, permissions) == 0;
|
||||
}
|
||||
|
||||
public static bool ChangeFileOwner(string file, uint user, uint? group)
|
||||
{
|
||||
// Changing file owner is only supported on Unix platforms
|
||||
if (Environment.OSVersion.Platform != PlatformID.Unix)
|
||||
return true;
|
||||
|
||||
return Syscall.chown(file, user, group ?? unchecked((uint) -1)) == 0;
|
||||
}
|
||||
|
||||
public static uint? GetGroupId(string groupName)
|
||||
{
|
||||
// Group IDs are only supported on Unix platforms
|
||||
if (Environment.OSVersion.Platform != PlatformID.Unix)
|
||||
return 0;
|
||||
|
||||
var gr = Syscall.getgrnam(groupName);
|
||||
return gr?.gr_gid;
|
||||
}
|
||||
|
||||
public static uint? GetUserId(string userName)
|
||||
{
|
||||
// User IDs are only supported on Unix platforms
|
||||
if (Environment.OSVersion.Platform != PlatformID.Unix)
|
||||
return 0;
|
||||
|
||||
var pw = Syscall.getpwnam(userName);
|
||||
return pw?.pw_uid;
|
||||
}
|
||||
|
||||
public static bool SetUserId(uint userId)
|
||||
{
|
||||
// Setting user ID is only supported on Unix platforms
|
||||
if (Environment.OSVersion.Platform != PlatformID.Unix)
|
||||
return true;
|
||||
|
||||
try
|
||||
{
|
||||
Syscall.seteuid(userId);
|
||||
Syscall.setuid(userId);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user