<commit_before>/*
 * Copyright 2012 <NAME>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package net.rubygrapefruit.platform.internal;

import net.rubygrapefruit.platform.NativeException;
import net.rubygrapefruit.platform.Process;
import net.rubygrapefruit.platform.internal.jni.PosixProcessFunctions;

import java.io.File;

public class DefaultProcess implements Process {
    private final Object workingDirectoryLock = new Object();
    private final Object environmentLock = new Object();

    public int getProcessId() throws NativeException {
        return PosixProcessFunctions.getPid();
    }

    public File getWorkingDirectory() throws NativeException {
        FunctionResult result = new FunctionResult();
        String dir;
        synchronized (workingDirectoryLock) {
            dir = PosixProcessFunctions.getWorkingDirectory(result);
        }
        if (result.isFailed()) {
            throw new NativeException(String.format("Could not get process working directory: %s",
                    result.getMessage()));
        }
        return new File(dir);
    }

    public void setWorkingDirectory(File directory) throws NativeException {
        FunctionResult result = new FunctionResult();
        synchronized (workingDirectoryLock) {
            PosixProcessFunctions.setWorkingDirectory(directory.getAbsolutePath(), result);
            System.setProperty("user.dir", directory.getAbsolutePath());
        }
        if (result.isFailed()) {
            throw new NativeException(String.format("Could not set process working directory: %s",
                    result.getMessage()));
        }
    }

    public String getEnvironmentVariable(String name) throws NativeException {
        FunctionResult result = new FunctionResult();
        String value;<commit_msg>Don't set 'user.dir' if the working directory could not be set.<commit_after>/*
 * Copyright 2012 <NAME>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package net.rubygrapefruit.platform.internal;

import net.rubygrapefruit.platform.NativeException;
import net.rubygrapefruit.platform.Process;
import net.rubygrapefruit.platform.internal.jni.PosixProcessFunctions;

import java.io.File;

public class DefaultProcess implements Process {
    private final Object workingDirectoryLock = new Object();
    private final Object environmentLock = new Object();

    public int getProcessId() throws NativeException {
        return PosixProcessFunctions.getPid();
    }

    public File getWorkingDirectory() throws NativeException {
        FunctionResult result = new FunctionResult();
        synchronized (workingDirectoryLock) {
            String dir = PosixProcessFunctions.getWorkingDirectory(result);
            if (result.isFailed()) {
                throw new NativeException(String.format("Could not get process working directory: %s",
                        result.getMessage()));
            }
            return new File(dir);
        }
    }

    public void setWorkingDirectory(File directory) throws NativeException {
        FunctionResult result = new FunctionResult();
        synchronized (workingDirectoryLock) {
            PosixProcessFunctions.setWorkingDirectory(directory.getAbsolutePath(), result);
            if (result.isFailed()) {
                throw new NativeException(String.format("Could not set process working directory: %s",
                        result.getMessage()));
            }
            System.setProperty("user.dir", directory.getAbsolutePath());
        }
    }

    public String getEnvironmentVariable(String name) throws NativeException {
        FunctionResult result = new FunctionResult();
        String value;
{'fraction_non_alphanumeric': 0.07904141265503468, 'fraction_numerical': 0.0033634643682993485, 'mean_word_length': 3.011804384485666, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5630101', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1106, 'n_words': 380}