Skip to content

Commit

Permalink
win32: use xmalloc_follow_symlinks() in stat(2)
Browse files Browse the repository at this point in the history
Commit 31467dd (win32: changes to stat(2) implementation) followed
symlinks manually.  Unfortunately the implementation was incorrect.
Use xmalloc_follow_symlinks() instead.

Saves 32-48 bytes.
  • Loading branch information
rmyorston committed Sep 27, 2022
1 parent 182e489 commit 1b3002f
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions win32/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ static int do_lstat(int follow, const char *file_name, struct mingw_stat *buf)
WIN32_FIND_DATAA findbuf;
DWORD low, high;
off64_t size;
char lname[PATH_MAX];
char *lname = NULL;
#if ENABLE_FEATURE_EXTRA_FILE_DATA
DWORD flags;
BY_HANDLE_FILE_INFORMATION hdata;
Expand All @@ -646,30 +646,22 @@ static int do_lstat(int follow, const char *file_name, struct mingw_stat *buf)
buf->st_tag = get_symlink_data(buf->st_attr, file_name, &findbuf);

if (buf->st_tag) {
ssize_t len = readlink(file_name, lname, PATH_MAX);
if (len < 0) {
err = errno;
break;
} else if (len == PATH_MAX) {
errno = ENAMETOOLONG;
break;
}
ssize_t len;
char content[PATH_MAX];

if (follow) {
/* The file size and times are wrong when Windows follows
* a symlink. Use the symlink target instead. */
if (follow++ > MAXSYMLINKS) {
err = ELOOP;
break;
}
lname[len] = '\0';
file_name = lname;
file_name = lname = xmalloc_follow_symlinks(file_name);
if (!lname)
return -1;
continue;
}

/* Get the contents of a symlink, not its target. */
buf->st_mode = S_IFLNK|S_IRWXU|S_IRWXG|S_IRWXO;
buf->st_size = len;
len = readlink(file_name, content, PATH_MAX);
buf->st_size = (len < 0 || len == PATH_MAX) ? 0 : len;
buf->st_atim = filetime_to_timespec(&(findbuf.ftLastAccessTime));
buf->st_mtim = filetime_to_timespec(&(findbuf.ftLastWriteTime));
buf->st_ctim = filetime_to_timespec(&(findbuf.ftCreationTime));
Expand Down Expand Up @@ -733,6 +725,7 @@ static int do_lstat(int follow, const char *file_name, struct mingw_stat *buf)
buf->st_blocks = ((size+4095)>>12)<<3;
return 0;
}
free(lname);
errno = err;
return -1;
}
Expand Down

0 comments on commit 1b3002f

Please sign in to comment.