Code

Merge branch 'sb/dashless'
[git.git] / compat / mingw.c
index 6b742873da066b6e48e14519cb6f2ed42e5fc524..c0bc849e45dc511202d1e7602f570de47630e6aa 100644 (file)
@@ -31,6 +31,11 @@ static inline time_t filetime_to_time_t(const FILETIME *ft)
        return (time_t)winTime;
 }
 
+static inline size_t size_to_blocks(size_t s)
+{
+       return (s+511)/512;
+}
+
 extern int _getdrive( void );
 /* We keep the do_lstat code in a separate function to avoid recursion.
  * When a path ends with a slash, the stat will fail with ENOENT. In
@@ -52,10 +57,10 @@ static int do_lstat(const char *file_name, struct stat *buf)
                buf->st_ino = 0;
                buf->st_gid = 0;
                buf->st_uid = 0;
-               buf->st_nlink = 1;
                buf->st_mode = fMode;
                buf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */
-               buf->st_dev = buf->st_rdev = (_getdrive() - 1);
+               buf->st_blocks = size_to_blocks(buf->st_size);
+               buf->st_dev = _getdrive() - 1;
                buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
                buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
                buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
@@ -89,7 +94,7 @@ static int do_lstat(const char *file_name, struct stat *buf)
  * complete. Note that Git stat()s are redirected to mingw_lstat()
  * too, since Windows doesn't really handle symlinks that well.
  */
-int mingw_lstat(const char *file_name, struct stat *buf)
+int mingw_lstat(const char *file_name, struct mingw_stat *buf)
 {
        int namelen;
        static char alt_name[PATH_MAX];
@@ -117,7 +122,8 @@ int mingw_lstat(const char *file_name, struct stat *buf)
 }
 
 #undef fstat
-int mingw_fstat(int fd, struct stat *buf)
+#undef stat
+int mingw_fstat(int fd, struct mingw_stat *buf)
 {
        HANDLE fh = (HANDLE)_get_osfhandle(fd);
        BY_HANDLE_FILE_INFORMATION fdata;
@@ -127,8 +133,22 @@ int mingw_fstat(int fd, struct stat *buf)
                return -1;
        }
        /* direct non-file handles to MS's fstat() */
-       if (GetFileType(fh) != FILE_TYPE_DISK)
-               return fstat(fd, buf);
+       if (GetFileType(fh) != FILE_TYPE_DISK) {
+               struct stat st;
+               if (fstat(fd, &st))
+                       return -1;
+               buf->st_ino = st.st_ino;
+               buf->st_gid = st.st_gid;
+               buf->st_uid = st.st_uid;
+               buf->st_mode = st.st_mode;
+               buf->st_size = st.st_size;
+               buf->st_blocks = size_to_blocks(buf->st_size);
+               buf->st_dev = st.st_dev;
+               buf->st_atime = st.st_atime;
+               buf->st_mtime = st.st_mtime;
+               buf->st_ctime = st.st_ctime;
+               return 0;
+       }
 
        if (GetFileInformationByHandle(fh, &fdata)) {
                int fMode = S_IREAD;
@@ -142,10 +162,10 @@ int mingw_fstat(int fd, struct stat *buf)
                buf->st_ino = 0;
                buf->st_gid = 0;
                buf->st_uid = 0;
-               buf->st_nlink = 1;
                buf->st_mode = fMode;
                buf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */
-               buf->st_dev = buf->st_rdev = (_getdrive() - 1);
+               buf->st_blocks = size_to_blocks(buf->st_size);
+               buf->st_dev = _getdrive() - 1;
                buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
                buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
                buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
@@ -155,6 +175,33 @@ int mingw_fstat(int fd, struct stat *buf)
        return -1;
 }
 
+static inline void time_t_to_filetime(time_t t, FILETIME *ft)
+{
+       long long winTime = t * 10000000LL + 116444736000000000LL;
+       ft->dwLowDateTime = winTime;
+       ft->dwHighDateTime = winTime >> 32;
+}
+
+int mingw_utime (const char *file_name, const struct utimbuf *times)
+{
+       FILETIME mft, aft;
+       int fh, rc;
+
+       /* must have write permission */
+       if ((fh = open(file_name, O_RDWR | O_BINARY)) < 0)
+               return -1;
+
+       time_t_to_filetime(times->modtime, &mft);
+       time_t_to_filetime(times->actime, &aft);
+       if (!SetFileTime((HANDLE)_get_osfhandle(fh), NULL, &aft, &mft)) {
+               errno = EINVAL;
+               rc = -1;
+       } else
+               rc = 0;
+       close(fh);
+       return rc;
+}
+
 unsigned int sleep (unsigned int seconds)
 {
        Sleep(seconds*1000);
@@ -319,6 +366,19 @@ char *mingw_getcwd(char *pointer, int len)
        return ret;
 }
 
+#undef getenv
+char *mingw_getenv(const char *name)
+{
+       char *result = getenv(name);
+       if (!result && !strcmp(name, "TMPDIR")) {
+               /* on Windows it is TMP and TEMP */
+               result = getenv("TMP");
+               if (!result)
+                       result = getenv("TEMP");
+       }
+       return result;
+}
+
 /*
  * See http://msdn2.microsoft.com/en-us/library/17w5ykft(vs.71).aspx
  * (Parsing C++ Command-Line Arguments)
@@ -957,3 +1017,25 @@ sig_handler_t mingw_signal(int sig, sig_handler_t handler)
        timer_fn = handler;
        return old;
 }
+
+static const char *make_backslash_path(const char *path)
+{
+       static char buf[PATH_MAX + 1];
+       char *c;
+
+       if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
+               die("Too long path: %.*s", 60, path);
+
+       for (c = buf; *c; c++) {
+               if (*c == '/')
+                       *c = '\\';
+       }
+       return buf;
+}
+
+void mingw_open_html(const char *unixpath)
+{
+       const char *htmlpath = make_backslash_path(unixpath);
+       printf("Launching default browser to display HTML ...\n");
+       ShellExecute(NULL, "open", htmlpath, NULL, "\\", 0);
+}