File indexing completed on 2024-04-28 05:42:09
0001 /*************************************************************************** 0002 * Copyright (C) 2008-2009 by Rajko Albrecht ral@alwins-world.de * 0003 * https://kde.org/applications/development/org.kde.kdesvn * 0004 * * 0005 * This program is free software; you can redistribute it and/or * 0006 * modify it under the terms of the GNU Lesser General Public * 0007 * License as published by the Free Software Foundation; either * 0008 * version 2.1 of the License, or (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 0013 * Lesser General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU Lesser General Public * 0016 * License along with this program (in the file LGPL.txt); if not, * 0017 * write to the Free Software Foundation, Inc., 51 Franklin St, * 0018 * Fifth Floor, Boston, MA 02110-1301 USA * 0019 * * 0020 * This software consists of voluntary contributions made by many * 0021 * individuals. For exact contribution history, see the revision * 0022 * history and logs, available at https://commits.kde.org/kdesvn. * 0023 ***************************************************************************/ 0024 #include "diff_data.h" 0025 #include "exception.h" 0026 #include "helper.h" 0027 0028 #include <QFile> 0029 0030 #include <svn_io.h> 0031 #include <svn_path.h> 0032 0033 namespace svn 0034 { 0035 DiffData::DiffData(const Path &aTmpPath, const Path &_p1, const Revision &_r1, const Path &_p2, const Revision &_r2) 0036 : m_Pool() 0037 #if SVN_API_VERSION >= SVN_VERSION_CHECK(1, 8, 0) 0038 , m_outStream(new stream::SvnByteStream) 0039 , m_errStream(new stream::SvnByteStream) 0040 #else 0041 , m_tmpPath(aTmpPath) 0042 , m_outFile(nullptr) 0043 , m_errFile(nullptr) 0044 , m_outFileName(nullptr) 0045 , m_errFileName(nullptr) 0046 #endif 0047 , m_p1(_p1) 0048 , m_p2(_p2) 0049 , m_r1(_r1) 0050 , m_r2(_r2) 0051 , m_working_copy_present(false) 0052 , m_url_is_present(false) 0053 { 0054 #if SVN_API_VERSION >= SVN_VERSION_CHECK(1, 8, 0) 0055 Q_UNUSED(aTmpPath) 0056 #endif 0057 init(); 0058 } 0059 0060 void DiffData::init() 0061 { 0062 #if SVN_API_VERSION < SVN_VERSION_CHECK(1, 8, 0) 0063 svn_error_t *error; 0064 Pool scratchPool; 0065 error = svn_io_open_unique_file3(&m_outFile, &m_outFileName, m_tmpPath.path().toUtf8(), svn_io_file_del_on_pool_cleanup, m_Pool, scratchPool); 0066 0067 if (error != 0) { 0068 clean(); 0069 throw ClientException(error); 0070 } 0071 error = svn_io_open_unique_file3(&m_errFile, &m_errFileName, m_tmpPath.path().toUtf8(), svn_io_file_del_on_pool_cleanup, m_Pool, scratchPool); 0072 if (error != 0) { 0073 clean(); 0074 throw ClientException(error); 0075 } 0076 #endif 0077 if (svn_path_is_url(m_p1.cstr())) { 0078 m_url_is_present = true; 0079 } else { 0080 m_working_copy_present = true; 0081 } 0082 if (svn_path_is_url(m_p2.cstr())) { 0083 m_url_is_present = true; 0084 } else { 0085 m_working_copy_present = true; 0086 } 0087 0088 if (m_r1.revision()->kind == svn_opt_revision_unspecified && m_working_copy_present) { 0089 m_r1 = svn_opt_revision_base; 0090 } 0091 if (m_r2.revision()->kind == svn_opt_revision_unspecified) { 0092 m_r2 = m_working_copy_present ? svn_opt_revision_working : svn_opt_revision_head; 0093 } 0094 } 0095 0096 DiffData::~DiffData() 0097 { 0098 close(); 0099 } 0100 0101 void DiffData::close() 0102 { 0103 #if SVN_API_VERSION >= SVN_VERSION_CHECK(1, 8, 0) 0104 delete m_outStream; 0105 m_outStream = nullptr; 0106 delete m_errStream; 0107 m_errStream = nullptr; 0108 #else 0109 if (m_outFile) { 0110 svn_io_file_close(m_outFile, m_Pool); 0111 m_outFile = nullptr; 0112 } 0113 if (m_errFile) { 0114 svn_io_file_close(m_errFile, m_Pool); 0115 m_errFile = nullptr; 0116 } 0117 #endif 0118 } 0119 0120 QByteArray DiffData::content() 0121 { 0122 #if SVN_API_VERSION >= SVN_VERSION_CHECK(1, 8, 0) 0123 return m_outStream->content(); 0124 #else 0125 if (!m_outFileName) { 0126 return QByteArray(); 0127 } 0128 close(); 0129 QFile fi(QString::fromUtf8(m_outFileName)); 0130 if (!fi.open(QIODevice::ReadOnly)) { 0131 throw ClientException(QStringLiteral("%1 '%2'").arg(fi.errorString(), fi.fileName())); 0132 } 0133 0134 QByteArray res = fi.readAll(); 0135 fi.close(); 0136 return res; 0137 #endif 0138 } 0139 }