Skip to content

Commit

Permalink
Implemented an partial emulation of fstatat for OS X
Browse files Browse the repository at this point in the history
fstatat does not exists under OS X and it was replaced by a dummy
stub. This resulted in symlinks being seen as unknown file type
when reading directory entries. Ultimately, this leads to the
inability to glob symlinks under OS X.

--
MOS_MIGRATED_REVID=89304190
  • Loading branch information
damienmg authored and hanwen committed Mar 24, 2015
1 parent b10e95f commit 4ccfb20
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/main/native/unix_jni_darwin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syslimits.h>

#include <string>

const int PATH_MAX2 = PATH_MAX * 2;

using std::string;

// See unix_jni.h.
Expand All @@ -33,9 +38,39 @@ string ErrorMessage(int error_number) {
return string(buf);
}


int portable_fstatat(int dirfd, char *name, struct stat *statbuf, int flags) {
errno = ENOSYS;
return -1;
char dirPath[PATH_MAX2]; // Have enough room for relative path

// No fstatat under darwin, simulate it
if (flags != 0) {
// We don't support any flags
errno = ENOSYS;
return -1;
}
if (strlen(name) == 0 || name[0] == '/') {
// Absolute path, simply stat
return stat(name, statbuf);
}
// Relative path, construct an absolute path
if (fcntl(dirfd, F_GETPATH, dirPath) == -1) {
return -1;
}
int l = strlen(dirPath);
if (dirPath[l-1] != '/') {
// dirPath is twice the PATH_MAX size, we always have room for the extra /
dirPath[l] = '/';
dirPath[l+1] = 0;
l++;
}
strncat(dirPath, name, PATH_MAX2-l-1);
char *newpath = realpath(dirPath, NULL); // this resolve the relative path
if (newpath == NULL) {
return -1;
}
int r = stat(newpath, statbuf);
free(newpath);
return r;
}

int StatSeconds(const struct stat &statbuf, StatTimes t) {
Expand Down

0 comments on commit 4ccfb20

Please sign in to comment.