2 Copyright (C) 2012 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <glibmm/miscutils.h>
29 #include "pbd/compose.h"
30 #include "pbd/pathexpand.h"
31 #include "pbd/error.h"
33 #include "ardour/filename_extensions.h"
34 #include "ardour/utils.h"
44 find_session (string str, string& path, string& snapshot, bool& isnew)
50 str = canonical_path (str);
52 /* check to see if it exists, and what it is */
54 if (stat (str.c_str(), &statbuf)) {
55 if (errno == ENOENT) {
58 error << string_compose (_("cannot check session path %1 (%2)"), str, strerror (errno))
66 /* it exists, so it must either be the name
67 of the directory, or the name of the statefile
71 if (S_ISDIR (statbuf.st_mode)) {
73 string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
75 if (slash == string::npos) {
77 /* a subdirectory of cwd, so statefile should be ... */
79 string tmp = Glib::build_filename (str, str+statefile_suffix);
83 if (stat (tmp.c_str(), &statbuf)) {
84 error << string_compose (_("cannot check statefile %1 (%2)"), tmp, strerror (errno))
94 /* some directory someplace in the filesystem.
95 the snapshot name is the directory name
100 snapshot = str.substr (slash+1);
104 } else if (S_ISREG (statbuf.st_mode)) {
106 string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
107 string::size_type suffix;
109 /* remove the suffix */
111 if (slash != string::npos) {
112 snapshot = str.substr (slash+1);
117 suffix = snapshot.find (statefile_suffix);
119 if (suffix == string::npos) {
120 error << string_compose (_("%1 is not a snapshot file"), str) << endmsg;
126 snapshot = snapshot.substr (0, suffix);
128 if (slash == string::npos) {
130 /* we must be in the directory where the
131 statefile lives. get it using cwd().
134 char cwd[PATH_MAX+1];
136 if (getcwd (cwd, sizeof (cwd)) == 0) {
137 error << string_compose (_("cannot determine current working directory (%1)"), strerror (errno))
146 /* full path to the statefile */
148 path = str.substr (0, slash);
153 /* what type of file is it? */
154 error << string_compose (_("unknown file type for session %1"), str) << endmsg;
160 /* its the name of a new directory. get the name
164 string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
166 if (slash == string::npos) {
168 /* no slash, just use the name, but clean it up */
170 path = legalize_for_path (str);
176 snapshot = str.substr (slash+1);
183 } // namespace ARDOUR