获取Linux系统临时目录文件(类似Windows下的GetTempPath)

本文介绍了一个在Linux系统中创建临时文件的方法,通过使用mktemp函数指定以/tmp/tbsXXXXXX为模板的临时文件路径。

char szTempFile[] = "/tmp/tbsXXXXXX";
char *pszTempFile = mktemp(szTempFile);
return pszTempFile;

如上,Linux系统的默认临时目录是/tmp

PS C:\Users\Administrator> # CurlTools.psm1 >> # ============== >> # 终极修复版模块主文件 >> >> # 初始化模块变量 >> $script:Config = $null >> $script:CurlPath = $null >> $script:ConfigDir = $null >> $script:ConfigPath = $null >> >> # 检测操作系统 - 终极兼容版 >> function script:Get-Platform { >> try { >> if ($PSVersionTable.PSEdition -eq "Core") { >> if ($IsWindows) { return "Windows" } >> elseif ($IsLinux) { return "Linux" } >> elseif ($IsMacOS) { return "macOS" } >> } else { >> # PowerShell 5.1 兼容处理 >> if ($env:OS -like "Windows*") { >> return "Windows" >> } elseif (Test-Path "/etc/os-release") { >> return "Linux" >> } else { >> return "macOS" >> } >> } >> } catch { >> return "Windows" # 默认返回Windows >> } >> } >> >> # 配置目录获取 - 无环境变量依赖 >> function script:Get-ConfigDir { >> $platform = Get-Platform >> $tempDir = [System.IO.Path]::GetTempPath() >> >> try { >> switch ($platform) { >> "Linux" { >> $homeDir = [Environment]::GetFolderPath('UserProfile') >> if (-not $homeDir) { $homeDir = "/home/$env:USER" } >> $configDir = "$homeDir/.config/curltools" >> } >> "macOS" { >> $homeDir = [Environment]::GetFolderPath('UserProfile') >> if (-not $homeDir) { $homeDir = "/Users/$env:USER" } >> $configDir = "$homeDir/Library/Application Support/CurlTools" >> } >> default { >> $appData = [Environment]::GetFolderPath('ApplicationData') >> if (-not $appData) { >> $appData = "$env:USERPROFILE\AppData\Roaming" >> } >> $configDir = "$appData\CurlTools" >> } >> } >> } catch { >> $configDir = "$tempDir\CurlTools" >> } >> >> # 确保目录存在 >> if (-not (Test-Path $configDir)) { >> New-Item -ItemType Directory -Path $configDir -Force | Out-Null >> } >> return $configDir >> } >> >> # curl路径检测 - 无依赖终极版 >> function script:Get-DefaultCurlPath { >> $platform = Get-Platform >> $knownPaths = @{ >> Windows = @( >> "C:\Windows\System32", >> "C:\Program Files\curl\bin", >> "C:\curl\bin", >> "$env:USERPROFILE\curl\bin", >> "C:\Program Files\Git\usr\bin", >> "C:\Program Files\Git\mingw64\bin" >> ) >> Linux = @("/usr/bin", "/usr/local/bin", "/bin", "/snap/bin") >> macOS = @("/usr/local/bin", "/opt/homebrew/bin", "/usr/bin", "/bin") >> } >> >> # 尝试所有已知路径 >> foreach ($path in $knownPaths[$platform]) { >> $curlExe = if ($platform -eq "Windows") { "curl.exe" } else { "curl" } >> $fullPath = Join-Path $path $curlExe >> if (Test-Path $fullPath -PathType Leaf) { >> return $path >> } >> } >> >> # 终极后备方案 - 搜索整个磁盘 >> if ($platform -eq "Windows") { >> $drives = Get-PSDrive -PSProvider FileSystem | Select-Object -ExpandProperty Root >> foreach ($drive in $drives) { >> $curlPath = Get-ChildItem -Path $drive -Filter "curl.exe" -Recurse -ErrorAction SilentlyContinue | >> Select-Object -First 1 -ExpandProperty DirectoryName >> if ($curlPath) { return $curlPath } >> } >> } >> >> return $null >> } >> >> # curl自动安装函数 >> function script:Install-Curl { >> $platform = Get-Platform >> $tempDir = [System.IO.Path]::GetTempPath() >> >> try { >> if ($platform -eq "Windows") { >> $curlZip = "$tempDir\curl.zip" >> $curlUrl = "https://curl.se/windows/dl-8.8.0_5/curl-8.8.0_5-win64-mingw.zip" >> >> # 下载并解压 >> Invoke-WebRequest -Uri $curlUrl -OutFile $curlZip -ErrorAction Stop >> Expand-Archive -Path $curlZip -DestinationPath "$tempDir\curl" -Force >> >> # 设置路径 >> $curlPath = "$tempDir\curl\curl-8.8.0_5-win64-mingw\bin" >> return $curlPath >> } else { >> # Linux/macOS 使用包管理器 >> if ($platform -eq "Linux") { >> if (Get-Command apt -ErrorAction SilentlyContinue) { >> Start-Process -Wait -NoNewWindow -FilePath "sudo" -ArgumentList "apt", "update" >> Start-Process -Wait -NoNewWindow -FilePath "sudo" -ArgumentList "apt", "install", "-y", "curl" >> } elseif (Get-Command yum -ErrorAction SilentlyContinue) { >> Start-Process -Wait -NoNewWindow -FilePath "sudo" -ArgumentList "yum", "install", "-y", "curl" >> } >> } else { >> # macOS >> if (Get-Command brew -ErrorAction SilentlyContinue) { >> brew install curl >> } else { >> Start-Process -Wait -NoNewWindow -FilePath "/bin/bash" -ArgumentList "-c", "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" >> brew install curl >> } >> } >> >> # 获取安装路径 >> return (Get-Command curl -ErrorAction SilentlyContinue).Source | Split-Path >> } >> } catch { >> Write-Warning "curl自动安装失败: $_" >> return $null >> } >> } >> >> # 模块初始化 - 无依赖实现 >> function script:Initialize-ModuleConfig { >> try { >> # 获取配置目录 >> $script:ConfigDir = Get-ConfigDir >> $script:ConfigPath = Join-Path -Path $script:ConfigDir -ChildPath "config.json" >> >> # 加载或创建默认配置 >> if (Test-Path $script:ConfigPath) { >> try { >> $script:Config = Get-Content $script:ConfigPath | ConvertFrom-Json >> } catch { >> $script:Config = $null >> } >> } >> >> if (-not $script:Config) { >> $defaultPath = Get-DefaultCurlPath >> >> if (-not $defaultPath) { >> # 终极后备方案 - 下载curl >> $defaultPath = Install-Curl >> } >> >> $script:Config = [PSCustomObject]@{ >> CurlPath = $defaultPath >> LastUpdate = (Get-Date).ToString("o") >> AutoUpdate = $true >> } >> $script:Config | ConvertTo-Json | Set-Content $script:ConfigPath -Force >> } >> >> # 设置模块路径 >> $script:CurlPath = $script:Config.CurlPath >> return $true >> } catch { >> return $false >> } >> } >> >> # ========== 公共函数定义 ========== >> function Get-CurlPath { >> # 延迟初始化 >> if (-not $script:Config) { >> Initialize-ModuleConfig | Out-Null >> } >> return $script:CurlPath >> } >> >> function Set-CurlPath { >> param( >> [Parameter(Mandatory=$true)] >> [string]$Path >> ) >> # 延迟初始化 >> if (-not $script:Config) { >> Initialize-ModuleConfig | Out-Null >> } >> >> $script:CurlPath = $Path >> $env:Path = "$Path;$env:Path" >> Write-Host "手动设置curl路径: $Path" -ForegroundColor Green >> >> # 更新配置 >> if ($script:Config) { >> $script:Config.CurlPath = $Path >> $script:Config | ConvertTo-Json | Set-Content $script:ConfigPath -Force >> } >> } >> >> function Get-CurlVersion { >> # 延迟初始化 >> if (-not $script:Config) { >> Initialize-ModuleConfig | Out-Null >> } >> >> $curlExe = if ((Get-Platform) -eq "Windows") { "curl.exe" } else { "curl" } >> $fullPath = Join-Path $script:CurlPath $curlExe >> & $fullPath --version | Select-Object -First 1 >> } >> >> function Invoke-SecureDownload { >> param( >> [Parameter(Mandatory=$true)] >> [string]$Url, >> >> [Parameter(Mandatory=$true)] >> [string]$OutputPath, >> >> [string]$ExpectedHash, >> [string]$HashAlgorithm = "SHA256" >> ) >> >> try { >> # 下载文件 >> Write-Verbose "下载文件: $Url" >> $ProgressPreference = 'SilentlyContinue' >> Invoke-WebRequest -Uri $Url -OutFile $OutputPath -UseBasicParsing >> >> # 验证哈希 >> if (-not [string]::IsNullOrWhiteSpace($ExpectedHash)) { >> $actualHash = (Get-FileHash -Path $OutputPath -Algorithm $HashAlgorithm).Hash >> if ($actualHash -ne $ExpectedHash) { >> Remove-Item -Path $OutputPath -Force >> throw "文件哈希不匹配! 期望: $ExpectedHash, 实际: $actualHash" >> } >> } >> >> return $OutputPath >> } catch { >> Write-Error "下载失败: $_" >> return $null >> } >> } >> >> # ========== 模块初始化 ========== >> # 使用脚本作用域函数进行初始化 >> try { >> $initSuccess = Initialize-ModuleConfig >> if (-not $initSuccess) { >> # 终极后备方案 - 使用临时curl >> $tempCurl = Install-Curl >> if ($tempCurl) { >> $script:CurlPath = $tempCurl >> Write-Warning "使用临时curl路径: $script:CurlPath" >> } else { >> Write-Warning "模块初始化失败且无法安装curl" >> } >> } >> } catch { >> Write-Warning "模块初始化错误: $_" >> } >> >> # ========== 模块导出区域 ========== >> # 必须在所有函数定义后,模块末尾执行 >> $exportFunctions = @( >> 'Get-CurlPath' >> 'Set-CurlPath' >> 'Get-CurlVersion' >> 'Invoke-SecureDownload' >> ) >> >> Export-ModuleMember -Function $exportFunctions >> Export-ModuleMember : 只能从模块内调用 Export-ModuleMember cmdlet。 所在位置 行:294 字符: 1 + Export-ModuleMember -Function $exportFunctions + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (:) [Export-ModuleMember], InvalidOperationException + FullyQualifiedErrorId : Modules_CanOnlyExecuteExportModuleMemberInsideAModule,Microsoft.PowerShell.Commands.Expo rtModuleMemberCommand PS C:\Users\Administrator>
最新发布
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值