#region
/* -----------------------------------------------------------------------
*
*
* Copyright 2011-2012 GRNET S.A. All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and
* documentation are those of the authors and should not be
* interpreted as representing official policies, either expressed
* or implied, of GRNET S.A.
*
* -----------------------------------------------------------------------
*/
#endregion
using System.Diagnostics.Contracts;
using System.IO;
using System.Reflection;
using NHibernate;
using NHibernate.Criterion;
using Pithos.Interfaces;
using Pithos.Network;
using log4net;
namespace Pithos.Core
{
using System;
using System.Collections.Generic;
///
/// TODO: Update summary.
///
/*[ActiveRecord]*/
public class FileState /*: ActiveRecordLinqBase*/
{
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private IList _tags = new List();
private DateTime? _modified;
/*[PrimaryKey(PrimaryKeyType.Guid)]*/
public virtual Guid Id { get; set; }
////[Property(Unique = true, UniqueKey = "IX_FileState_ObjectID")]
/*////[Property]*/
public virtual string ObjectID { get; set; }
public virtual long FileId { get; set; }
/* //[Property(Unique = true, UniqueKey = "IX_FileState_FilePath")]*/
public virtual string FilePath { get; set; }
/*////[Property]*/
public virtual FileOverlayStatus? OverlayStatus { get; set; }
/*////[Property]*/
public virtual FileStatus? FileStatus { get; set; }
/*////[Property]*/
public virtual string ConflictReason { get; set; }
///
/// The tophash value of the file, calculated by using a Merkle hash with the SHA256 algorithm
///
///
/// The SHA256 algorithm is substantially more expenive than other algorithms.
/// Recalculating the Checksum should be avoided whenever possible.
///
////[Property]
public virtual string Checksum { get; set; }
///
/// An easy to calcualte hash over the entire file, used to detect file changes
///
/// The algorithm used to calculate this hash should be cheap
//[Property(NotNull = true, Default = "")]
public virtual string ETag { get; set; }
////[Property(NotNull = true, Default = "")]
//public string LastMD5 { get; set; }
////[Property]
public virtual string Hashes { get; set; }
////[Property]
public virtual DateTime? LastWriteDate { get; set; }
////[Property]
public virtual long? LastLength { get; set; }
////[Property]
public virtual long? Version { get; set; }
////[Property]
public virtual DateTime? VersionTimeStamp { get; set; }
////[Property]
public virtual bool IsShared { get; set; }
////[Property]
public virtual string SharedBy { get; set; }
////[Property]
public virtual bool ShareWrite { get; set; }
////[Property]
public virtual bool IsFolder { get; set; }
//[HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true, Inverse = true)]
public virtual IList Tags
{
get { return _tags; }
set { _tags = value; }
}
////[Property]
public virtual DateTime? Modified
{
//Modified is only used for information/debugging puproses in the database. It's value is set to the current date if it's not modified by some other function
get { return _modified??DateTime.Now; }
set { _modified = value; }
}
public virtual FileSystemInfo GetFileSystemInfo()
{
if (String.IsNullOrWhiteSpace(FilePath))
throw new InvalidOperationException();
Contract.EndContractBlock();
return Directory.Exists(FilePath) ?
(FileSystemInfo)new DirectoryInfo(FilePath)
: new FileInfo(FilePath);
}
public virtual Uri GetRelativeUrl(AccountInfo accountInfo)
{
if (accountInfo==null)
throw new ArgumentNullException("accountInfo");
Contract.EndContractBlock();
var fsi=GetFileSystemInfo();
return fsi.AsRelativeUrlTo(accountInfo.AccountPath);
}
public static FileState CreateFor(FileSystemInfo info,IStatusNotification notification)
{
if(info==null)
throw new ArgumentNullException("info");
Contract.EndContractBlock();
//The file may not exist, if the call is made for a new server object
//var md5 = (info is DirectoryInfo || !info.Exists)
// ?Signature.MD5_EMPTY
// :((FileInfo)info).ComputeShortHash(notification);
var length = (info is DirectoryInfo || !info.Exists)
? 0
: ((FileInfo) info).Length;
var lastWriteTime = (info.Exists)
?(DateTime?)info.LastWriteTime
:null;
var fileState = new FileState
{
FilePath = info.FullName,
OverlayStatus = FileOverlayStatus.Unversioned,
FileStatus = Pithos.Core.FileStatus.Created,
ETag=String.Empty,
//LastMD5=md5,
LastWriteDate=lastWriteTime,
LastLength=length,
IsFolder=(info is DirectoryInfo),
Modified=DateTime.Now
};
return fileState;
}
public virtual string ToDebugString()
{
return String.Format("[STATE] FilePath:[{0}] ObjectID:[{1}], ETAG: [{2}], Checksum: [{3}]", FilePath, ObjectID,
ETag, Checksum);
//return String.Format("[STATE] FilePath:[{0}] ObjectID:[{1}], ETAG: [{2}], Checksum: [{3}] LastMD5:[{4}]", FilePath, ObjectID,
// ETag, Checksum, LastMD5);
}
}
//[ActiveRecord("Tags")]
public class FileTag //: ActiveRecordLinqBase
{
//[PrimaryKey]
public int Id { get; set; }
////[Property]
public string Name { get; set; }
////[Property]
public string Value { get; set; }
//[BelongsTo("FileStateId")]
public FileState FileState { get; set; }
}
public class PithosVersion
{
public virtual int Id { get; set; }
public virtual string Version { get; set; }
}
}