switch MIDI Clock slave code to use DEBUG_TRACE; don't make it require start/stop...
[ardour2.git] / libs / ardour / find_session.cc
bloba572a9a7a390d9600d99cae2cba72a43cbeeded4
1 #include <unistd.h>
2 #include <sys/stat.h>
4 #include <cstring>
5 #include <climits>
6 #include <cerrno>
8 #include "pbd/compose.h"
9 #include "pbd/error.h"
11 #include "ardour/session_utils.h"
12 #include "ardour/filename_extensions.h"
13 #include "ardour/utils.h"
15 #include "i18n.h"
17 using namespace std;
18 using namespace PBD;
20 int
21 ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
23 struct stat statbuf;
24 char buf[PATH_MAX+1];
26 isnew = false;
28 if (!realpath (str.c_str(), buf) && (errno != ENOENT && errno != ENOTDIR)) {
29 error << string_compose (_("Could not resolve path: %1 (%2)"), buf, strerror(errno)) << endmsg;
30 return -1;
33 str = buf;
35 /* check to see if it exists, and what it is */
37 if (stat (str.c_str(), &statbuf)) {
38 if (errno == ENOENT) {
39 isnew = true;
40 } else {
41 error << string_compose (_("cannot check session path %1 (%2)"), str, strerror (errno))
42 << endmsg;
43 return -1;
47 if (!isnew) {
49 /* it exists, so it must either be the name
50 of the directory, or the name of the statefile
51 within it.
54 if (S_ISDIR (statbuf.st_mode)) {
56 string::size_type slash = str.find_last_of ('/');
58 if (slash == string::npos) {
60 /* a subdirectory of cwd, so statefile should be ... */
62 string tmp;
63 tmp = str;
64 tmp += '/';
65 tmp += str;
66 tmp += statefile_suffix;
68 /* is it there ? */
70 if (stat (tmp.c_str(), &statbuf)) {
71 error << string_compose (_("cannot check statefile %1 (%2)"), tmp, strerror (errno))
72 << endmsg;
73 return -1;
76 path = str;
77 snapshot = str;
79 } else {
81 /* some directory someplace in the filesystem.
82 the snapshot name is the directory name
83 itself.
86 path = str;
87 snapshot = str.substr (slash+1);
91 } else if (S_ISREG (statbuf.st_mode)) {
93 string::size_type slash = str.find_last_of ('/');
94 string::size_type suffix;
96 /* remove the suffix */
98 if (slash != string::npos) {
99 snapshot = str.substr (slash+1);
100 } else {
101 snapshot = str;
104 suffix = snapshot.find (statefile_suffix);
106 if (suffix == string::npos) {
107 error << string_compose (_("%1 is not an Ardour snapshot file"), str) << endmsg;
108 return -1;
111 /* remove suffix */
113 snapshot = snapshot.substr (0, suffix);
115 if (slash == string::npos) {
117 /* we must be in the directory where the
118 statefile lives. get it using cwd().
121 char cwd[PATH_MAX+1];
123 if (getcwd (cwd, sizeof (cwd)) == 0) {
124 error << string_compose (_("cannot determine current working directory (%1)"), strerror (errno))
125 << endmsg;
126 return -1;
129 path = cwd;
131 } else {
133 /* full path to the statefile */
135 path = str.substr (0, slash);
138 } else {
140 /* what type of file is it? */
141 error << string_compose (_("unknown file type for session %1"), str) << endmsg;
142 return -1;
145 } else {
147 /* its the name of a new directory. get the name
148 as "dirname" does.
151 string::size_type slash = str.find_last_of ('/');
153 if (slash == string::npos) {
155 /* no slash, just use the name, but clean it up */
157 path = legalize_for_path (str);
158 snapshot = path;
160 } else {
162 path = str;
163 snapshot = str.substr (slash+1);
167 return 0;