{"thread":{"id":"15688","subject":"[PATCH 3/4] mingw: move common functionality to win32.h","startedAt":"2008-09-27T08:43:01Z","lastAt":"2008-09-29T18:37:22Z","messageCount":5,"participants":["Dmitry Potapov","Johannes Sixt"],"isPatch":true,"patchVersion":1,"patchTotal":4},"messages":[{"id":"91736","messageId":"20080927084301.GB21650@dpotapov.dyndns.org","threadId":"15688","inReplyTo":null,"subject":"[PATCH 3/4] mingw: move common functionality to win32.h","fromName":"Dmitry Potapov","fromEmail":"dpotapov@gmail.com","sentAt":"2008-09-27T08:43:01Z","receivedAt":"2008-09-27T08:43:01Z","isPatch":true,"sender":{"key":"dpotapov@gmail.com","avatar":"https://avatars.githubusercontent.com/u/6568595?v=4"},"body":"Some small Win32 specific functions will be shared by MinGW and\nCygwin compatibility layer. Place them into a separate header.\n\nSigned-off-by: Dmitry Potapov <dpotapov@gmail.com>\n---\n compat/mingw.c |   42 ++++--------------------------------------\n compat/win32.h |   34 ++++++++++++++++++++++++++++++++++\n 2 files changed, 38 insertions(+), 38 deletions(-)\n create mode 100644 compat/win32.h\n\ndiff --git a/compat/mingw.c b/compat/mingw.c\nindex a2b8cd7..ac77283 100644\n--- a/compat/mingw.c\n+++ b/compat/mingw.c\n@@ -1,4 +1,5 @@\n #include \"../git-compat-util.h\"\n+#include \"win32.h\"\n #include \"../strbuf.h\"\n \n unsigned int _CRT_fmode = _O_BINARY;\n@@ -39,46 +40,19 @@ static int do_lstat(const char *file_name, struct stat *buf)\n {\n \tWIN32_FILE_ATTRIBUTE_DATA fdata;\n \n-\tif (GetFileAttributesExA(file_name, GetFileExInfoStandard, &fdata)) {\n-\t\tint fMode = S_IREAD;\n-\t\tif (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)\n-\t\t\tfMode |= S_IFDIR;\n-\t\telse\n-\t\t\tfMode |= S_IFREG;\n-\t\tif (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY))\n-\t\t\tfMode |= S_IWRITE;\n-\n+\tif (!(errno = get_file_attr(file_name, &fdata))) {\n \t\tbuf->st_ino = 0;\n \t\tbuf->st_gid = 0;\n \t\tbuf->st_uid = 0;\n \t\tbuf->st_nlink = 1;\n-\t\tbuf->st_mode = fMode;\n+\t\tbuf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);\n \t\tbuf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */\n \t\tbuf->st_dev = buf->st_rdev = 0; /* not used by Git */\n \t\tbuf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));\n \t\tbuf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));\n \t\tbuf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));\n-\t\terrno = 0;\n \t\treturn 0;\n \t}\n-\n-\tswitch (GetLastError()) {\n-\tcase ERROR_ACCESS_DENIED:\n-\tcase ERROR_SHARING_VIOLATION:\n-\tcase ERROR_LOCK_VIOLATION:\n-\tcase ERROR_SHARING_BUFFER_EXCEEDED:\n-\t\terrno = EACCES;\n-\t\tbreak;\n-\tcase ERROR_BUFFER_OVERFLOW:\n-\t\terrno = ENAMETOOLONG;\n-\t\tbreak;\n-\tcase ERROR_NOT_ENOUGH_MEMORY:\n-\t\terrno = ENOMEM;\n-\t\tbreak;\n-\tdefault:\n-\t\terrno = ENOENT;\n-\t\tbreak;\n-\t}\n \treturn -1;\n }\n \n@@ -130,19 +104,11 @@ int mingw_fstat(int fd, struct stat *buf)\n \t\treturn fstat(fd, buf);\n \n \tif (GetFileInformationByHandle(fh, &fdata)) {\n-\t\tint fMode = S_IREAD;\n-\t\tif (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)\n-\t\t\tfMode |= S_IFDIR;\n-\t\telse\n-\t\t\tfMode |= S_IFREG;\n-\t\tif (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY))\n-\t\t\tfMode |= S_IWRITE;\n-\n \t\tbuf->st_ino = 0;\n \t\tbuf->st_gid = 0;\n \t\tbuf->st_uid = 0;\n \t\tbuf->st_nlink = 1;\n-\t\tbuf->st_mode = fMode;\n+\t\tbuf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);\n \t\tbuf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */\n \t\tbuf->st_dev = buf->st_rdev = 0; /* not used by Git */\n \t\tbuf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));\ndiff --git a/compat/win32.h b/compat/win32.h\nnew file mode 100644\nindex 0000000..c26384e\n--- /dev/null\n+++ b/compat/win32.h\n@@ -0,0 +1,34 @@\n+/* common Win32 functions for MinGW and Cygwin */\n+#include <windows.h>\n+\n+static inline int file_attr_to_st_mode (DWORD attr)\n+{\n+\tint fMode = S_IREAD;\n+\tif (attr & FILE_ATTRIBUTE_DIRECTORY)\n+\t\tfMode |= S_IFDIR;\n+\telse\n+\t\tfMode |= S_IFREG;\n+\tif (!(attr & FILE_ATTRIBUTE_READONLY))\n+\t\tfMode |= S_IWRITE;\n+\treturn fMode;\n+}\n+\n+static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fdata)\n+{\n+\tif (GetFileAttributesExA(fname, GetFileExInfoStandard, fdata))\n+\t\treturn 0;\n+\n+\tswitch (GetLastError()) {\n+\tcase ERROR_ACCESS_DENIED:\n+\tcase ERROR_SHARING_VIOLATION:\n+\tcase ERROR_LOCK_VIOLATION:\n+\tcase ERROR_SHARING_BUFFER_EXCEEDED:\n+\t\treturn EACCES;\n+\tcase ERROR_BUFFER_OVERFLOW:\n+\t\treturn ENAMETOOLONG;\n+\tcase ERROR_NOT_ENOUGH_MEMORY:\n+\t\treturn ENOMEM;\n+\tdefault:\n+\t\treturn ENOENT;\n+\t}\n+}\n-- \n1.6.0.2.237.g0297e5\n"},{"id":"91745","messageId":"200809272034.04931.johannes.sixt@telecom.at","threadId":"15688","inReplyTo":"20080927084301.GB21650@dpotapov.dyndns.org","subject":"Re: [PATCH 3/4] mingw: move common functionality to win32.h","fromName":"Johannes Sixt","fromEmail":"johannes.sixt@telecom.at","sentAt":"2008-09-27T18:34:04Z","receivedAt":"2008-09-27T18:34:04Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"On Samstag, 27. September 2008, Dmitry Potapov wrote:\n> +static inline int get_file_attr(const char *fname,\n> WIN32_FILE_ATTRIBUTE_DATA *fdata) +{\n> +\tif (GetFileAttributesExA(fname, GetFileExInfoStandard, fdata))\n> +\t\treturn 0;\n> +\n> +\tswitch (GetLastError()) {\n> +\tcase ERROR_ACCESS_DENIED:\n> +\tcase ERROR_SHARING_VIOLATION:\n> +\tcase ERROR_LOCK_VIOLATION:\n> +\tcase ERROR_SHARING_BUFFER_EXCEEDED:\n> +\t\treturn EACCES;\n> +\tcase ERROR_BUFFER_OVERFLOW:\n> +\t\treturn ENAMETOOLONG;\n> +\tcase ERROR_NOT_ENOUGH_MEMORY:\n> +\t\treturn ENOMEM;\n> +\tdefault:\n> +\t\treturn ENOENT;\n> +\t}\n> +}\n\nI've long wished for a function that translates Win32 error codes to errno \ncodes. It would be useful in a number of other places, too.\n\nHere you introduce a new function get_file_attr() that is nothing but \nGetFileAttributesExA() followed by such an error code translation.\n\nI suggest that we leave the original call to GetFileAttributesExA() alone and \nhave a function win32_to_errno(void) that is just the switch statement above.\n\n-- Hannes\n"},{"id":"91749","messageId":"20080927215102.GF21650@dpotapov.dyndns.org","threadId":"15688","inReplyTo":"200809272034.04931.johannes.sixt@telecom.at","subject":"Re: [PATCH 3/4] mingw: move common functionality to win32.h","fromName":"Dmitry Potapov","fromEmail":"dpotapov@gmail.com","sentAt":"2008-09-27T21:51:02Z","receivedAt":"2008-09-27T21:51:02Z","isPatch":true,"sender":{"key":"dpotapov@gmail.com","avatar":"https://avatars.githubusercontent.com/u/6568595?v=4"},"body":"On Sat, Sep 27, 2008 at 08:34:04PM +0200, Johannes Sixt wrote:\n> On Samstag, 27. September 2008, Dmitry Potapov wrote:\n> > +static inline int get_file_attr(const char *fname,\n> > WIN32_FILE_ATTRIBUTE_DATA *fdata) +{\n> > +\tif (GetFileAttributesExA(fname, GetFileExInfoStandard, fdata))\n> > +\t\treturn 0;\n> > +\n> > +\tswitch (GetLastError()) {\n> > +\tcase ERROR_ACCESS_DENIED:\n> > +\tcase ERROR_SHARING_VIOLATION:\n> > +\tcase ERROR_LOCK_VIOLATION:\n> > +\tcase ERROR_SHARING_BUFFER_EXCEEDED:\n> > +\t\treturn EACCES;\n> > +\tcase ERROR_BUFFER_OVERFLOW:\n> > +\t\treturn ENAMETOOLONG;\n> > +\tcase ERROR_NOT_ENOUGH_MEMORY:\n> > +\t\treturn ENOMEM;\n> > +\tdefault:\n> > +\t\treturn ENOENT;\n> > +\t}\n> > +}\n> \n> I've long wished for a function that translates Win32 error codes to errno \n> codes. It would be useful in a number of other places, too.\n> \n> Here you introduce a new function get_file_attr() that is nothing but \n> GetFileAttributesExA() followed by such an error code translation.\n> \n> I suggest that we leave the original call to GetFileAttributesExA() alone and \n> have a function win32_to_errno(void) that is just the switch statement above.\n\nwin32_to_errno was the first thing that implemented but then released\nthat translation of Win32 errors to errno cannot be in general case.\nFor instance, ERROR_BUFFER_OVERFLOW means ENAMETOOLONG here, but it\ncan be translated to ETOOSMALL in other cases. How do you propose to\ndeal with that?\n\nSo I have not found a better solution than to add get_file_attr(), which\ncalls GetFileAttributesExA() and translates Win32 error.\n\n\nDmitry\n"},{"id":"91775","messageId":"200809281110.48256.johannes.sixt@telecom.at","threadId":"15688","inReplyTo":"20080927215102.GF21650@dpotapov.dyndns.org","subject":"Re: [PATCH 3/4] mingw: move common functionality to win32.h","fromName":"Johannes Sixt","fromEmail":"johannes.sixt@telecom.at","sentAt":"2008-09-28T09:10:48Z","receivedAt":"2008-09-28T09:10:48Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"On Samstag, 27. September 2008, Dmitry Potapov wrote:\n> win32_to_errno was the first thing that implemented but then released\n> that translation of Win32 errors to errno cannot be in general case.\n> For instance, ERROR_BUFFER_OVERFLOW means ENAMETOOLONG here, but it\n> can be translated to ETOOSMALL in other cases. How do you propose to\n> deal with that?\n\nWe deal with that when the need arises, in an evolutionary manner. The first \nstep is to *have* an error code translation routine.\n\n-- Hannes\n"},{"id":"91870","messageId":"20080929183722.GJ21650@dpotapov.dyndns.org","threadId":"15688","inReplyTo":"200809281110.48256.johannes.sixt@telecom.at","subject":"Re: [PATCH 3/4] mingw: move common functionality to win32.h","fromName":"Dmitry Potapov","fromEmail":"dpotapov@gmail.com","sentAt":"2008-09-29T18:37:22Z","receivedAt":"2008-09-29T18:37:22Z","isPatch":true,"sender":{"key":"dpotapov@gmail.com","avatar":"https://avatars.githubusercontent.com/u/6568595?v=4"},"body":"On Sun, Sep 28, 2008 at 11:10:48AM +0200, Johannes Sixt wrote:\n> On Samstag, 27. September 2008, Dmitry Potapov wrote:\n> > win32_to_errno was the first thing that implemented but then released\n> > that translation of Win32 errors to errno cannot be in general case.\n> > For instance, ERROR_BUFFER_OVERFLOW means ENAMETOOLONG here, but it\n> > can be translated to ETOOSMALL in other cases. How do you propose to\n> > deal with that?\n> \n> We deal with that when the need arises, in an evolutionary manner. The first \n> step is to *have* an error code translation routine.\n\nStep to what? IMHO, the idea of win32_to_errno is deeply flawed, and, in\nany case, refactoring handling of Win32 error in MinGW is not the\npurpose of my series. If you want to introduce win32_to_errno in mingw,\nyou can send your own patch to that effect, and we can discuss that\nseparately. So far, I am not convinced that it will improve anything in\nthe existing code. As to avoiding duplication of Win32 specific code,\nget_file_attr() fits better.  So, let's proceed step-wise, and first\nfinish one thing, namely, speed-up of Cygwin version of Git and then\ndiscuss adding win32_to_errno to MinGW.\n\n\nDmitry\n"}]}