fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / find_session.cc
index 4388d8e2991a341927be2bab4ad2ff0b9d80d6d5..af39a475277a940ae6d0fc95d5b4cb8c8d49de3a 100644 (file)
@@ -1,39 +1,59 @@
+/*
+    Copyright (C) 2012 Paul Davis
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
 #include <unistd.h>
-#include <sys/stat.h>
 
 #include <cstring>
 #include <climits>
 #include <cerrno>
 
-#include <pbd/compose.h>
-#include <pbd/error.h>
+#include "pbd/gstdio_compat.h"
+
+#include <glibmm/miscutils.h>
 
-#include <ardour/session_utils.h>
-#include <ardour/filename_extensions.h>
-#include <ardour/utils.h>
+#include "pbd/compose.h"
+#include "pbd/pathexpand.h"
+#include "pbd/error.h"
 
-#include "i18n.h"
+#include "ardour/filename_extensions.h"
+#include "ardour/utils.h"
+#include "ardour/session_utils.h"
 
+#include "pbd/i18n.h"
+
+using namespace std;
 using namespace PBD;
 
+namespace ARDOUR {
+
 int
-ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
+find_session (string str, string& path, string& snapshot, bool& isnew)
 {
-       struct stat statbuf;
-       char buf[PATH_MAX+1];
+       GStatBuf statbuf;
 
        isnew = false;
 
-       if (!realpath (str.c_str(), buf) && (errno != ENOENT && errno != ENOTDIR)) {
-               error << string_compose (_("Could not resolve path: %1 (%2)"), buf, strerror(errno)) << endmsg;
-               return -1;
-       }
+       str = canonical_path (str);
 
-       str = buf;
-       
        /* check to see if it exists, and what it is */
 
-       if (stat (str.c_str(), &statbuf)) {
+       if (g_stat (str.c_str(), &statbuf)) {
                if (errno == ENOENT) {
                        isnew = true;
                } else {
@@ -52,21 +72,17 @@ ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
 
                if (S_ISDIR (statbuf.st_mode)) {
 
-                       string::size_type slash = str.find_last_of ('/');
-               
+                       string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
+
                        if (slash == string::npos) {
-                               
+
                                /* a subdirectory of cwd, so statefile should be ... */
 
-                               string tmp;
-                               tmp = str;
-                               tmp += '/';
-                               tmp += str;
-                               tmp += statefile_suffix;
+                               string tmp = Glib::build_filename (str, str+statefile_suffix);
 
                                /* is it there ? */
-                               
-                               if (stat (tmp.c_str(), &statbuf)) {
+
+                               if (g_stat (tmp.c_str(), &statbuf)) {
                                        error << string_compose (_("cannot check statefile %1 (%2)"), tmp, strerror (errno))
                                              << endmsg;
                                        return -1;
@@ -84,16 +100,16 @@ ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
 
                                path = str;
                                snapshot = str.substr (slash+1);
-                                       
+
                        }
 
                } else if (S_ISREG (statbuf.st_mode)) {
-                       
-                       string::size_type slash = str.find_last_of ('/');
+
+                       string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
                        string::size_type suffix;
 
                        /* remove the suffix */
-                       
+
                        if (slash != string::npos) {
                                snapshot = str.substr (slash+1);
                        } else {
@@ -101,19 +117,22 @@ ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
                        }
 
                        suffix = snapshot.find (statefile_suffix);
-                       
-                       if (suffix == string::npos) {
-                               error << string_compose (_("%1 is not an Ardour snapshot file"), str) << endmsg;
+
+                       const string::size_type start_pos_of_extension = snapshot.size () - strlen (statefile_suffix);
+                       // we should check the start of extension position
+                       // because files '*.ardour.bak' are possible
+                       if (suffix != start_pos_of_extension) {
+                               error << string_compose (_("%1 is not a snapshot file"), str) << endmsg;
                                return -1;
                        }
 
                        /* remove suffix */
 
                        snapshot = snapshot.substr (0, suffix);
-                       
+
                        if (slash == string::npos) {
-                               
-                               /* we must be in the directory where the 
+
+                               /* we must be in the directory where the
                                   statefile lives. get it using cwd().
                                */
 
@@ -133,7 +152,7 @@ ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
 
                                path = str.substr (0, slash);
                        }
-                               
+
                } else {
 
                        /* what type of file is it? */
@@ -147,17 +166,17 @@ ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
                   as "dirname" does.
                */
 
-               string::size_type slash = str.find_last_of ('/');
+               string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
 
                if (slash == string::npos) {
-                       
+
                        /* no slash, just use the name, but clean it up */
-                       
+
                        path = legalize_for_path (str);
                        snapshot = path;
-                       
+
                } else {
-                       
+
                        path = str;
                        snapshot = str.substr (slash+1);
                }
@@ -165,3 +184,5 @@ ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
 
        return 0;
 }
+
+}  // namespace ARDOUR