if a request to reset the speed to zero as the default arrives when stopped, reset...
[ardour.git] / libs / ardour / find_session.cc
1 #include <unistd.h>
2 #include <sys/stat.h>
3
4 #include <cstring>
5 #include <climits>
6 #include <cerrno>
7
8 #include <glibmm/miscutils.h>
9
10 #include "pbd/compose.h"
11 #include "pbd/error.h"
12
13 #include "ardour/filename_extensions.h"
14 #include "ardour/utils.h"
15
16 #include "i18n.h"
17
18 using namespace std;
19 using namespace PBD;
20
21 namespace ARDOUR {
22
23 int
24 find_session (string str, string& path, string& snapshot, bool& isnew)
25 {
26         struct stat statbuf;
27         char buf[PATH_MAX+1];
28
29         isnew = false;
30
31         if (!realpath (str.c_str(), buf) && (errno != ENOENT && errno != ENOTDIR)) {
32                 error << string_compose (_("Could not resolve path: %1 (%2)"), buf, strerror(errno)) << endmsg;
33                 return -1;
34         }
35
36         str = buf;
37
38         /* check to see if it exists, and what it is */
39
40         if (stat (str.c_str(), &statbuf)) {
41                 if (errno == ENOENT) {
42                         isnew = true;
43                 } else {
44                         error << string_compose (_("cannot check session path %1 (%2)"), str, strerror (errno))
45                               << endmsg;
46                         return -1;
47                 }
48         }
49
50         if (!isnew) {
51
52                 /* it exists, so it must either be the name
53                    of the directory, or the name of the statefile
54                    within it.
55                 */
56
57                 if (S_ISDIR (statbuf.st_mode)) {
58
59                         string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
60
61                         if (slash == string::npos) {
62
63                                 /* a subdirectory of cwd, so statefile should be ... */
64
65                                 string tmp = Glib::build_filename (str, str+statefile_suffix);
66
67                                 /* is it there ? */
68
69                                 if (stat (tmp.c_str(), &statbuf)) {
70                                         error << string_compose (_("cannot check statefile %1 (%2)"), tmp, strerror (errno))
71                                               << endmsg;
72                                         return -1;
73                                 }
74
75                                 path = str;
76                                 snapshot = str;
77
78                         } else {
79
80                                 /* some directory someplace in the filesystem.
81                                    the snapshot name is the directory name
82                                    itself.
83                                 */
84
85                                 path = str;
86                                 snapshot = str.substr (slash+1);
87
88                         }
89
90                 } else if (S_ISREG (statbuf.st_mode)) {
91
92                         string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
93                         string::size_type suffix;
94
95                         /* remove the suffix */
96
97                         if (slash != string::npos) {
98                                 snapshot = str.substr (slash+1);
99                         } else {
100                                 snapshot = str;
101                         }
102
103                         suffix = snapshot.find (statefile_suffix);
104
105                         if (suffix == string::npos) {
106                                 error << string_compose (_("%1 is not a snapshot file"), str) << endmsg;
107                                 return -1;
108                         }
109
110                         /* remove suffix */
111
112                         snapshot = snapshot.substr (0, suffix);
113
114                         if (slash == string::npos) {
115
116                                 /* we must be in the directory where the
117                                    statefile lives. get it using cwd().
118                                 */
119
120                                 char cwd[PATH_MAX+1];
121
122                                 if (getcwd (cwd, sizeof (cwd)) == 0) {
123                                         error << string_compose (_("cannot determine current working directory (%1)"), strerror (errno))
124                                               << endmsg;
125                                         return -1;
126                                 }
127
128                                 path = cwd;
129
130                         } else {
131
132                                 /* full path to the statefile */
133
134                                 path = str.substr (0, slash);
135                         }
136
137                 } else {
138
139                         /* what type of file is it? */
140                         error << string_compose (_("unknown file type for session %1"), str) << endmsg;
141                         return -1;
142                 }
143
144         } else {
145
146                 /* its the name of a new directory. get the name
147                    as "dirname" does.
148                 */
149
150                 string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
151
152                 if (slash == string::npos) {
153
154                         /* no slash, just use the name, but clean it up */
155
156                         path = legalize_for_path (str);
157                         snapshot = path;
158
159                 } else {
160
161                         path = str;
162                         snapshot = str.substr (slash+1);
163                 }
164         }
165
166         return 0;
167 }
168
169 }  // namespace ARDOUR