forked from microsoft/team-explorer-everywhere
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement native_misc library for macOS and Linux
- Loading branch information
Showing
17 changed files
with
355 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...soft.tfs.jni/src/com/microsoft/tfs/jni/internal/platformmisc/LinuxNativePlatformMisc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.microsoft.tfs.jni.internal.platformmisc; | ||
|
||
import com.microsoft.tfs.jni.internal.unix.linux.LibC; | ||
import com.microsoft.tfs.jni.internal.unix.linux.passwd; | ||
import com.sun.jna.platform.linux.XAttr; | ||
import com.sun.jna.ptr.PointerByReference; | ||
|
||
public class LinuxNativePlatformMisc extends UnixNativePlatformMisc { | ||
|
||
private static final LibC libC = LibC.INSTANCE; | ||
|
||
protected LinuxNativePlatformMisc() { | ||
super(libC); | ||
} | ||
|
||
@Override | ||
public String getHomeDirectory(String username) { | ||
passwd pwd = new passwd(); | ||
byte[] pwdBuffer = new byte[1024]; | ||
PointerByReference tempPwdPtr = new PointerByReference(); | ||
if (libC.getpwnam_r(username, pwd, pwdBuffer, new XAttr.size_t(pwdBuffer.length), tempPwdPtr) != 0 | ||
|| tempPwdPtr.getValue() == null) | ||
return null; | ||
|
||
passwd tempPwd = new passwd(tempPwdPtr.getValue()); | ||
return tempPwd.pw_dir; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...soft.tfs.jni/src/com/microsoft/tfs/jni/internal/platformmisc/MacOsNativePlatformMisc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.microsoft.tfs.jni.internal.platformmisc; | ||
|
||
import com.microsoft.tfs.jni.internal.unix.macos.*; | ||
import com.sun.jna.platform.linux.XAttr; | ||
import com.sun.jna.platform.mac.CoreFoundation; | ||
import com.sun.jna.ptr.IntByReference; | ||
import com.sun.jna.ptr.PointerByReference; | ||
|
||
public class MacOsNativePlatformMisc extends UnixNativePlatformMisc { | ||
|
||
private static final LibC libC = LibC.INSTANCE; | ||
private static final CoreFoundation coreFoundation = CoreFoundation.INSTANCE; | ||
private static final CoreServices coreServices = CoreServices.INSTANCE; | ||
private static final SystemConfiguration systemConfiguration = SystemConfiguration.INSTANCE; | ||
|
||
protected MacOsNativePlatformMisc() { | ||
super(libC); | ||
} | ||
|
||
@Override | ||
public String getHomeDirectory(String username) { | ||
passwd pwd = new passwd(); | ||
byte[] pwdBuffer = new byte[1024]; | ||
PointerByReference tempPwdPtr = new PointerByReference(); | ||
if (libC.getpwnam_r(username, pwd, pwdBuffer, new XAttr.size_t(pwdBuffer.length), tempPwdPtr) != 0 | ||
|| tempPwdPtr.getValue() == null) | ||
return null; | ||
|
||
passwd tempPwd = new passwd(tempPwdPtr.getValue()); | ||
return tempPwd.pw_dir; | ||
} | ||
|
||
/** | ||
* This will attempt to return the HostName system preference (10.4+). If that fails, this will attempt to return | ||
* the LocalHostName system preference (aka, the Bonjour name as configured in Sharing preferences; 10.3+). This is | ||
* to work around MacOS's dynamic hostname. If both of those fail this uses the Unix hostname. | ||
*/ | ||
@Override | ||
public String getComputerName() { | ||
IntByReference osVersion = new IntByReference(); | ||
if (coreServices.Gestalt(CoreServices.gestaltSystemVersion, osVersion) != 0) | ||
osVersion.setValue(0); | ||
|
||
if (osVersion.getValue() >= 0x1040) { | ||
CoreFoundation.CFStringRef microsoft = CoreFoundation.CFStringRef.createCFString("Microsoft"); | ||
try { | ||
SCPreferencesRef configuration = systemConfiguration.SCPreferencesCreate(null, microsoft, null); | ||
if (configuration != null) { | ||
try { | ||
CoreFoundation.CFStringRef hostName = | ||
systemConfiguration.SCPreferencesGetHostName(configuration); | ||
if (hostName != null) { | ||
return hostName.stringValue(); | ||
} | ||
} finally { | ||
coreFoundation.CFRelease(configuration); | ||
} | ||
} | ||
} finally { | ||
CoreFoundation.INSTANCE.CFRelease(microsoft); | ||
} | ||
} | ||
|
||
if (osVersion.getValue() >= 0x1030) { | ||
CoreFoundation.CFStringRef hostName = systemConfiguration.SCDynamicStoreCopyLocalHostName(null); | ||
if (hostName != null) { | ||
try { | ||
return hostName.stringValue(); | ||
} finally { | ||
coreFoundation.CFRelease(hostName); | ||
} | ||
} | ||
} | ||
|
||
return super.getComputerName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...osoft.tfs.jni/src/com/microsoft/tfs/jni/internal/platformmisc/UnixNativePlatformMisc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.microsoft.tfs.jni.internal.platformmisc; | ||
|
||
import com.microsoft.tfs.jni.PlatformMisc; | ||
import com.microsoft.tfs.jni.internal.unix.LibC; | ||
import com.microsoft.tfs.jni.internal.unix.linux.passwd; | ||
import com.sun.jna.platform.linux.XAttr; | ||
import com.sun.jna.platform.unix.LibCAPI; | ||
import com.sun.jna.ptr.PointerByReference; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
public abstract class UnixNativePlatformMisc implements PlatformMisc { | ||
|
||
private final LibC libC; | ||
|
||
protected UnixNativePlatformMisc(LibC libC) { | ||
this.libC = libC; | ||
} | ||
|
||
@Override | ||
public boolean changeCurrentDirectory(String directory) { | ||
return libC.chdir(directory) == 0; | ||
} | ||
|
||
@Override | ||
public int getDefaultCodePage() { | ||
throw new RuntimeException("Platform not supported"); | ||
} | ||
|
||
@Override | ||
public String getComputerName() { | ||
byte[] buffer = new byte[LibCAPI.HOST_NAME_MAX]; | ||
if (libC.gethostname(buffer, buffer.length) != 0) { | ||
return null; | ||
} | ||
|
||
return new String(buffer, Charset.defaultCharset()); | ||
} | ||
|
||
@Override | ||
public String getEnvironmentVariable(String name) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String expandEnvironmentString(String value) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getCurrentIdentityUser() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getWellKnownSID(int wellKnownSIDType, String domainSIDString) { | ||
return null; | ||
} | ||
} |
15 changes: 7 additions & 8 deletions
15
...t/tfs/jni/internal/console/unix/LibC.java → ...microsoft/tfs/jni/internal/unix/LibC.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,26 @@ | ||
package com.microsoft.tfs.jni.internal.console.unix; | ||
package com.microsoft.tfs.jni.internal.unix; | ||
|
||
import com.sun.jna.Library; | ||
import com.sun.jna.Native; | ||
import com.sun.jna.Platform; | ||
import com.sun.jna.Pointer; | ||
import com.sun.jna.Structure; | ||
import com.sun.jna.platform.linux.XAttr; | ||
import com.sun.jna.ptr.PointerByReference; | ||
|
||
public interface LibC extends Library { | ||
|
||
LibC INSTANCE = (LibC) Native.loadLibrary("c", LibC.class); | ||
|
||
public interface LibC extends com.sun.jna.platform.unix.LibC { | ||
int O_RDONLY = 0; | ||
long TIOCGWINSZ = Platform.isLinux() ? 0x5413L : 0x40087468L; | ||
int STDIN_FILENO = 0; | ||
int ECHO = 8; | ||
int TCSANOW = 0; | ||
|
||
int open(String pathname, int flags); | ||
|
||
int close(int fd); | ||
|
||
int ioctl(int fd, long cmd, winsize p); | ||
|
||
int tcgetattr(int fd, termios termios_p); | ||
|
||
int tcsetattr(int fd, int optional_actions, termios termios_p); | ||
|
||
int chdir(String path); | ||
} |
11 changes: 11 additions & 0 deletions
11
source/com.microsoft.tfs.jni/src/com/microsoft/tfs/jni/internal/unix/linux/LibC.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.microsoft.tfs.jni.internal.unix.linux; | ||
|
||
import com.sun.jna.Native; | ||
import com.sun.jna.platform.linux.XAttr; | ||
import com.sun.jna.ptr.PointerByReference; | ||
|
||
public interface LibC extends com.microsoft.tfs.jni.internal.unix.LibC { | ||
LibC INSTANCE = Native.load("c", LibC.class); | ||
|
||
int getpwnam_r(String name, passwd pwd, byte[] buf, XAttr.size_t buflen, PointerByReference result); | ||
} |
Oops, something went wrong.