2009-01-14 17:29:30 +00:00
#region Disclaimer / License
2009-02-10 23:36:15 +00:00
// Copyright (C) 2009, Kenneth Skovhede
2009-01-14 17:29:30 +00:00
// http://www.hexad.dk, opensource@hexad.dk
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System ;
using System.Collections.Generic ;
using System.Text ;
namespace Duplicati.Library.Backend
{
2009-01-25 19:33:36 +00:00
public class File : IStreamingBackend
2009-01-14 17:29:30 +00:00
{
private string m_path ;
2009-01-15 19:42:14 +00:00
private string m_username ;
private string m_password ;
2009-01-14 17:29:30 +00:00
Dictionary < string , string > m_options ;
public File ()
{
}
public File ( string url , Dictionary < string , string > options )
{
m_options = options ;
2009-01-15 19:42:14 +00:00
m_path = url . Substring ( "file://" . Length );
if ( m_path . IndexOf ( "@" ) > 0 )
{
m_username = m_path . Substring ( 0 , m_path . IndexOf ( "@" ));
m_path = m_path . Substring ( m_path . IndexOf ( "@" ) + 1 );
if ( m_username . IndexOf ( ":" ) > 0 )
{
m_password = m_username . Substring ( 0 , m_username . IndexOf ( ":" ));
m_username = m_username . Substring ( m_username . IndexOf ( ":" ) + 1 );
}
else
{
2009-03-21 12:46:54 +00:00
if ( options . ContainsKey ( "ftp-password" ))
m_password = options [ "ftp-password" ];
2009-01-15 19:42:14 +00:00
}
}
2009-01-14 17:29:30 +00:00
if (! System . IO . Path . IsPathRooted ( url ))
m_path = System . IO . Path . GetFullPath ( m_path );
2009-01-15 19:42:14 +00:00
2009-01-14 17:29:30 +00:00
}
#region IBackendInterface Members
public string DisplayName
{
get { return "File based" ; }
}
public string ProtocolKey
{
get { return "file" ; }
}
2009-01-25 19:33:36 +00:00
public bool SupportsStreaming
{
get { return true ; }
}
2009-01-14 17:29:30 +00:00
public List < FileEntry > List ()
{
string path = m_path ;
List < FileEntry > ls = new List < FileEntry >();
2009-01-15 19:42:14 +00:00
//Attempt to apply credentials
if (! string . IsNullOrEmpty ( m_username ) && m_password != null )
Win32 . PreAuthenticate ( m_path , m_username , m_password );
2009-01-14 17:29:30 +00:00
foreach ( string s in System . IO . Directory . GetFiles ( path ))
{
System . IO . FileInfo fi = new System . IO . FileInfo ( s );
ls . Add ( new FileEntry ( fi . Name , fi . Length , fi . LastAccessTime , fi . LastWriteTime ));
}
foreach ( string s in System . IO . Directory . GetDirectories ( path ))
{
System . IO . DirectoryInfo di = new System . IO . DirectoryInfo ( s );
FileEntry fe = new FileEntry ( di . Name , 0 , di . LastAccessTime , di . LastWriteTime );
fe . IsFolder = true ;
ls . Add ( fe );
}
return ls ;
}
2009-01-25 19:33:36 +00:00
public void Put ( string remotename , System . IO . Stream stream )
{
string path = System . IO . Path . Combine ( m_path , remotename );
using ( System . IO . FileStream writestream = System . IO . File . Open ( path , System . IO . FileMode . Create , System . IO . FileAccess . Write , System . IO . FileShare . None ))
Core . Utility . CopyStream ( stream , writestream );
}
2009-01-14 17:29:30 +00:00
public void Put ( string remotename , string filename )
2009-01-25 19:33:36 +00:00
{
using ( System . IO . FileStream readstream = System . IO . File . Open ( filename , System . IO . FileMode . Open , System . IO . FileAccess . Read , System . IO . FileShare . Read ))
Put ( remotename , filename );
}
public void Get ( string remotename , System . IO . Stream stream )
2009-01-14 17:29:30 +00:00
{
string path = System . IO . Path . Combine ( m_path , remotename );
2009-01-25 19:33:36 +00:00
using ( System . IO . FileStream readstream = System . IO . File . Open ( path , System . IO . FileMode . Open , System . IO . FileAccess . Read , System . IO . FileShare . Read ))
Core . Utility . CopyStream ( readstream , stream );
2009-01-14 17:29:30 +00:00
}
public void Get ( string remotename , string filename )
{
2009-01-25 19:33:36 +00:00
using ( System . IO . FileStream writestream = System . IO . File . Open ( filename , System . IO . FileMode . Create , System . IO . FileAccess . Write , System . IO . FileShare . None ))
Get ( remotename , writestream );
2009-01-14 17:29:30 +00:00
}
public void Delete ( string remotename )
{
string path = System . IO . Path . Combine ( m_path , remotename );
System . IO . File . Delete ( path );
}
2009-03-21 12:46:54 +00:00
public IList < ICommandLineArgument > SupportedCommands
{
get
{
return new List < ICommandLineArgument >( new ICommandLineArgument [] {
new CommandLineArgument ( "ftp-password" , CommandLineArgument . ArgumentType . String , "Supplies the password used to connect to the server" , "The password used to connect to the server. This may also be supplied as the environment variable \"FTP_PASSWORD\"." )
});
}
}
public string Description
{
get
{
2009-03-22 22:55:04 +00:00
return "This backend can read and write data to an file based backend. Allowed formats are \"file://hostname/folder\" or \"file://username:password@hostname/folder\". You may supply UNC paths (eg: \"file://\\\\server\\folder\") or local paths (eg: (win) \"file://c:\\folder\", (linux) \"file:///usr/pub/files\")." ;
2009-03-21 12:46:54 +00:00
}
}
2009-01-15 19:42:14 +00:00
#endregion
2009-01-25 19:33:36 +00:00
#region IDisposable Members
public void Dispose ()
{
if ( m_options != null )
m_options = null ;
if ( m_username != null )
m_username = null ;
if ( m_password != null )
m_password = null ;
}
#endregion
2009-01-15 19:42:14 +00:00
public static bool PreAuthenticate ( string path , string username , string password )
2009-01-14 17:29:30 +00:00
{
2009-01-15 19:42:14 +00:00
return Win32 . PreAuthenticate ( path , username , password );
2009-01-14 17:29:30 +00:00
}
2009-01-25 19:33:36 +00:00
2009-01-14 17:29:30 +00:00
}
}