@@ -251,12 +251,12 @@ pub fn get_free_space(location: String) -> Result<u64, BeansError>
251251 }
252252
253253 let mut l = parse_location ( location. clone ( ) ) ;
254- while l . len ( ) >= 2 {
254+ while !l . is_empty ( ) {
255255 debug ! ( "[get_free_space] Checking if {} is in data" , l) ;
256256 if let Some ( x) = data. get ( & l) {
257257 return Ok ( x. clone ( ) ) ;
258258 }
259- l = remove_path_head ( l. clone ( ) ) ;
259+ l = remove_path_head ( l) ;
260260 }
261261
262262 Err ( BeansError :: FreeSpaceCheckFailure {
@@ -376,11 +376,32 @@ pub fn format_size(i: usize) -> String {
376376 }
377377 return format ! ( "{}{}" , whole, dec_x) ;
378378}
379+ /// Check if we should use the custom temporary directory, which is stored in the environment variable
380+ /// defined in `CUSTOM_TMPDIR_NAME`.
381+ ///
382+ /// ## Return
383+ /// `Some` when the environment variable is set, and the directory exist.
384+ /// Otherwise `None` is returned.
385+ pub fn use_custom_tmpdir ( ) -> Option < String >
386+ {
387+ if let Ok ( x) = std:: env:: var ( CUSTOM_TMPDIR_NAME ) {
388+ let s = x. to_string ( ) ;
389+ if dir_exists ( s. clone ( ) ) {
390+ return Some ( s) ;
391+ } else {
392+ warn ! ( "[use_custom_tmp_dir] Custom temporary directory \" {}\" doesn't exist" , s) ;
393+ }
394+ }
395+ return None ;
396+ }
397+ pub const CUSTOM_TMPDIR_NAME : & str = "ADASTRAL_TMPDIR" ;
379398/// Create directory in temp directory with name of "beans-rs"
380399pub fn get_tmp_dir ( ) -> String
381400{
382401 let mut dir = std:: env:: temp_dir ( ) . to_str ( ) . unwrap_or ( "" ) . to_string ( ) ;
383- if is_steamdeck ( ) {
402+ if let Some ( x) = use_custom_tmpdir ( ) {
403+ dir = x;
404+ } else if is_steamdeck ( ) {
384405 trace ! ( "[helper::get_tmp_dir] Detected that we are running on a steam deck. Using ~/.tmp/beans-rs" ) ;
385406 match simple_home_dir:: home_dir ( ) {
386407 Some ( v) => {
0 commit comments