Пример #1
0
wxString CLocalFileSystem::GetSymbolicLinkTarget(wxString const& path)
{
	wxString target;

#ifdef __WXMSW__
	HANDLE hFile = CreateFile(path, FILE_READ_ATTRIBUTES | FILE_READ_EA, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
	if (hFile != INVALID_HANDLE_VALUE) {
		DWORD const size = 1024;
		wxChar out[size];
		DWORD ret = GetFinalPathNameByHandle(hFile, out, size, 0);
		if (ret > 0 && ret < size) {
			target = out;
		}
		CloseHandle(hFile);
	}
#else
	size_t const size = 1024;
	char out[size];

	const wxCharBuffer p = path.fn_str();
	ssize_t res = readlink(static_cast<char const*>(p), out, size);
	if( res > 0 && static_cast<size_t>(res) < size ) {
		out[res] = 0;
		target = wxString(out, *wxConvFileName);
	}
#endif
	return target;
}
Пример #2
0
BOOL WINAPI ResilientCloseHandle(
    HANDLE hObject)
{
    BOOL Success;
    DWORD LastError;
    DWORD HandleFlags = 0, FileNameLen;
    WCHAR FileNameBuf[sizeof "\\\\?\\GLOBALROOT" - 1 + 1024] = L"\\\\?\\GLOBALROOT";

    if (GetHandleInformation(hObject, &HandleFlags) &&
        (HANDLE_FLAG_PROTECT_FROM_CLOSE & HandleFlags))
    {
        SetHandleInformation(hObject,
            HANDLE_FLAG_PROTECT_FROM_CLOSE, 0);
        FileNameLen = GetFinalPathNameByHandle(hObject,
            FileNameBuf + sizeof "\\\\?\\GLOBALROOT" - 1, 1023,
            FILE_NAME_OPENED | VOLUME_NAME_NT);
        if (0 == FileNameLen || FileNameLen >= 1024)
            HandleFlags = 0;
    }

    Success = CloseHandle(
        hObject);
    LastError = GetLastError();

    if (Success)
    {
        if (HANDLE_FLAG_PROTECT_FROM_CLOSE & HandleFlags)
            WaitDeletePending(FileNameBuf);
    }

    SetLastError(LastError);
    return Success;
}
Пример #3
0
char *common_get_fd_path(int fd) {
  struct stat st;
  if (fstat(fd, &st)) {
    return NULL;
  }

#if defined WIN32
  // Windows
  HANDLE hdl = (HANDLE) _get_osfhandle(fd);
  if (hdl != INVALID_HANDLE_VALUE) {
    DWORD size = GetFinalPathNameByHandle(hdl, NULL, 0, 0);
    if (size) {
      char *path = g_malloc(size);
      DWORD ret = GetFinalPathNameByHandle(hdl, path, size - 1, 0);
      if (ret > 0 && ret <= size) {
        return path;
      }
      g_free(path);
    }
  }
#elif defined HAVE_PROC_PIDFDINFO
  // Mac OS X
  // Ignore kqueues, since they can be opened behind our back for
  // Grand Central Dispatch
  struct kqueue_fdinfo kqi;
  if (proc_pidfdinfo(getpid(), fd, PROC_PIDFDKQUEUEINFO, &kqi, sizeof(kqi))) {
    return NULL;
  }
  char *path = g_malloc(MAXPATHLEN);
  if (!fcntl(fd, F_GETPATH, path)) {
    return path;
  }
  g_free(path);
#else
  // Fallback; works only on Linux
  char *link_path = g_strdup_printf("/proc/%d/fd/%d", getpid(), fd);
  char *path = g_file_read_link(link_path, NULL);
  g_free(link_path);
  if (path) {
    return path;
  }
#endif

  return g_strdup("<unknown>");
}
Пример #4
0
void XFile::CreateAndAssignPathInfo()
{
    if (this->IsOpen() && this->Exists)
    {
#ifdef WIN32
        // Here I have a valid handle and the file exists
        TCHAR path[MAX_PATH];
        DWORD res;

        res = GetFinalPathNameByHandle(winFileHandle, path, MAX_PATH, VOLUME_NAME_DOS);

        if (res < MAX_PATH)
        {
            std::string _fileName;
            std::string _parentDirPath;
            std::string _fileExt;

            std::wstring x = path;
            std::string y = WideStringToCString(x);

            int lastSlash = y.find_last_of("\\");

            _fileName = y.substr(lastSlash + 1, y.length() - lastSlash);

            _parentDirPath = y.substr(0, lastSlash);

            int fileNameExtensionDot = _fileName.find_last_of(".");
            _fileExt = _fileName.substr(fileNameExtensionDot, _fileName.length() - fileNameExtensionDot);

            FileName = _fileName;
            FileExt = _fileExt;

            ParentDirectoryPath = CleanupDirectoryName(_parentDirPath);

            this->parentDir = new XDirectory(ParentDirectoryPath);
        }
        else
        {
            // Path too big ?
            assert(true);
        }

#endif
    }
    else
    {
        // Path is relative,probably
    }
}
Пример #5
0
	void Path::checkPath(Path& path, bool resolveSymLink)
	{
		HANDLE file = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

		DWORD attr = GetFileAttributes(path);
		DWORD type = GetFileType(file);

		if(file != INVALID_HANDLE_VALUE && attr != INVALID_FILE_ATTRIBUTES)
		{
			switch(type)
			{
				case FILE_TYPE_CHAR:
					path.typeVal = Type::Character;
					break;
				case FILE_TYPE_DISK:
					path.typeVal = Type::Block;
					break;
				case FILE_TYPE_PIPE:
					path.typeVal = Type::Pipe;
					break;
				default:
					path.typeVal = Type::Unknown;
					break;
			}

			if((attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
				path.typeVal = Type::Directory;
			else if((attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
				path.typeVal = Type::SymLink;
			else
				path.typeVal = Type::File;

			// ignore permissions on Windows... it's... convoluted. for now at least

			if(path.typeVal != Type::SymLink || resolveSymLink)
			{
				// get absolute path
				char buf[MAX_PATH];

				std::size_t rSize = GetFinalPathNameByHandle(file, buf, MAX_PATH, FILE_NAME_OPENED);

				path.pathStr = {buf, rSize};
			}

			// get rid of the unicode stuff on the front of string
			std::size_t driveColon = path.pathStr.find(':');
			if(driveColon != std::string::npos)
				path.pathStr.erase(0, driveColon - 1);

			// add directory divider to end of directory Paths
			if(path.typeVal == Type::Directory)
				path.pathStr += '\\';

			path.existsVal = true;
		}
		else
		{
			path.typeVal = Type::Unknown;
			path.existsVal = false;
		}
	}
Пример #6
0
        bool search::generate_pbo_list() {
            NTSTATUS status;
            PSYSTEM_HANDLE_INFORMATION handleInfo;
            ULONG handleInfoSize = 0x10000;
            ULONG pid;
            HANDLE processHandle;
            ULONG i;

            _NtQuerySystemInformation NtQuerySystemInformation =
                (_NtQuerySystemInformation)GetLibraryProcAddress("ntdll.dll", "NtQuerySystemInformation");
            _NtDuplicateObject NtDuplicateObject =
                (_NtDuplicateObject)GetLibraryProcAddress("ntdll.dll", "NtDuplicateObject");
            _NtQueryObject NtQueryObject =
                (_NtQueryObject)GetLibraryProcAddress("ntdll.dll", "NtQueryObject");

            if (!NtQuerySystemInformation || !NtDuplicateObject || !NtQueryObject)
                return false;

            pid = GetCurrentProcessId();
            processHandle = GetCurrentProcess();

            handleInfo = (PSYSTEM_HANDLE_INFORMATION)malloc(handleInfoSize);

            while ((status = NtQuerySystemInformation(
                SystemHandleInformation,
                handleInfo,
                handleInfoSize,
                NULL
                )) == STATUS_INFO_LENGTH_MISMATCH)
                handleInfo = (PSYSTEM_HANDLE_INFORMATION)realloc(handleInfo, handleInfoSize *= 2);

            /* NtQuerySystemInformation stopped giving us STATUS_INFO_LENGTH_MISMATCH. */
            if (!NT_SUCCESS(status))
            {
                LOG(ERROR) << "Error opening object for pbo search";
                free(handleInfo);
                return false;
            }

            for (i = 0; i < handleInfo->HandleCount; i++)
            {
                SYSTEM_HANDLE handle = handleInfo->Handles[i];
                HANDLE dupHandle = NULL;
                POBJECT_TYPE_INFORMATION objectTypeInfo;
                PVOID objectNameInfo;
                UNICODE_STRING objectName;
                ULONG returnLength;

                /* Check if this handle belongs to the PID the user specified. */
                if (handle.ProcessId != pid)
                    continue;

                /* Duplicate the handle so we can query it. */
                if (!NT_SUCCESS(NtDuplicateObject(
                    processHandle,
                    (HANDLE)handle.Handle,
                    GetCurrentProcess(),
                    &dupHandle,
                    0,
                    0,
                    0
                    )))
                {
                    continue;
                }

                /* Query the object type. */
                objectTypeInfo = (POBJECT_TYPE_INFORMATION)malloc(0x1000);
                if (!NT_SUCCESS(NtQueryObject(
                    dupHandle,
                    ObjectTypeInformation,
                    objectTypeInfo,
                    0x1000,
                    NULL
                    )))
                {
                    CloseHandle(dupHandle);
                    continue;
                }

                /* Query the object name (unless it has an access of
                0x0012019f, on which NtQueryObject could hang. */
                if (handle.GrantedAccess == 0x0012019f)
                {

                    free(objectTypeInfo);
                    CloseHandle(dupHandle);
                    continue;
                }

                objectNameInfo = malloc(0x1000);
                if (!NT_SUCCESS(NtQueryObject(
                    dupHandle,
                    ObjectNameInformation,
                    objectNameInfo,
                    0x1000,
                    &returnLength
                    )))
                {
                    /* Reallocate the buffer and try again. */
                    objectNameInfo = realloc(objectNameInfo, returnLength);
                    if (!NT_SUCCESS(NtQueryObject(
                        dupHandle,
                        ObjectNameInformation,
                        objectNameInfo,
                        returnLength,
                        NULL
                        )))
                    {
                        free(objectTypeInfo);
                        free(objectNameInfo);
                        CloseHandle(dupHandle);
                        continue;
                    }
                }

                /* Cast our buffer into an UNICODE_STRING. */
                objectName = *(PUNICODE_STRING)objectNameInfo;
               
                

                /* Print the information! */
                if (objectName.Length)
                {
                    std::wstring tmp_type(objectTypeInfo->Name.Buffer);
                    std::wstring tmp_name(objectName.Buffer);
                    
                    std::string object_type(tmp_type.begin(), tmp_type.end());
                    std::string object_name(tmp_name.begin(), tmp_name.end());
                    if (object_type == "File" && object_name.find(".pbo") != object_name.npos) {
                        char buffer[MAX_PATH];
                        GetFinalPathNameByHandle(dupHandle, buffer, sizeof(buffer), VOLUME_NAME_DOS);

                        LOG(DEBUG) << "Pbo: " << buffer;
                        _active_pbo_list.push_back(std::string(buffer));
                    }
                }              

                free(objectTypeInfo);
                free(objectNameInfo);
                CloseHandle(dupHandle);
            }

            free(handleInfo);

            return true;
        }