/// <summary> /// Attempts to change the owner of the <paramref name="filename"/> to <paramref name="user"/>. /// </summary> /// <param name="ctx">Runtime context.</param> /// <param name="filename">Path to the file to change owner.</param> /// <param name="user">A <see cref="string"/> or <see cref="int"/> identifier of the target group.</param> /// <returns>Whether the function succeeded.</returns> public static bool chown(Context ctx, string filename, PhpValue user) { if (PhpPath.ResolvePath(ctx, ref filename, false, out var wrapper)) { var fs = new FileSecurity(filename, AccessControlSections.Owner); // throws if file does not exist or no permissions IdentityReference identity; if (user.IsString(out var uname)) { var sepidx = uname.IndexOf('/'); var domain_user = sepidx >= 0 ? (uname.Remove(sepidx), uname.Substring(sepidx + 1)) : (null, uname); identity = new NTAccount(domain_user.Item1, domain_user.Item2); } //else if (user.IsLong(out var uid)) //{ //} else { PhpException.InvalidArgumentType(nameof(user), PhpVariable.TypeNameString); return(false); } //var identity = user.IsString(out var uname) ? new NTAccount(uname) : user.IsLong(out var uid) ? new IdentityReference(...) : null; fs.SetOwner(identity); // throws if no permission or error return(true); } // return(false); }
/// <summary> /// Changes file permissions. /// </summary> /// <remarks> /// On Windows platform this function supports only the /// <c>_S_IREAD (0400)</c> and <c>_S_IWRITE (0200)</c> /// options (set read / write permissions for the file owner). /// Note that the constants are octal numbers. /// </remarks> /// <param name="ctx">Runtime context.</param> /// <param name="path">Path to the file to change group.</param> /// <param name="mode">New file permissions (see remarks).</param> /// <returns><c>true</c> on success, <c>false</c> on failure.</returns> public static bool chmod(Context ctx, string path, int mode) { StreamWrapper wrapper; if (!PhpPath.ResolvePath(ctx, ref path, false, out wrapper)) { return(false); } bool isDir = PhpPath.is_dir(ctx, path); FileSystemInfo fInfo = isDir ? (FileSystemInfo) new DirectoryInfo(path) : new FileInfo(path); if (!fInfo.Exists) { //PhpException.Throw(PhpError.Warning, CoreResources.GetString("invalid_path", path)); // TODO: Err invalid_path return(false); } //Directories has no equivalent of a readonly flag, //instead, their content permission should be adjusted accordingly //[http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.directorysecurity.aspx] if (isDir) { return(false); } else { // according to <io.h> and <chmod.c> from C libraries in Visual Studio 2008 // and PHP 5.3 source codes, which are using standard _chmod() function in C // on Windows it only changes the ReadOnly flag of the file // // see <chmod.c> for more details /* #define _S_IREAD 0x0100 // read permission, owner #define _S_IWRITE 0x0080 // write permission, owner #define _S_IEXEC 0x0040 // execute/search permission, owner */ ((FileInfo)fInfo).IsReadOnly = ((mode & 0x0080) == 0); } return(true); }