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.

97 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 CHANGELOG.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 <lockfile.h>
  25. #include <wx/filename.h>
  26. #include <wx/snglinst.h>
  27. #include <common.h>
  28. std::unique_ptr<wxSingleInstanceChecker> LockFile( const wxString& aFileName )
  29. {
  30. // first make absolute and normalize, to avoid that different lock files
  31. // for the same file can be created
  32. wxFileName fn( aFileName );
  33. fn.MakeAbsolute();
  34. wxString lockFileName = fn.GetFullPath() + ".lock";
  35. lockFileName.Replace( "/", "_" );
  36. // We can have filenames coming from Windows, so also convert Windows separator
  37. lockFileName.Replace( "\\", "_" );
  38. auto p = std::make_unique<wxSingleInstanceChecker>( lockFileName,
  39. GetKicadLockFilePath() );
  40. if( p->IsAnotherRunning() )
  41. {
  42. p = nullptr;
  43. }
  44. return p;
  45. }
  46. wxString GetKicadLockFilePath()
  47. {
  48. wxFileName lockpath;
  49. lockpath.AssignDir( wxGetHomeDir() ); // Default wx behavior
  50. #if defined( __WXMAC__ )
  51. // In OSX use the standard per user cache directory
  52. lockpath.AppendDir( "Library" );
  53. lockpath.AppendDir( "Caches" );
  54. lockpath.AppendDir( "kicad" );
  55. #elif defined( __UNIX__ )
  56. wxString envstr;
  57. // Try first the standard XDG_RUNTIME_DIR, falling back to XDG_CACHE_HOME
  58. if( wxGetEnv( "XDG_RUNTIME_DIR", &envstr ) && !envstr.IsEmpty() )
  59. {
  60. lockpath.AssignDir( envstr );
  61. }
  62. else if( wxGetEnv( "XDG_CACHE_HOME", &envstr ) && !envstr.IsEmpty() )
  63. {
  64. lockpath.AssignDir( envstr );
  65. }
  66. else
  67. {
  68. // If all fails, just use ~/.cache
  69. lockpath.AppendDir( ".cache" );
  70. }
  71. lockpath.AppendDir( "kicad" );
  72. #endif
  73. #if defined( __WXMAC__ ) || defined( __UNIX__ )
  74. if( !lockpath.DirExists() )
  75. {
  76. // Lockfiles should be only readable by the user
  77. lockpath.Mkdir( 0700, wxPATH_MKDIR_FULL );
  78. }
  79. #endif
  80. return lockpath.GetPath();
  81. }