Skip to content

Commit

Permalink
User-scope tmp folder on Linux (microsoft/vscode-remote-release#2347)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmarti committed Jun 27, 2022
1 parent 208ebb3 commit 664ecb2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/spec-common/cliHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface CLIHost {
mkdirp(dirpath: string): Promise<void>;
readDir(dirpath: string): Promise<string[]>;
readDirWithTypes?(dirpath: string): Promise<[string, FileTypeBitmask][]>;
getUsername(): Promise<string>;
getuid(): Promise<number>;
getgid(): Promise<number>;
toCommonURI(filePath: string): Promise<URI | undefined>;
Expand Down Expand Up @@ -78,6 +79,7 @@ function createLocalCLIHostFromExecFunctions(localCwd: string, exec: ExecFunctio
await mkdirpLocal(dirpath);
},
readDir: readLocalDir,
getUsername: async () => os.userInfo().username,
getuid: async () => process.getuid(),
getgid: async () => process.getgid(),
toCommonURI: async (filePath) => URI.file(filePath),
Expand Down
4 changes: 2 additions & 2 deletions src/spec-node/containerFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { LogLevel, makeLog, toErrorText } from '../spec-utils/log';
import { FeaturesConfig, getContainerFeaturesFolder, getContainerFeaturesBaseDockerFile, getFeatureLayers, getFeatureMainValue, getFeatureValueObject, generateFeaturesConfig, getSourceInfoString, collapseFeaturesConfig, Feature, multiStageBuildExploration } from '../spec-configuration/containerFeaturesConfiguration';
import { readLocalFile } from '../spec-utils/pfs';
import { includeAllConfiguredFeatures } from '../spec-utils/product';
import { createFeaturesTempFolder, DockerResolverParameters, getFolderImageName, inspectDockerImage } from './utils';
import { createFeaturesTempFolder, DockerResolverParameters, getCacheFolder, getFolderImageName, inspectDockerImage } from './utils';
import { isEarlierVersion, parseVersion } from '../spec-common/commonUtils';

// Escapes environment variable keys.
Expand Down Expand Up @@ -347,7 +347,7 @@ export async function updateRemoteUserUID(params: DockerResolverParameters, conf
const dockerfileName = 'updateUID.Dockerfile';
const srcDockerfile = path.join(common.extensionPath, 'scripts', dockerfileName);
const version = common.package.version;
const destDockerfile = cliHost.path.join(await cliHost.tmpdir(), 'vsch', `${dockerfileName}-${version}`);
const destDockerfile = cliHost.path.join(await getCacheFolder(cliHost), `${dockerfileName}-${version}`);
const tmpDockerfile = `${destDockerfile}-${Date.now()}`;
await cliHost.mkdirp(cliHost.path.dirname(tmpDockerfile));
await cliHost.writeFile(tmpDockerfile, await readLocalFile(srcDockerfile));
Expand Down
11 changes: 10 additions & 1 deletion src/spec-node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import * as path from 'path';
import * as crypto from 'crypto';
import * as os from 'os';

import { ContainerError, toErrorText } from '../spec-common/errors';
import { CLIHost, runCommandNoPty, runCommand } from '../spec-common/commonUtils';
Expand Down Expand Up @@ -318,11 +319,19 @@ export async function createFeaturesTempFolder(params: { cliHost: CLIHost; packa
const { cliHost } = params;
const { version } = params.package;
// Create temp folder
const tmpFolder: string = cliHost.path.join(await cliHost.tmpdir(), 'vsch', 'container-features', `${version}-${Date.now()}`);
const tmpFolder: string = cliHost.path.join(await getCacheFolder(cliHost), 'container-features', `${version}-${Date.now()}`);
await cliHost.mkdirp(tmpFolder);
return tmpFolder;
}

export async function getCacheFolder(cliHost: CLIHost): Promise<string> {
return cliHost.path.join(await cliHost.tmpdir(), cliHost.platform === 'linux' ? `vsch-${await cliHost.getUsername()}` : 'vsch');
}

export function getLocalCacheFolder(): string {
return path.join(os.tmpdir(), process.platform === 'linux' ? `vsch-${os.userInfo().username}` : 'vsch');
}

// not expected to be called externally (exposed for testing)
export function ensureDockerfileHasFinalStageName(dockerfile: string, defaultLastStageName: string): { lastStageName: string; modifiedDockerfile: string | undefined } {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { assert } from 'chai';
import { generateFeaturesConfig, getFeatureLayers } from '../../spec-configuration/containerFeaturesConfiguration';
import { createPlainLog, LogLevel, makeLog } from '../../spec-utils/log';
import * as os from 'os';
import * as path from 'path';
import { mkdirpLocal } from '../../spec-utils/pfs';
import { DevContainerConfig } from '../../spec-configuration/configuration';
import { URI } from 'vscode-uri';
import { getLocalCacheFolder } from '../../spec-node/utils';

export const output = makeLog(createPlainLog(text => process.stdout.write(text), () => LogLevel.Trace));

Expand Down Expand Up @@ -35,7 +35,7 @@ describe('validate (offline) generateFeaturesConfig()', function () {
it('should correctly return a featuresConfig with just local features', async function () {

const version = 'unittest';
const tmpFolder: string = path.join(os.tmpdir(), 'vsch', 'container-features', `${version}-${Date.now()}`);
const tmpFolder: string = path.join(getLocalCacheFolder(), 'container-features', `${version}-${Date.now()}`);
await mkdirpLocal(tmpFolder);

const config: DevContainerConfig = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { assert } from 'chai';
import { generateFeaturesConfig } from '../../spec-configuration/containerFeaturesConfiguration';
import { createPlainLog, LogLevel, makeLog } from '../../spec-utils/log';
import * as os from 'os';
import * as path from 'path';
import { mkdirpLocal } from '../../spec-utils/pfs';
import { DevContainerConfig } from '../../spec-configuration/configuration';
import { URI } from 'vscode-uri';
import { getLocalCacheFolder } from '../../spec-node/utils';

export const output = makeLog(createPlainLog(text => process.stdout.write(text), () => LogLevel.Trace));

Expand Down Expand Up @@ -34,7 +34,7 @@ describe('validate online functionality of generateFeaturesConfig() ', function

it('should correct return a featuresConfig fetched from a remote tgz', async function () {
const version = 'unittest2';
const tmpFolder: string = path.join(os.tmpdir(), 'vsch', 'container-features', `${version}-${Date.now()}`);
const tmpFolder: string = path.join(getLocalCacheFolder(), 'container-features', `${version}-${Date.now()}`);
await mkdirpLocal(tmpFolder);

const config: DevContainerConfig = {
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('validate online functionality of generateFeaturesConfig() ', function
it('should correctly return a featuresConfig with github-hosted remote features from two remote repos', async function () {

const version = 'unittest3';
const tmpFolder: string = path.join(os.tmpdir(), 'vsch', 'container-features', `${version}-${Date.now()}`);
const tmpFolder: string = path.join(getLocalCacheFolder(), 'container-features', `${version}-${Date.now()}`);
await mkdirpLocal(tmpFolder);

const config: DevContainerConfig = {
Expand Down

0 comments on commit 664ecb2

Please sign in to comment.