Exemplo n.º 1
0
        /// <summary>
        /// Tries to get the information for a file behind a handle.
        /// </summary>
        public bool TryGetInfoForHandle(IntPtr handle, out FileInfo info)
        {
            info = null;

            if (!_handleToInfoMap.ContainsKey(handle))
            {
                return(false);
            }

            info = _handleToInfoMap[handle];
            return(true);
        }
Exemplo n.º 2
0
        private int NtCreateFileImpl(out IntPtr handle, FileAccess access, ref Native.Native.OBJECT_ATTRIBUTES objectAttributes, ref Native.Native.IO_STATUS_BLOCK ioStatus, ref long allocSize, uint fileAttributes, FileShare share, uint createDisposition, uint createOptions, IntPtr eaBuffer, uint eaLength)
        {
            lock (_createLock)
            {
                string oldFileName = objectAttributes.ObjectName.ToString();
                if (!TryGetFullPath(oldFileName, out var newFilePath))
                {
                    return(_createFileHook.OriginalFunction(out handle, access, ref objectAttributes, ref ioStatus, ref allocSize, fileAttributes, share, createDisposition, createOptions, eaBuffer, eaLength));
                }

                // Check if ONE file and register if it is.
                if (newFilePath.Contains(Constants.OneExtension, StringComparison.OrdinalIgnoreCase))
                {
                    var result = _createFileHook.OriginalFunction(out handle, access, ref objectAttributes, ref ioStatus, ref allocSize, fileAttributes, share, createDisposition, createOptions, eaBuffer, eaLength);
                    DisableRedirectionHooks();
                    if (IsOneFile(newFilePath))
                    {
#if DEBUG
                        Console.WriteLine($"[ONEHook] ONE Handle Opened: {handle}, File: {newFilePath}");
#endif
                        _handleToInfoMap[handle] = new FileInfo(newFilePath, 0);
                        OnOneHandleOpened(handle, newFilePath);
                    }
                    EnableRedirectionHooks();
                    return(result);
                }

                var ntStatus = _createFileHook.OriginalFunction(out handle, access, ref objectAttributes, ref ioStatus, ref allocSize, fileAttributes, share, createDisposition, createOptions, eaBuffer, eaLength);

                // Invalidate Duplicate Handles (until we implement NtClose hook).
                if (_handleToInfoMap.ContainsKey(handle))
                {
                    _handleToInfoMap.TryRemove(handle, out var value);
#if DEBUG
                    Console.WriteLine($"[ONEHook] Removed old disposed handle: {handle}, File: {value.FilePath}");
#endif
                }

                return(ntStatus);
            }
        }