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