You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014-2015 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2014-2017 KiCad Developers, see AUTHORS.TXT for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <memory>
  25. #include <build_version.h>
  26. #include <lockfile.h>
  27. #include <wx/filename.h>
  28. #include <wx/snglinst.h>
  29. std::unique_ptr<wxSingleInstanceChecker> LockFile( const wxString& aFileName )
  30. {
  31. // first make absolute and normalize, to avoid that different lock files
  32. // for the same file can be created
  33. wxFileName fn( aFileName );
  34. fn.MakeAbsolute();
  35. wxString lockFileName = fn.GetFullPath() + ".lock";
  36. lockFileName.Replace( "/", "_" );
  37. // We can have filenames coming from Windows, so also convert Windows separator
  38. lockFileName.Replace( "\\", "_" );
  39. auto p = std::make_unique<wxSingleInstanceChecker>( lockFileName,
  40. GetKicadLockFilePath() );
  41. if( p->IsAnotherRunning() )
  42. {
  43. p = nullptr;
  44. }
  45. return p;
  46. }
  47. wxString GetKicadLockFilePath()
  48. {
  49. wxFileName lockpath;
  50. lockpath.AssignDir( wxGetHomeDir() ); // Default wx behavior
  51. #if defined( __WXMAC__ )
  52. // In OSX use the standard per user cache directory
  53. lockpath.AppendDir( "Library" );
  54. lockpath.AppendDir( "Caches" );
  55. lockpath.AppendDir( "kicad" );
  56. #elif defined( __UNIX__ )
  57. wxString envstr;
  58. // Try first the standard XDG_RUNTIME_DIR, falling back to XDG_CACHE_HOME
  59. if( wxGetEnv( "XDG_RUNTIME_DIR", &envstr ) && !envstr.IsEmpty() )
  60. {
  61. lockpath.AssignDir( envstr );
  62. }
  63. else if( wxGetEnv( "XDG_CACHE_HOME", &envstr ) && !envstr.IsEmpty() )
  64. {
  65. lockpath.AssignDir( envstr );
  66. }
  67. else
  68. {
  69. // If all fails, just use ~/.cache
  70. lockpath.AppendDir( ".cache" );
  71. }
  72. lockpath.AppendDir( wxString::Format( "kicad_v%s", GetMajorMinorVersion() ) );
  73. #endif
  74. #if defined( __WXMAC__ ) || defined( __UNIX__ )
  75. if( !lockpath.DirExists() )
  76. {
  77. // Lockfiles should be only readable by the user
  78. lockpath.Mkdir( 0700, wxPATH_MKDIR_FULL );
  79. }
  80. #endif
  81. return lockpath.GetPath();
  82. }