Skip to content

Consolidate tar file extraction into one function again #3909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 15 additions & 36 deletions src/backend/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
axiosClient,
downloadFile,
execAsync,
getWineFromProton,
spawnAsync
extractFiles,
getWineFromProton
} from '../utils'
import {
execOptions,
Expand Down Expand Up @@ -60,7 +60,6 @@ interface Tool {
name: string
url: string
os: string
extractCommand: string
strip?: number
}

Expand Down Expand Up @@ -118,33 +117,17 @@ async function installOrUpdateTool(tool: Tool) {

const extractDestination = join(toolsPath, tool.name, latestVersion)
mkdirSync(extractDestination, { recursive: true })
const extractResult = await extractFiles({
path: latestVersionArchivePath,
destination: extractDestination,
strip: tool.strip ?? 1
})
rmSync(latestVersionArchivePath)

await spawnAsync(tool.extractCommand, [
latestVersion,
'-C',
`${toolsPath}/${tool.name}`
])
.then(() => {
writeFileSync(installedVersionStorage, latestVersion)
logInfo(`${tool.name} updated!`, LogPrefix.DXVKInstaller)
})
.catch((error) => {
logWarning(
[`Error when extracting ${tool.name}`, error],
LogPrefix.DXVKInstaller
)
showDialogBoxModalAuto({
title: i18next.t('box.error.dxvk.title', 'DXVK/VKD3D error'),
message: i18next.t(
'box.error.dxvk.message',
'Error installing DXVK/VKD3D! Check your connection!'
),
type: 'ERROR'
})
})
.finally(() => {
rmSync(latestVersionArchivePath)
})
if (extractResult.status === 'done') {
writeFileSync(installedVersionStorage, latestVersion)
logInfo(`${tool.name} updated!`, LogPrefix.DXVKInstaller)
}
}

export const DXVK = {
Expand All @@ -164,27 +147,23 @@ export const DXVK = {
{
name: 'vkd3d',
url: getVkd3dUrl(),
os: 'linux',
extractCommand: 'tar -xf'
os: 'linux'
},
{
name: 'dxvk',
url: getDxvkUrl(),
os: 'linux',
extractCommand: 'tar -xf'
os: 'linux'
},
{
name: 'dxvk-nvapi',
url: 'https://api.github.com/repos/jp7677/dxvk-nvapi/releases/latest',
os: 'linux',
extractCommand: 'tar --one-top-level -xf',
strip: 0
},
{
name: 'dxvk-macOS',
url: 'https://api.github.com/repos/Gcenx/DXVK-macOS/releases/latest',
os: 'darwin',
extractCommand: 'tar -xf'
os: 'darwin'
}
]

Expand Down
33 changes: 33 additions & 0 deletions src/backend/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,38 @@ function calculateEta(
return eta
}

interface ExtractOptions {
path: string
destination: string
strip: number
}

async function extractFiles({ path, destination, strip = 0 }: ExtractOptions) {
if (path.includes('.tar')) return extractTarFile({ path, destination, strip })

logError(['extractFiles: Unsupported file', path], LogPrefix.Backend)
return { status: 'error', error: 'Unsupported file type' }
}

async function extractTarFile({
path,
destination,
strip = 0
}: ExtractOptions) {
const { code, stderr } = await spawnAsync('tar', [
'-xf',
path,
'-C',
destination,
`--strip-components=${strip}`
])
if (code !== 0) {
logError(`Extracting Error: ${stderr}`, LogPrefix.Backend)
return { status: 'error', error: stderr }
}
return { status: 'done', installPath: destination }
}

const axiosClient = axios.create({
timeout: 10 * 1000,
httpsAgent: new https.Agent({ keepAlive: true })
Expand Down Expand Up @@ -1518,6 +1550,7 @@ export {
sendGameStatusUpdate,
sendProgressUpdate,
calculateEta,
extractFiles,
axiosClient
}

Expand Down
60 changes: 12 additions & 48 deletions src/backend/wine/manager/downloader/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { isMac } from '../../../constants'
import { existsSync, statSync, unlinkSync } from 'graceful-fs'
import { spawnSync, spawn } from 'child_process'
import { spawnSync } from 'child_process'

import { VersionInfo, Type, type WineManagerStatus } from 'common/types'
import { axiosClient } from 'backend/utils'
import { axiosClient, extractFiles } from 'backend/utils'

interface fetchProps {
url: string
Expand Down Expand Up @@ -140,8 +140,7 @@ interface unzipProps {
async function unzipFile({
filePath,
unzipDir,
onProgress,
abortSignal
onProgress
}: unzipProps): Promise<string> {
return new Promise((resolve, reject) => {
try {
Expand All @@ -158,52 +157,17 @@ async function unzipFile({
reject(error.message)
}

let extension_options = ''
if (filePath.endsWith('tar.gz')) {
extension_options = '-zxf'
} else if (filePath.endsWith('tar.xz')) {
extension_options = '-Jxf'
} else {
reject(`Archive type ${filePath.split('.').pop()} not supported!`)
}

const args = [
'--directory',
unzipDir,
'--strip-components=1',
extension_options,
filePath
]

const unzip = spawn('tar', args, { signal: abortSignal })
extractFiles({ path: filePath, destination: unzipDir, strip: 1 })
.then(() => {
onProgress({ status: 'idle' })
resolve(`Succesfully unzip ${filePath} to ${unzipDir}.`)
})
.catch((error) => {
onProgress({ status: 'idle' })
reject(`Unzip of ${filePath} failed with:\n ${error}!`)
})

onProgress({ status: 'unzipping' })

unzip.stdout.on('data', function () {
onProgress({ status: 'unzipping' })
})

unzip.stderr.on('data', function (stderr: string) {
onProgress({ status: 'idle' })
reject(`Unzip of ${filePath} failed with:\n ${stderr}!`)
})

unzip.on('close', function (exitcode: number) {
onProgress({ status: 'idle' })
if (exitcode !== 0) {
reject(`Unzip of ${filePath} failed with exit code:\n ${exitcode}!`)
}

resolve(`Succesfully unzip ${filePath} to ${unzipDir}.`)
})

unzip.on('error', (error: Error) => {
if (error.name.includes('AbortError')) {
reject(error.name)
} else {
reject(`Unzip of ${filePath} failed with:\n ${error.message}!`)
}
})
})
}

Expand Down
18 changes: 5 additions & 13 deletions src/backend/wine/runtimes/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { axiosClient } from 'backend/utils'
import { axiosClient, extractFiles } from 'backend/utils'
import { existsSync, mkdirSync, writeFile } from 'graceful-fs'
import { spawn } from 'child_process'

interface GithubAssetMetadata {
url: string
Expand Down Expand Up @@ -88,19 +87,12 @@ async function extractTarFile(
extractedPath = splitPath.join('.tar')
}
mkdirSync(extractedPath, { recursive: true })
const tarflags = '-Jxf'
const strip = options?.strip

return new Promise((res, rej) => {
const child = spawn('tar', [
'--directory',
extractedPath,
...(strip ? ['--strip-components', `${strip}`] : []),
tarflags,
filePath
])
child.on('close', res)
child.on('error', rej)
return extractFiles({
path: filePath,
destination: extractedPath,
strip: strip || 0
})
}

Expand Down
Loading