Audacity  3.0.3
TempDirectory.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  TempDirectory.cpp
6 
7  Paul Licameli split from FileNames.cpp
8 
9  **********************************************************************/
10 
11 #include "TempDirectory.h"
12 
13 #include "FileNames.h"
14 #include "widgets/ErrorDialog.h"
15 
16 static wxString &TempDirPath()
17 {
18  static wxString path;
19  return path;
20 }
21 
27 {
28  auto &path = TempDirPath();
29  if (gPrefs && path.empty())
30  path =
31  gPrefs->Read(PreferenceKey(FileNames::Operation::Temp,
32  FileNames::PathType::_None), wxT(""));
33 
35  {
37  nullptr,
38  XO("Unsuitable"),
39  XO("The temporary files directory is on a FAT formatted drive.\n"
40  "Resetting to default location."),
41  "Error:_Unsuitable_drive"
42  );
43 
44  path = DefaultTempDir();
45  UpdateDefaultPath(FileNames::Operation::Temp, path);
46  }
47 
48  return FileNames::MkDir(path);
49 }
50 
52 {
53  TempDirPath().clear();
54 }
55 
58 
60 {
61  return sDefaultTempDir;
62 }
63 
65 {
66  sDefaultTempDir = tempDir;
67 }
68 
69 // We now disallow temp directory name that puts it where cleaner apps will
70 // try to clean out the files.
72 {
73  if( Name.empty() )
74  return false;
75 
76  wxFileName tmpFile;
77  tmpFile.AssignTempFileName(wxT("nn"));
78  // use Long Path to expand out any abbreviated long substrings.
79  wxString BadPath = tmpFile.GetLongPath();
80  ::wxRemoveFile(tmpFile.GetFullPath());
81 
82 #ifdef __WXMAC__
83  // This test is to fix bug 1220 on a 1.x to 2.x to 2.1.3 upgrade.
84  // It is less permissive than we could be as it stops a path
85  // with this string ANYWHERE within it rather than excluding just
86  // the paths that the earlier Audacities used to create.
87  if( Name.Contains( "/tmp/") )
88  return false;
89  BadPath = BadPath.BeforeLast( '/' ) + "/";
90  wxFileName cmpFile( Name );
91  wxString NameCanonical = cmpFile.GetLongPath( ) + "/";
92 #else
93  BadPath = BadPath.BeforeLast( '\\' ) + "\\";
94  wxFileName cmpFile( Name );
95  wxString NameCanonical = cmpFile.GetLongPath( ) + "\\";
96 #endif
97 
98  if (FATFilesystemDenied(NameCanonical,
99  XO("The temporary files directory is on a FAT formatted drive.\n"
100  "Resetting to default location.")))
101  {
102  return false;
103  }
104 
105  return !(NameCanonical.StartsWith( BadPath ));
106 }
107 
109 {
110  wxFileName fn(TempDir(),
112 
113  return fn.GetFullPath();
114 }
115 
117  const TranslatableString &msg,
118  wxWindow *window /* = nullptr */ )
119 {
121  {
123  window,
124  XO("Unsuitable"),
125  XO("%s\n\nFor tips on suitable drives, click the help button.").Format(msg),
126  "Error:_Unsuitable_drive"
127  );
128 
129  return true;
130  }
131 
132  return false;
133 }
FilePath
wxString FilePath
Definition: Identifier.h:227
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
FileNames::UnsavedProjectExtension
AUDACITY_DLL_API wxString UnsavedProjectExtension()
TempDirectory::DefaultTempDir
AUDACITY_DLL_API const FilePath & DefaultTempDir()
Definition: TempDirectory.cpp:59
fn
static const auto fn
Definition: WaveformView.cpp:1102
sDefaultTempDir
static FilePath sDefaultTempDir
Default temp directory.
Definition: TempDirectory.cpp:57
TempDirectory::ResetTempDir
AUDACITY_DLL_API void ResetTempDir()
Definition: TempDirectory.cpp:51
gPrefs
FileConfig * gPrefs
Definition: Prefs.cpp:68
FileNames::UpdateDefaultPath
AUDACITY_DLL_API void UpdateDefaultPath(Operation op, const FilePath &path)
Format
Abstract base class used in importing a file.
XO
#define XO(s)
Definition: Internat.h:31
FileNames::MkDir
AUDACITY_DLL_API wxString MkDir(const wxString &Str)
TempDirectory.h
TempDirectory::IsTempDirectoryNameOK
AUDACITY_DLL_API bool IsTempDirectoryNameOK(const FilePath &Name)
Definition: TempDirectory.cpp:71
FileNames::IsOnFATFileSystem
AUDACITY_DLL_API bool IsOnFATFileSystem(const FilePath &path)
ShowErrorDialog
void ShowErrorDialog(wxWindow *parent, const TranslatableString &dlogTitle, const TranslatableString &message, const wxString &helpPage, const bool Close, const wxString &log)
Displays an error dialog with a button that offers help.
Definition: ErrorDialog.cpp:152
TempDirectory::SetDefaultTempDir
AUDACITY_DLL_API void SetDefaultTempDir(const FilePath &tempDir)
Definition: TempDirectory.cpp:64
TempDirectory::UnsavedProjectFileName
AUDACITY_DLL_API wxString UnsavedProjectFileName()
Definition: TempDirectory.cpp:108
FileNames::CreateUniqueName
AUDACITY_DLL_API wxString CreateUniqueName(const wxString &prefix, const wxString &suffix=wxEmptyString)
FileNames.h
ErrorDialog.h
FileNames::PreferenceKey
AUDACITY_DLL_API wxString PreferenceKey(FileNames::Operation op, FileNames::PathType type)
TempDirPath
static wxString & TempDirPath()
Definition: TempDirectory.cpp:16
TempDirectory::FATFilesystemDenied
AUDACITY_DLL_API bool FATFilesystemDenied(const FilePath &path, const TranslatableString &msg, wxWindow *window=nullptr)
Definition: TempDirectory.cpp:116
anonymous_namespace{Registry.cpp}::BadPath
void BadPath(const TranslatableString &format, const wxString &key, const Identifier &name)
Definition: Registry.cpp:166
TempDirectory::TempDir
AUDACITY_DLL_API wxString TempDir()
Definition: TempDirectory.cpp:26