gnu-arch-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Gnu-arch-users] Arch On Cygwin(Win32)


From: Robert Collins
Subject: Re: [Gnu-arch-users] Arch On Cygwin(Win32)
Date: Mon, 17 Nov 2003 08:14:33 +1100
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5b) Gecko/20030723 Thunderbird/0.1

Robert Collins wrote:

Parker, Ron wrote:

-----Original Message-----
From: Robert Collins [mailto:address@hidden



Well, I'm happy for you to shoot over the patches to me as they are.
I'll see what they do for me :).



You asked for it.  Here are 5 patches against cygwin 1.5.5-1:


Here's my current state. remaining TODO:

And the rest of it... I forgot to include the new file, and provisional changelog.

Rob
2003-11-11  Robert Collins <address@hidden>
            Ron Parker <address@hidden>

        Rename thunked functions to function_name, 
        create unicode capable thunks, and add autoload support, for:

          CopyFile
          CreateFile
          CreateDirectory
          CreateHardLink
          DeleteFile
          FindFirstFile
          FindNextFile
          RemoveDirectory
          SetFileAttributes
          GetFileAttributes
          MoveFile
          MoveFileEx
        
        * assert.cc: Ditto.
        * dcrt0.cc: Ditto.
        * dir.cc: Ditto.
        * exceptions.cc: Ditto.
        * fhandler.cc: Ditto.
        * fhandler_console.cc: Ditto.
        * fhandler_disk_file.cc: Ditto.
        * fhandler_proc.cc: Ditto.
        * fhandler_socket.cc: Ditto.
        * fork.cc: Ditto.
        * ntea.cc: Ditto.
        * path.cc: Ditto.
        * security.cc: Ditto.
        * spawn.cc: Ditto.
        * syscalls.cc: Ditto.
        * times.cc: Ditto.
        * uinfo.cc: Ditto.
        * autoload.cc: Add appropriate ...W autoload stubs.
        * wincap.cc:  Add has_unicode_io capability.
        * wincap.h:  Add has_unicode_io capability.
        * winsup.h: Change CYG_MAX_PATH to 300.
/* cygio.h

   Copyright 2003 Robert Collins  <address@hidden>
   Copyright 2003 Ron Parker      <address@hidden>

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#ifndef _IO_H_
#define _IO_H_


/* this file is a tuhnk layer to automatically use unicode calls when possible.
 * ALL routines presume that valid memory checks have already been carried out.
 */

inline bool filename_is_dos(LPCSTR file_name)
{
  return file_name[1] == ':';
}

class IOThunkState
{
  public:
    inline IOThunkState(LPCSTR file_name);
    inline ~IOThunkState();
    enum Condition {FAILED, ANSI, WIDE} condition;
    inline WCHAR *getWide();
    inline size_t ansiLength() const;
    inline size_t wideLength() const;

    /* Not implemented */
    inline IOThunkState(IOThunkState const &);
    inline IOThunkState& operator = (IOThunkState const &);
  private:
    size_t len;
    WCHAR *wfilename;  
    LPCSTR file_name;/* pointer used during object life */
};

inline void trace_file_wcs(IOThunkState &);

IOThunkState::IOThunkState(LPCSTR filename): wfilename (NULL), 
file_name(filename)
{
  len = strlen(file_name);
  /* If it exceeds ANSI call length, fail. */
  if (!wincap.has_unicode_io() && len > MAX_PATH) 
    {
      SetLastError(111); /* The file name is too long. */
      condition = FAILED;
      return;
    }
  /* Call the ansi call if possible / required */
  if (!wincap.has_unicode_io() || !filename_is_dos(file_name)
      || len <= MAX_PATH)
    {
      condition = ANSI;
      return;
    }
  /* we need to use unicode to support this call */
  getWide();
}

WCHAR *
IOThunkState::getWide()
{
  if (wfilename != NULL || condition == FAILED)
    return wfilename;
  if ((wfilename = (WCHAR *)malloc (sizeof(WCHAR) * (len + 4 + 1))) == NULL)
    {
      SetLastError(111); /* The file name is too long. */
      condition = FAILED;
      return NULL;
    }
  condition = WIDE;
  /* And convert */
  if (sys_mbstowcs (wfilename, "\\\\?\\", wideLength()) == 0 ||
      sys_mbstowcs (wfilename + 4, file_name, wideLength() - 5) == 0)
  {
    debug_printf("sys_mbstowcs failed %E");
    condition = FAILED;
    free (wfilename);
    wfilename = NULL;
  }
  return wfilename;
}

IOThunkState::~IOThunkState()
{
  if (wfilename)
    free(wfilename);
}

size_t 
IOThunkState::ansiLength() const
{
  return len;
}

size_t 
IOThunkState::wideLength() const
{
  return 2 * (len + 5);
}

inline void
trace_file_wcs (IOThunkState &what)
{
#if 0
  /* Diagnostic stuff if in doubt */
  HANDLE h = CreateFileA("C:\\,,rdp", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 
FILE_ATTRIBUTE_NORMAL, NULL);
  SetFilePointer(h, 0, 0, FILE_END);
  DWORD written;
  WriteFile(h, what.getWide(), what.wideLength(), &written, NULL);
  WriteFile(h, L"\r\n", 4, &written, NULL);
  CloseHandle(h);
#endif
}

inline BOOL
copy_file (LPCSTR oldfilename, LPCSTR newfilename, BOOL fail_if_exists)
{
  /* 0 is failure */
  return CopyFileA (oldfilename, newfilename, fail_if_exists);
}

inline HANDLE
create_file (LPCSTR filename, DWORD access, DWORD share_mode,
                                  LPSECURITY_ATTRIBUTES sec_attr,
                                  DWORD disposition, DWORD flags,
                                  HANDLE template_file)
{
  IOThunkState state(filename);
  switch (state.condition) 
  {
    case IOThunkState::FAILED:
      return 0;
    case IOThunkState::ANSI:
      return CreateFileA (filename, access, share_mode, sec_attr, disposition,
                       flags, template_file);
    case IOThunkState::WIDE: 
      trace_file_wcs(state);
      return CreateFileW (state.getWide(), access, share_mode, sec_attr, 
disposition,
                      flags, template_file);
  };
}

inline BOOL
create_directory (LPCSTR filename, LPSECURITY_ATTRIBUTES sec_attr)
{
  IOThunkState state(filename);
  switch (state.condition) 
  {
    case IOThunkState::FAILED:
      return 0;
    case IOThunkState::ANSI:
      return CreateDirectoryA (filename, sec_attr);
    case IOThunkState::WIDE:
      return CreateDirectoryW (state.getWide(), sec_attr);
  };
}

inline BOOL
create_hard_link (LPCSTR newfilename, LPCSTR oldfilename, 
        LPSECURITY_ATTRIBUTES sec_attr)
{
  /* 0 is failure */
  return CreateHardLinkA (newfilename, oldfilename, sec_attr);
}

inline BOOL
delete_file(LPCSTR filename)
{
  /* 0 is failure */
  return DeleteFileA(filename);
}

inline BOOL 
remove_directory(LPCSTR filename)
{
  /* 0 is failure */
  return RemoveDirectoryA(filename);
}

inline HANDLE
find_first_file (LPCSTR filename, LPWIN32_FIND_DATA data)
{
  /* INVALID_HANDLE_VALUE is failure */
  return FindFirstFile (filename, data);
}


inline BOOL
find_next_file (HANDLE handle, LPWIN32_FIND_DATA data)
{
  /* 0 is failure */
  return FindNextFileA (handle, data);
}

inline DWORD
get_file_attributes(LPCSTR filename)
{
  IOThunkState state(filename);
  switch (state.condition) 
  {
    case IOThunkState::FAILED:
      return INVALID_FILE_ATTRIBUTES;
    case IOThunkState::ANSI:
      return GetFileAttributesA (filename);
    case IOThunkState::WIDE:
      return GetFileAttributesW (state.getWide());
  };
}

inline BOOL
set_file_attributes(LPCSTR filename, DWORD attr)
{
  IOThunkState state(filename);
  switch (state.condition) 
  {
    case IOThunkState::FAILED:
      return 0;
    case IOThunkState::ANSI:
      return SetFileAttributesA(filename, attr);
    case IOThunkState::WIDE:
      return SetFileAttributesW(state.getWide(), attr);
  };
}

inline BOOL
move_file(LPCSTR oldfilename, LPCSTR newfilename)
{
  IOThunkState oldstate(oldfilename);
  IOThunkState newstate(newfilename);

  if (oldstate.condition == IOThunkState::WIDE || newstate.condition == 
IOThunkState::WIDE)
  {
    /* get the wide string / trigger failures that may occur */
    oldstate.getWide();
    newstate.getWide();
  }
  if (oldstate.condition == IOThunkState::FAILED || newstate.condition == 
IOThunkState::FAILED)
    return 0;
  if (oldstate.condition == IOThunkState::ANSI)
    return MoveFileA(oldfilename,newfilename);
  return MoveFileW(oldstate.getWide(), newstate.getWide());
};

inline BOOL
move_file_ex(LPCSTR oldfilename, LPCSTR newfilename, DWORD flags)
{
  IOThunkState oldstate(oldfilename);
  IOThunkState newstate(newfilename);

  if (oldstate.condition == IOThunkState::WIDE || newstate.condition == 
IOThunkState::WIDE)
  {
    /* get the wide string / trigger failures that may occur */
    oldstate.getWide();
    newstate.getWide();
  }
  if (oldstate.condition == IOThunkState::FAILED || newstate.condition == 
IOThunkState::FAILED)
    return 0;
  if (oldstate.condition == IOThunkState::ANSI)
    return MoveFileExA(oldfilename,newfilename, flags);
  return MoveFileExW(oldstate.getWide(), newstate.getWide(), flags);
};

#endif /* _IO_H_ */

reply via email to

[Prev in Thread] Current Thread [Next in Thread]