3535#include < Windows.h>
3636#include < WinIoCtl.h>
3737#else
38+ #if !defined(ARCH_OS_FREEBSD)
3839#include < alloca.h>
40+ #else
41+ #include < stdlib.h>
42+ #endif
3943#include < sys/mman.h>
4044#include < sys/file.h>
4145#include < unistd.h>
4246#endif
4347
48+ #if defined(ARCH_OS_FREEBSD)
49+ #include < sys/types.h>
50+ #include < sys/sysctl.h>
51+ #include < unistd.h>
52+ #include < stdlib.h>
53+ #include < sys/user.h>
54+ #endif
55+
4456PXR_NAMESPACE_OPEN_SCOPE
4557
4658using std::pair;
@@ -143,7 +155,7 @@ int ArchRmDir(const char* path)
143155bool
144156ArchStatIsWritable (const ArchStatType *st)
145157{
146- #if defined(ARCH_OS_LINUX) || defined (ARCH_OS_DARWIN) || \
158+ #if defined(ARCH_OS_LINUX) || defined (ARCH_OS_DARWIN) || defined(ARCH_OS_FREEBSD) || \
147159 defined (ARCH_OS_WASM_VM)
148160 if (st) {
149161 return (st->st_mode & S_IWOTH) ||
@@ -181,7 +193,7 @@ ArchGetModificationTime(const char* pathname, double* time)
181193double
182194ArchGetModificationTime (const ArchStatType& st)
183195{
184- #if defined(ARCH_OS_LINUX) || defined(ARCH_OS_WASM_VM)
196+ #if defined(ARCH_OS_LINUX) || defined(ARCH_OS_WASM_VM) || defined(ARCH_OS_FREEBSD)
185197 return st.st_mtim .tv_sec + 1e-9 *st.st_mtim .tv_nsec ;
186198#elif defined(ARCH_OS_DARWIN)
187199 return st.st_mtimespec .tv_sec + 1e-9 *st.st_mtimespec .tv_nsec ;
@@ -433,7 +445,7 @@ ArchGetStatMode(const char *pathname, int *mode)
433445double
434446ArchGetAccessTime (const struct stat & st)
435447{
436- #if defined(ARCH_OS_LINUX) || defined(ARCH_OS_WASM_VM)
448+ #if defined(ARCH_OS_LINUX) || defined(ARCH_OS_WASM_VM) || defined(ARCH_OS_FREEBSD)
437449 return st.st_atim .tv_sec + 1e-9 *st.st_atim .tv_nsec ;
438450#elif defined(ARCH_OS_DARWIN)
439451 return st.st_atimespec .tv_sec + 1e-9 *st.st_atimespec .tv_nsec ;
@@ -448,7 +460,7 @@ ArchGetAccessTime(const struct stat& st)
448460double
449461ArchGetStatusChangeTime (const struct stat & st)
450462{
451- #if defined(ARCH_OS_LINUX) || defined(ARCH_OS_WASM_VM)
463+ #if defined(ARCH_OS_LINUX) || defined(ARCH_OS_WASM_VM) || defined(ARCH_OS_FREEBSD)
452464 return st.st_ctim .tv_sec + 1e-9 *st.st_ctim .tv_nsec ;
453465#elif defined(ARCH_OS_DARWIN)
454466 return st.st_ctimespec .tv_sec + 1e-9 *st.st_ctimespec .tv_nsec ;
@@ -479,7 +491,7 @@ ArchGetFileLength(FILE *file)
479491 if (!file)
480492 return -1 ;
481493#if defined (ARCH_OS_LINUX) || defined (ARCH_OS_DARWIN) || \
482- defined (ARCH_OS_WASM_VM)
494+ defined (ARCH_OS_WASM_VM) || defined (ARCH_OS_FREEBSD)
483495 struct stat buf;
484496 return fstat (fileno (file), &buf) < 0 ? -1 :
485497 static_cast <int64_t >(buf.st_size );
@@ -494,7 +506,7 @@ int64_t
494506ArchGetFileLength (const char * fileName)
495507{
496508#if defined (ARCH_OS_LINUX) || defined (ARCH_OS_DARWIN) || \
497- defined (ARCH_OS_WASM_VM)
509+ defined (ARCH_OS_WASM_VM) || defined (ARCH_OS_FREEBSD)
498510 struct stat buf;
499511 return stat (fileName, &buf) < 0 ? -1 : static_cast <int64_t >(buf.st_size );
500512#elif defined (ARCH_OS_WINDOWS)
@@ -557,7 +569,49 @@ ArchGetFileName(FILE *file)
557569 std::filesystem::path (filePath.begin (), filePath.begin () + dwSize));
558570 result = ArchWindowsUtf16ToUtf8 (canonicalPath.wstring ());
559571 }
560- return result;
572+ return result;
573+ #elif defined (ARCH_OS_FREEBSD)
574+ int mib[4 ];
575+ size_t i, len;
576+ char *buffer = NULL ;
577+ struct kinfo_file *kf;
578+ string path;
579+
580+ mib[0 ] = CTL_KERN;
581+ mib[1 ] = KERN_PROC;
582+ mib[2 ] = KERN_PROC_FILEDESC;
583+ mib[3 ] = getpid ();
584+ len = 0 ;
585+ if (sysctl ( mib, sizeof (mib)/sizeof (mib[0 ]), NULL , &len, NULL , 0 ))
586+ goto end;
587+ len *= 2 ;
588+ buffer = (char *)malloc ( len );
589+ if (!buffer)
590+ goto end;
591+ if (sysctl ( mib, sizeof (mib)/sizeof (mib[0 ]), buffer, &len, NULL , 0 ))
592+ goto end;
593+
594+ for (i = 0 ; i < len; )
595+ {
596+ kf = (struct kinfo_file *) &buffer[i];
597+ if (kf->kf_structsize == 0 )
598+ break ;
599+ i += kf->kf_structsize ;
600+
601+ if (kf->kf_fd == fileno (file))
602+ {
603+ if (kf->kf_path [0 ])
604+ {
605+ path.resize (strlen (kf->kf_path ) + 1 );
606+ sprintf (&path[0 ], " %s" , kf->kf_path );
607+ }
608+ break ;
609+ }
610+ }
611+
612+ end:
613+ free ( buffer );
614+ return path;
561615#else
562616#error Unknown system architecture
563617#endif
@@ -921,6 +975,9 @@ ArchQueryMappedMemoryResidency(
921975 reinterpret_cast <caddr_t >(const_cast <void *>(addr)), len,
922976 reinterpret_cast <char *>(pageMap));
923977 return ret == 0 ;
978+ #elif defined(ARCH_OS_FREEBSD)
979+ int ret = mincore (const_cast <const void *>(addr), len, (char *)(pageMap));
980+ return ret == 0 ;
924981#endif
925982 // XXX: Not implemented for other platforms yet.
926983 return false ;
0 commit comments