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.
26 #include "pbd/gstdio_compat.h"
28 #include <glibmm/miscutils.h>
29 #include <glibmm/fileutils.h>
31 #include "pbd/basename.h"
32 #include "pbd/compose.h"
33 #include "pbd/file_archive.h"
34 #include "pbd/pathexpand.h"
35 #include "pbd/error.h"
37 #include "ardour/filename_extensions.h"
38 #include "ardour/utils.h"
39 #include "ardour/session_utils.h"
49 find_session (string str, string& path, string& snapshot, bool& isnew)
55 str = canonical_path (str);
57 /* check to see if it exists, and what it is */
59 if (g_stat (str.c_str(), &statbuf)) {
60 if (errno == ENOENT) {
63 error << string_compose (_("cannot check session path %1 (%2)"), str, strerror (errno))
71 /* it exists, so it must either be the name
72 of the directory, or the name of the statefile
76 if (S_ISDIR (statbuf.st_mode)) {
78 string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
80 if (slash == string::npos) {
82 /* a subdirectory of cwd, so statefile should be ... */
84 string tmp = Glib::build_filename (str, str+statefile_suffix);
88 if (g_stat (tmp.c_str(), &statbuf)) {
89 error << string_compose (_("cannot check statefile %1 (%2)"), tmp, strerror (errno))
99 /* some directory someplace in the filesystem.
100 the snapshot name is the directory name
105 snapshot = str.substr (slash+1);
109 } else if (S_ISREG (statbuf.st_mode)) {
111 string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
112 string::size_type suffix;
114 /* remove the suffix */
116 if (slash != string::npos) {
117 snapshot = str.substr (slash+1);
122 suffix = snapshot.find (statefile_suffix);
124 const string::size_type start_pos_of_extension = snapshot.size () - strlen (statefile_suffix);
125 // we should check the start of extension position
126 // because files '*.ardour.bak' are possible
127 if (suffix != start_pos_of_extension) {
128 error << string_compose (_("%1 is not a snapshot file"), str) << endmsg;
134 snapshot = snapshot.substr (0, suffix);
136 if (slash == string::npos) {
138 /* we must be in the directory where the
139 statefile lives. get it using cwd().
142 char cwd[PATH_MAX+1];
144 if (getcwd (cwd, sizeof (cwd)) == 0) {
145 error << string_compose (_("cannot determine current working directory (%1)"), strerror (errno))
154 /* full path to the statefile */
156 path = str.substr (0, slash);
161 /* what type of file is it? */
162 error << string_compose (_("unknown file type for session %1"), str) << endmsg;
168 /* its the name of a new directory. get the name
172 string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
174 if (slash == string::npos) {
176 /* no slash, just use the name, but clean it up */
178 path = legalize_for_path (str);
184 snapshot = str.substr (slash+1);
191 /* check if zip is a session-archive,
192 * return > 0 if file is not an archive
193 * return < 0 if unzip failed
194 * return 0 on success. path and snapshot are set.
197 inflate_session (const std::string& zipfile, const std::string& target_dir, string& path, string& snapshot)
199 if (zipfile.find (session_archive_suffix) == string::npos) {
204 PBD::FileArchive ar (zipfile);
205 std::vector<std::string> files = ar.contents ();
207 if (files.size () == 0) {
208 error << _("Archive is empty") << endmsg;
212 /* session archives are expected to be named after the archive */
213 std::string bn = Glib::path_get_dirname (files.front());
215 error << _("Archive does not contain a session folder") << endmsg;
219 size_t sep = bn.find_first_of ('/');
221 if (sep != string::npos) {
222 bn = bn.substr (0, sep);
226 error << _("Archive does not contain a valid session structure") << endmsg;
230 string sn = Glib::build_filename (bn, bn + statefile_suffix);
232 if (std::find (files.begin(), files.end(), sn) == files.end()) {
233 error << _("Archive does not contain a session file") << endmsg;
237 /* check if target folder exists */
238 string dest = Glib::build_filename (target_dir, bn);
239 if (Glib::file_test (dest, Glib::FILE_TEST_EXISTS)) {
240 error << string_compose (_("Destination '%1' already exists."), dest) << endmsg;
244 if (0 == ar.inflate (target_dir)) {
245 info << string_compose (_("Extracted session-archive to '%1'."), dest) << endmsg;
252 error << _("Error reading file-archive") << endmsg;
256 error << _("Error extracting file-archive") << endmsg;
260 string inflate_error (int e) {
263 return _("No Error");
265 return string_compose (_("File extension is not %1"), session_archive_suffix);
267 return _("Archive is empty");
269 return _("Archive does not contain a session folder");
271 return _("Archive does not contain a valid session structure");
273 return _("Archive does not contain a session file");
275 return _("Error reading file-archive");
277 return _("Destination folder already exists.");
279 return _("Error extracting file-archive");
284 return _("Unknown Error");
287 } // namespace ARDOUR