<commit_before>/*
 * Copyright 2017 balena.io
 *
 * 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.
 */

import * as childProcess from 'child_process';
import { withTmpFile } from 'etcher-sdk/build/tmp';
import { promises as fs } from 'fs';
import * as _ from 'lodash';
import * as os from 'os';
import * as semver from 'semver';
import * as sudoPrompt from '@balena/sudo-prompt';
import { promisify } from 'util';

import { sudo as catalinaSudo } from './catalina-sudo/sudo';
import * as errors from './errors';

const execAsync = promisify(childProcess.exec);
const execFileAsync = promisify(childProcess.execFile);

type Std = string | Buffer | undefined;

function sudoExecAsync(
	cmd: string,
	options: { name: string },
): Promise<{ stdout: Std; stderr: Std }> {
	return new Promise((resolve, reject) => {
		sudoPrompt.exec(
			cmd,
			options,
			(error: Error | undefined, stdout: Std, stderr: Std) => {
				if (error !== undefined) {
					reject(error);
				} else {
					resolve({ stdout, stderr });
				}
			},
		);
	});
}

/**
 * @summary The user id of the UNIX "superuser"
 */
const UNIX_SUPERUSER_USER_ID = 0;

export async function isElevated(): Promise<boolean> {
	if (os.platform() === 'win32') {
		// `fltmc` is available on WinPE, XP, Vista, 7, 8, and 10
		// Works even when the "Server" service is disabled
		// See http://stackoverflow.com/a/28268802
		try {
			await execAsync('fltmc');
		} catch (error: any) {<commit_msg>patch: fix mesage of null<commit_after>/*
 * Copyright 2017 balena.io
 *
 * 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.
 */

import * as childProcess from 'child_process';
import { withTmpFile } from 'etcher-sdk/build/tmp';
import { promises as fs } from 'fs';
import * as _ from 'lodash';
import * as os from 'os';
import * as semver from 'semver';
import * as sudoPrompt from '@balena/sudo-prompt';
import { promisify } from 'util';

import { sudo as catalinaSudo } from './catalina-sudo/sudo';
import * as errors from './errors';

const execAsync = promisify(childProcess.exec);
const execFileAsync = promisify(childProcess.execFile);

type Std = string | Buffer | undefined;

function sudoExecAsync(
	cmd: string,
	options: { name: string },
): Promise<{ stdout: Std; stderr: Std }> {
	return new Promise((resolve, reject) => {
		sudoPrompt.exec(
			cmd,
			options,
			(error: Error | undefined, stdout: Std, stderr: Std) => {
				if (error) {
					reject(error);
				} else {
					resolve({ stdout, stderr });
				}
			},
		);
	});
}

/**
 * @summary The user id of the UNIX "superuser"
 */
const UNIX_SUPERUSER_USER_ID = 0;

export async function isElevated(): Promise<boolean> {
	if (os.platform() === 'win32') {
		// `fltmc` is available on WinPE, XP, Vista, 7, 8, and 10
		// Works even when the "Server" service is disabled
		// See http://stackoverflow.com/a/28268802
		try {
			await execAsync('fltmc');
		} catch (error: any) {
{'fraction_non_alphanumeric': 0.12314007183170857, 'fraction_numerical': 0.011800923550538737, 'mean_word_length': 3.703256936067551, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '3594093', 'n_tokens_mistral': 1327, 'n_tokens_neox': 1202, 'n_words': 447}