annotate lib/utime.c @ 40235:5a52ef2d4772

all: Update URLs to msdn.microsoft.com. * lib/stat-w32.c et al.: Update URLs after most of msdn.microsoft.com was moved to docs.microsoft.com.
author Bruno Haible <bruno@clisp.org>
date Thu, 14 Mar 2019 09:49:24 +0100
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Work around platform bugs in utime.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19595
diff changeset
2 Copyright (C) 2017-2019 Free Software Foundation, Inc.
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18873
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible. */
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #include <config.h>
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 /* Specification. */
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <utime.h>
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
24 #if defined _WIN32 && ! defined __CYGWIN__
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 # include <errno.h>
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 # include <stdbool.h>
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 # include <windows.h>
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 # include "filename.h"
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 # include "malloca.h"
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 int
18873
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
33 _gl_utimens_windows (const char *name, struct timespec ts[2])
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 /* POSIX <http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 specifies: "More than two leading <slash> characters shall be treated as
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 a single <slash> character." */
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 if (ISSLASH (name[0]) && ISSLASH (name[1]) && ISSLASH (name[2]))
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 name += 2;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 while (ISSLASH (name[1]))
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 name++;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 size_t len = strlen (name);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 size_t drive_prefix_len = (HAS_DEVICE (name) ? 2 : 0);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 /* Remove trailing slashes (except the very first one, at position
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 drive_prefix_len), but remember their presence. */
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 size_t rlen;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 bool check_dir = false;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 rlen = len;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 while (rlen > drive_prefix_len && ISSLASH (name[rlen-1]))
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 check_dir = true;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 if (rlen == drive_prefix_len + 1)
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 break;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 rlen--;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 const char *rname;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 char *malloca_rname;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 if (rlen == len)
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 rname = name;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 malloca_rname = NULL;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 else
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 malloca_rname = malloca (rlen + 1);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 if (malloca_rname == NULL)
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 errno = ENOMEM;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 return -1;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 memcpy (malloca_rname, name, rlen);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 malloca_rname[rlen] = '\0';
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 rname = malloca_rname;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 DWORD error;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 /* Open a handle to the file.
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 CreateFile
40235
5a52ef2d4772 all: Update URLs to msdn.microsoft.com.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
86 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilea>
5a52ef2d4772 all: Update URLs to msdn.microsoft.com.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
87 <https://docs.microsoft.com/en-us/windows/desktop/FileIO/creating-and-opening-files> */
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 HANDLE handle =
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 CreateFile (rname,
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 NULL,
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 OPEN_EXISTING,
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 /* FILE_FLAG_POSIX_SEMANTICS (treat file names that differ only
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 in case as different) makes sense only when applied to *all*
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 filesystem operations. */
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 FILE_FLAG_BACKUP_SEMANTICS /* | FILE_FLAG_POSIX_SEMANTICS */,
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 NULL);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 if (handle == INVALID_HANDLE_VALUE)
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 error = GetLastError ();
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 goto failed;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 if (check_dir)
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 /* GetFileAttributes
40235
5a52ef2d4772 all: Update URLs to msdn.microsoft.com.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
108 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileattributesa> */
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 DWORD attributes = GetFileAttributes (rname);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 if (attributes == INVALID_FILE_ATTRIBUTES)
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 error = GetLastError ();
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 CloseHandle (handle);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 goto failed;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 CloseHandle (handle);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 if (malloca_rname != NULL)
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 freea (malloca_rname);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 errno = ENOTDIR;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 return -1;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 /* Use SetFileTime(). See
40235
5a52ef2d4772 all: Update URLs to msdn.microsoft.com.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
128 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-setfiletime>
5a52ef2d4772 all: Update URLs to msdn.microsoft.com.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
129 <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-filetime> */
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 FILETIME last_access_time;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 FILETIME last_write_time;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 if (ts == NULL)
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 /* GetSystemTimeAsFileTime is the same as
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 GetSystemTime followed by SystemTimeToFileTime.
40235
5a52ef2d4772 all: Update URLs to msdn.microsoft.com.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
136 <https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getsystemtimeasfiletime>.
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 It would be overkill to use
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 GetSystemTimePreciseAsFileTime
40235
5a52ef2d4772 all: Update URLs to msdn.microsoft.com.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
139 <https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime>. */
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 FILETIME current_time;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 GetSystemTimeAsFileTime (&current_time);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 last_access_time = current_time;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 last_write_time = current_time;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 else
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 ULONGLONG time_since_16010101 =
18873
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
149 (ULONGLONG) ts[0].tv_sec * 10000000 + ts[0].tv_nsec / 100 + 116444736000000000LL;
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 last_access_time.dwLowDateTime = (DWORD) time_since_16010101;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 last_access_time.dwHighDateTime = time_since_16010101 >> 32;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 ULONGLONG time_since_16010101 =
18873
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
155 (ULONGLONG) ts[1].tv_sec * 10000000 + ts[1].tv_nsec / 100 + 116444736000000000LL;
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 last_write_time.dwLowDateTime = (DWORD) time_since_16010101;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 last_write_time.dwHighDateTime = time_since_16010101 >> 32;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 if (SetFileTime (handle, NULL, &last_access_time, &last_write_time))
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 CloseHandle (handle);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 if (malloca_rname != NULL)
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 freea (malloca_rname);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 return 0;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 else
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 #if 0
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 DWORD sft_error = GetLastError ();
18873
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
171 fprintf (stderr, "utimens SetFileTime error 0x%x\n", (unsigned int) sft_error);
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 #endif
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 CloseHandle (handle);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 if (malloca_rname != NULL)
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 freea (malloca_rname);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 errno = EINVAL;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 return -1;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 failed:
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 #if 0
18873
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
184 fprintf (stderr, "utimens CreateFile/GetFileAttributes error 0x%x\n", (unsigned int) error);
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 #endif
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 if (malloca_rname != NULL)
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 freea (malloca_rname);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 switch (error)
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 {
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 /* Some of these errors probably cannot happen with the specific flags
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 that we pass to CreateFile. But who knows... */
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 case ERROR_FILE_NOT_FOUND: /* The last component of rname does not exist. */
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 case ERROR_PATH_NOT_FOUND: /* Some directory component in rname does not exist. */
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 case ERROR_BAD_PATHNAME: /* rname is such as '\\server'. */
18871
3c73d7ec23d2 utime: Handle more Windows error codes.
Bruno Haible <bruno@clisp.org>
parents: 18832
diff changeset
196 case ERROR_BAD_NETPATH: /* rname is such as '\\nonexistentserver\share'. */
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 case ERROR_BAD_NET_NAME: /* rname is such as '\\server\nonexistentshare'. */
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 case ERROR_INVALID_NAME: /* rname contains wildcards, misplaced colon, etc. */
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 case ERROR_DIRECTORY:
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 errno = ENOENT;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 break;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 case ERROR_ACCESS_DENIED: /* rname is such as 'C:\System Volume Information\foo'. */
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 case ERROR_SHARING_VIOLATION: /* rname is such as 'C:\pagefile.sys'. */
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 errno = (ts != NULL ? EPERM : EACCES);
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 break;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 case ERROR_OUTOFMEMORY:
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 errno = ENOMEM;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 break;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 case ERROR_WRITE_PROTECT:
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 errno = EROFS;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 break;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 case ERROR_WRITE_FAULT:
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 case ERROR_READ_FAULT:
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218 case ERROR_GEN_FAILURE:
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 errno = EIO;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 break;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 case ERROR_BUFFER_OVERFLOW:
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223 case ERROR_FILENAME_EXCED_RANGE:
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 errno = ENAMETOOLONG;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 break;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 case ERROR_DELETE_PENDING: /* XXX map to EACCESS or EPERM? */
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 errno = EPERM;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 break;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 default:
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 errno = EINVAL;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 break;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 return -1;
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 }
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239
18873
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
240 int
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
241 utime (const char *name, const struct utimbuf *ts)
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
242 {
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
243 if (ts == NULL)
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
244 return _gl_utimens_windows (name, NULL);
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
245 else
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
246 {
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
247 struct timespec ts_with_nanoseconds[2];
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
248 ts_with_nanoseconds[0].tv_sec = ts->actime;
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
249 ts_with_nanoseconds[0].tv_nsec = 0;
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
250 ts_with_nanoseconds[1].tv_sec = ts->modtime;
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
251 ts_with_nanoseconds[1].tv_nsec = 0;
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
252 return _gl_utimens_windows (name, ts_with_nanoseconds);
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
253 }
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
254 }
626a5740518f utimens: On native Windows, support 100ns resolution also if fd < 0.
Bruno Haible <bruno@clisp.org>
parents: 18871
diff changeset
255
18832
99fa25ea15a5 utime: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
256 #endif