8 #include <glibmm/miscutils.h>
10 #include "pbd/compose.h"
11 #include "pbd/error.h"
13 #include "ardour/session_utils.h"
14 #include "ardour/filename_extensions.h"
15 #include "ardour/utils.h"
23 ARDOUR::find_session (string str
, string
& path
, string
& snapshot
, bool& isnew
)
30 if (!realpath (str
.c_str(), buf
) && (errno
!= ENOENT
&& errno
!= ENOTDIR
)) {
31 error
<< string_compose (_("Could not resolve path: %1 (%2)"), buf
, strerror(errno
)) << endmsg
;
37 /* check to see if it exists, and what it is */
39 if (stat (str
.c_str(), &statbuf
)) {
40 if (errno
== ENOENT
) {
43 error
<< string_compose (_("cannot check session path %1 (%2)"), str
, strerror (errno
))
51 /* it exists, so it must either be the name
52 of the directory, or the name of the statefile
56 if (S_ISDIR (statbuf
.st_mode
)) {
58 string::size_type slash
= str
.find_last_of (G_DIR_SEPARATOR
);
60 if (slash
== string::npos
) {
62 /* a subdirectory of cwd, so statefile should be ... */
64 string tmp
= Glib::build_filename (str
, str
+statefile_suffix
);
68 if (stat (tmp
.c_str(), &statbuf
)) {
69 error
<< string_compose (_("cannot check statefile %1 (%2)"), tmp
, strerror (errno
))
79 /* some directory someplace in the filesystem.
80 the snapshot name is the directory name
85 snapshot
= str
.substr (slash
+1);
89 } else if (S_ISREG (statbuf
.st_mode
)) {
91 string::size_type slash
= str
.find_last_of (G_DIR_SEPARATOR
);
92 string::size_type suffix
;
94 /* remove the suffix */
96 if (slash
!= string::npos
) {
97 snapshot
= str
.substr (slash
+1);
102 suffix
= snapshot
.find (statefile_suffix
);
104 if (suffix
== string::npos
) {
105 error
<< string_compose (_("%1 is not a snapshot file"), str
) << endmsg
;
111 snapshot
= snapshot
.substr (0, suffix
);
113 if (slash
== string::npos
) {
115 /* we must be in the directory where the
116 statefile lives. get it using cwd().
119 char cwd
[PATH_MAX
+1];
121 if (getcwd (cwd
, sizeof (cwd
)) == 0) {
122 error
<< string_compose (_("cannot determine current working directory (%1)"), strerror (errno
))
131 /* full path to the statefile */
133 path
= str
.substr (0, slash
);
138 /* what type of file is it? */
139 error
<< string_compose (_("unknown file type for session %1"), str
) << endmsg
;
145 /* its the name of a new directory. get the name
149 string::size_type slash
= str
.find_last_of (G_DIR_SEPARATOR
);
151 if (slash
== string::npos
) {
153 /* no slash, just use the name, but clean it up */
155 path
= legalize_for_path (str
);
161 snapshot
= str
.substr (slash
+1);