Esempio n. 1
0
static NTSTATUS DoQuery (PVFAT_IRP_CONTEXT IrpContext)
{
  NTSTATUS RC = STATUS_SUCCESS;
  long BufferLength = 0;
  PUNICODE_STRING pSearchPattern = NULL;
  FILE_INFORMATION_CLASS FileInformationClass;
  unsigned char *Buffer = NULL;
  PFILE_NAMES_INFORMATION Buffer0 = NULL;
  PVFATFCB pFcb;
  PVFATCCB pCcb;
  BOOLEAN FirstQuery = FALSE;
  BOOLEAN FirstCall = TRUE;
  VFAT_DIRENTRY_CONTEXT DirContext;
  WCHAR LongNameBuffer[LONGNAME_MAX_LENGTH + 1];
  WCHAR ShortNameBuffer[13];

  PIO_STACK_LOCATION Stack = IrpContext->Stack;

  pCcb = (PVFATCCB) IrpContext->FileObject->FsContext2;
  pFcb = (PVFATFCB) IrpContext->FileObject->FsContext;

  // determine Buffer for result :
  BufferLength = Stack->Parameters.QueryDirectory.Length;
#if 0
  /* Do not probe the user buffer until SEH is available */
  if (IrpContext->Irp->RequestorMode != KernelMode &&
      IrpContext->Irp->MdlAddress == NULL &&
      IrpContext->Irp->UserBuffer != NULL)
    {
      ProbeForWrite(IrpContext->Irp->UserBuffer, BufferLength, 1);
    }
#endif
  Buffer = VfatGetUserBuffer(IrpContext->Irp);

  if (!ExAcquireResourceSharedLite(&pFcb->MainResource,
                                   (BOOLEAN)(IrpContext->Flags & IRPCONTEXT_CANWAIT)))
    {
      RC = VfatLockUserBuffer(IrpContext->Irp, BufferLength, IoWriteAccess);
      if (NT_SUCCESS(RC))
        {
          RC = STATUS_PENDING;
        }
      return RC;
    }

  /* Obtain the callers parameters */
#ifdef _MSC_VER
  /* HACKHACK: Bug in the MS ntifs.h header:
   * FileName is really a PUNICODE_STRING, not a PSTRING */
  pSearchPattern = (PUNICODE_STRING)Stack->Parameters.QueryDirectory.FileName;
#else
  pSearchPattern = Stack->Parameters.QueryDirectory.FileName;
#endif
  FileInformationClass =
    Stack->Parameters.QueryDirectory.FileInformationClass;
  if (pSearchPattern)
    {
      if (!pCcb->SearchPattern.Buffer)
        {
          FirstQuery = TRUE;
          pCcb->SearchPattern.MaximumLength = pSearchPattern->Length + sizeof(WCHAR);
          pCcb->SearchPattern.Buffer = ExAllocatePoolWithTag(NonPagedPool, pCcb->SearchPattern.MaximumLength, TAG_VFAT);
          if (!pCcb->SearchPattern.Buffer)
            {
              ExReleaseResourceLite(&pFcb->MainResource);
              return STATUS_INSUFFICIENT_RESOURCES;
            }
          RtlCopyUnicodeString(&pCcb->SearchPattern, pSearchPattern);
          pCcb->SearchPattern.Buffer[pCcb->SearchPattern.Length / sizeof(WCHAR)] = 0;
	}
    }
  else if (!pCcb->SearchPattern.Buffer)
    {
      FirstQuery = TRUE;
      pCcb->SearchPattern.MaximumLength = 2 * sizeof(WCHAR);
      pCcb->SearchPattern.Buffer = ExAllocatePoolWithTag(NonPagedPool, 2 * sizeof(WCHAR), TAG_VFAT);
      if (!pCcb->SearchPattern.Buffer)
        {
          ExReleaseResourceLite(&pFcb->MainResource);
          return STATUS_INSUFFICIENT_RESOURCES;
        }
      pCcb->SearchPattern.Buffer[0] = L'*';
      pCcb->SearchPattern.Buffer[1] = 0;
      pCcb->SearchPattern.Length = sizeof(WCHAR);
    }

  if (IrpContext->Stack->Flags & SL_INDEX_SPECIFIED)
    {
      DirContext.DirIndex = pCcb->Entry = Stack->Parameters.QueryDirectory.FileIndex;
    }
  else if (FirstQuery || (IrpContext->Stack->Flags & SL_RESTART_SCAN))
    {
      DirContext.DirIndex = pCcb->Entry = 0;
    }
  else
    {
      DirContext.DirIndex = pCcb->Entry;
    }

  DPRINT ("Buffer=%p tofind=%wZ\n", Buffer, &pCcb->SearchPattern);

  DirContext.LongNameU.Buffer = LongNameBuffer;
  DirContext.LongNameU.MaximumLength = sizeof(LongNameBuffer);
  DirContext.ShortNameU.Buffer = ShortNameBuffer;
  DirContext.ShortNameU.MaximumLength = sizeof(ShortNameBuffer);

  while (RC == STATUS_SUCCESS && BufferLength > 0)
    {
      RC = FindFile (IrpContext->DeviceExt, pFcb,
                     &pCcb->SearchPattern, &DirContext, FirstCall);
      pCcb->Entry = DirContext.DirIndex;
      DPRINT ("Found %wZ, RC=%x, entry %x\n", &DirContext.LongNameU, RC, pCcb->Entry);
      FirstCall = FALSE;
      if (NT_SUCCESS (RC))
        {
          switch (FileInformationClass)
            {
              case FileNameInformation:
                RC = VfatGetFileNameInformation (&DirContext,
                                                 (PFILE_NAMES_INFORMATION) Buffer,
					         BufferLength);
                break;
              case FileDirectoryInformation:
                RC = VfatGetFileDirectoryInformation (&DirContext,
	                                              IrpContext->DeviceExt,
						      (PFILE_DIRECTORY_INFORMATION) Buffer,
						      BufferLength);
                break;
             case FileFullDirectoryInformation:
               RC = VfatGetFileFullDirectoryInformation (&DirContext,
	                                                 IrpContext->DeviceExt,
						         (PFILE_FULL_DIR_INFORMATION) Buffer,
						         BufferLength);
               break;
             case FileBothDirectoryInformation:
               RC = VfatGetFileBothInformation (&DirContext,
	                                        IrpContext->DeviceExt,
					        (PFILE_BOTH_DIR_INFORMATION) Buffer,
					        BufferLength);
               break;
             default:
               RC = STATUS_INVALID_INFO_CLASS;
	    }
          if (RC == STATUS_BUFFER_OVERFLOW)
            {
              break;
            }
	}
      else
        {
          if (FirstQuery)
            {
              RC = STATUS_NO_SUCH_FILE;
            }
          else
            {
              RC = STATUS_NO_MORE_FILES;
            }
          break;
	}
      Buffer0 = (PFILE_NAMES_INFORMATION) Buffer;
      Buffer0->FileIndex = DirContext.DirIndex;
      pCcb->Entry = ++DirContext.DirIndex;
      BufferLength -= Buffer0->NextEntryOffset;
      if (IrpContext->Stack->Flags & SL_RETURN_SINGLE_ENTRY)
        {
          break;
        }
      Buffer += Buffer0->NextEntryOffset;
    }
  if (Buffer0)
    {
      Buffer0->NextEntryOffset = 0;
      RC = STATUS_SUCCESS;
      IrpContext->Irp->IoStatus.Information = Stack->Parameters.QueryDirectory.Length - BufferLength;

    }
  ExReleaseResourceLite(&pFcb->MainResource);
  return RC;
}
Esempio n. 2
0
static
NTSTATUS
DoQuery(
    PVFAT_IRP_CONTEXT IrpContext)
{
    NTSTATUS Status = STATUS_SUCCESS;
    LONG BufferLength = 0;
    PUNICODE_STRING pSearchPattern = NULL;
    FILE_INFORMATION_CLASS FileInformationClass;
    PUCHAR Buffer = NULL;
    PFILE_NAMES_INFORMATION Buffer0 = NULL;
    PVFATFCB pFcb;
    PVFATCCB pCcb;
    BOOLEAN FirstQuery = FALSE;
    BOOLEAN FirstCall = TRUE;
    VFAT_DIRENTRY_CONTEXT DirContext;
    WCHAR LongNameBuffer[LONGNAME_MAX_LENGTH + 1];
    WCHAR ShortNameBuffer[13];

    PIO_STACK_LOCATION Stack = IrpContext->Stack;

    pCcb = (PVFATCCB)IrpContext->FileObject->FsContext2;
    pFcb = (PVFATFCB)IrpContext->FileObject->FsContext;

    /* Determine Buffer for result : */
    BufferLength = Stack->Parameters.QueryDirectory.Length;
#if 0
    /* Do not probe the user buffer until SEH is available */
    if (IrpContext->Irp->RequestorMode != KernelMode &&
        IrpContext->Irp->MdlAddress == NULL &&
        IrpContext->Irp->UserBuffer != NULL)
    {
        ProbeForWrite(IrpContext->Irp->UserBuffer, BufferLength, 1);
    }
#endif
    Buffer = VfatGetUserBuffer(IrpContext->Irp, FALSE);

    if (!ExAcquireResourceSharedLite(&pFcb->MainResource,
                                     BooleanFlagOn(IrpContext->Flags, IRPCONTEXT_CANWAIT)))
    {
        Status = VfatLockUserBuffer(IrpContext->Irp, BufferLength, IoWriteAccess);
        if (NT_SUCCESS(Status))
            Status = STATUS_PENDING;

        return Status;
    }

    /* Obtain the callers parameters */
#ifdef _MSC_VER
    /* HACKHACK: Bug in the MS ntifs.h header:
     * FileName is really a PUNICODE_STRING, not a PSTRING */
    pSearchPattern = (PUNICODE_STRING)Stack->Parameters.QueryDirectory.FileName;
#else
    pSearchPattern = Stack->Parameters.QueryDirectory.FileName;
#endif
    FileInformationClass = Stack->Parameters.QueryDirectory.FileInformationClass;

    /* Allocate search pattern in case:
     * -> We don't have one already in context
     * -> We have been given an input pattern
     * -> The pattern length is not null
     * -> The pattern buffer is not null
     * Otherwise, we'll fall later and allocate a match all (*) pattern
     */ 
    if (pSearchPattern &&
        pSearchPattern->Length != 0 && pSearchPattern->Buffer != NULL)
    {
        if (!pCcb->SearchPattern.Buffer)
        {
            FirstQuery = TRUE;
            pCcb->SearchPattern.MaximumLength = pSearchPattern->Length + sizeof(WCHAR);
            pCcb->SearchPattern.Buffer = ExAllocatePoolWithTag(NonPagedPool,
                                                               pCcb->SearchPattern.MaximumLength,
                                                               TAG_VFAT);
            if (!pCcb->SearchPattern.Buffer)
            {
                ExReleaseResourceLite(&pFcb->MainResource);
                return STATUS_INSUFFICIENT_RESOURCES;
            }
            RtlCopyUnicodeString(&pCcb->SearchPattern, pSearchPattern);
            pCcb->SearchPattern.Buffer[pCcb->SearchPattern.Length / sizeof(WCHAR)] = 0;
        }
    }
    else if (!pCcb->SearchPattern.Buffer)
    {
        FirstQuery = TRUE;
        pCcb->SearchPattern.MaximumLength = 2 * sizeof(WCHAR);
        pCcb->SearchPattern.Buffer = ExAllocatePoolWithTag(NonPagedPool,
                                                           2 * sizeof(WCHAR),
                                                           TAG_VFAT);
        if (!pCcb->SearchPattern.Buffer)
        {
            ExReleaseResourceLite(&pFcb->MainResource);
            return STATUS_INSUFFICIENT_RESOURCES;
        }
        pCcb->SearchPattern.Buffer[0] = L'*';
        pCcb->SearchPattern.Buffer[1] = 0;
        pCcb->SearchPattern.Length = sizeof(WCHAR);
    }

    if (IrpContext->Stack->Flags & SL_INDEX_SPECIFIED)
    {
        DirContext.DirIndex = pCcb->Entry = Stack->Parameters.QueryDirectory.FileIndex;
    }
    else if (FirstQuery || (IrpContext->Stack->Flags & SL_RESTART_SCAN))
    {
        DirContext.DirIndex = pCcb->Entry = 0;
    }
    else
    {
        DirContext.DirIndex = pCcb->Entry;
    }

    DPRINT("Buffer=%p tofind=%wZ\n", Buffer, &pCcb->SearchPattern);

    DirContext.LongNameU.Buffer = LongNameBuffer;
    DirContext.LongNameU.MaximumLength = sizeof(LongNameBuffer);
    DirContext.ShortNameU.Buffer = ShortNameBuffer;
    DirContext.ShortNameU.MaximumLength = sizeof(ShortNameBuffer);

    if (!ExAcquireResourceExclusiveLite(&IrpContext->DeviceExt->DirResource,
                                        BooleanFlagOn(IrpContext->Flags, IRPCONTEXT_CANWAIT)))
    {
        ExReleaseResourceLite(&pFcb->MainResource);
        return VfatMarkIrpContextForQueue(IrpContext);
    }

    while ((Status == STATUS_SUCCESS) && (BufferLength > 0))
    {
        Status = FindFile(IrpContext->DeviceExt,
                          pFcb,
                          &pCcb->SearchPattern,
                          &DirContext,
                          FirstCall);
        pCcb->Entry = DirContext.DirIndex;

        DPRINT("Found %wZ, Status=%x, entry %x\n", &DirContext.LongNameU, Status, pCcb->Entry);

        FirstCall = FALSE;
        if (NT_SUCCESS(Status))
        {
            switch (FileInformationClass)
            {
                case FileNameInformation:
                    Status = VfatGetFileNameInformation(&DirContext,
                                                        (PFILE_NAMES_INFORMATION)Buffer,
                                                        BufferLength);
                    break;

                case FileDirectoryInformation:
                    Status = VfatGetFileDirectoryInformation(&DirContext,
                                                             IrpContext->DeviceExt,
                                                             (PFILE_DIRECTORY_INFORMATION)Buffer,
                                                             BufferLength);
                    break;

                case FileFullDirectoryInformation:
                    Status = VfatGetFileFullDirectoryInformation(&DirContext,
                                                                 IrpContext->DeviceExt,
                                                                 (PFILE_FULL_DIR_INFORMATION)Buffer,
                                                                 BufferLength);
                    break;

                case FileBothDirectoryInformation:
                    Status = VfatGetFileBothInformation(&DirContext,
                                                        IrpContext->DeviceExt,
                                                        (PFILE_BOTH_DIR_INFORMATION)Buffer,
                                                        BufferLength);
                    break;

                default:
                    Status = STATUS_INVALID_INFO_CLASS;
                    break;
            }

            if (Status == STATUS_BUFFER_OVERFLOW || Status == STATUS_INVALID_INFO_CLASS)
                break;
        }
        else
        {
            Status = (FirstQuery ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES);
            break;
        }

        Buffer0 = (PFILE_NAMES_INFORMATION) Buffer;
        Buffer0->FileIndex = DirContext.DirIndex;
        pCcb->Entry = ++DirContext.DirIndex;
        BufferLength -= Buffer0->NextEntryOffset;

        if (IrpContext->Stack->Flags & SL_RETURN_SINGLE_ENTRY)
            break;

        Buffer += Buffer0->NextEntryOffset;
    }

    if (Buffer0)
    {
        Buffer0->NextEntryOffset = 0;
        Status = STATUS_SUCCESS;
        IrpContext->Irp->IoStatus.Information = Stack->Parameters.QueryDirectory.Length - BufferLength;
    }

    ExReleaseResourceLite(&IrpContext->DeviceExt->DirResource);
    ExReleaseResourceLite(&pFcb->MainResource);

    return Status;
}