Exemplo n.º 1
0
        public virtual IDataAccessObjectAdvanced CreateDataAccessObject <K>(Type type, K primaryKey, PrimaryKeyType primaryKeyType)
        {
            if (!typeof(IDataAccessObjectAdvanced).IsAssignableFrom(type) ||
                !typeof(DataAccessObject <>).IsAssignableFromIgnoreGenericParameters(type))
            {
                throw new ArgumentException("Type must be a DataAccessObjectType", nameof(type));
            }

            var objectPropertyAndValues = this.GetObjectPropertyValues(type, primaryKey, primaryKeyType);

            if (objectPropertyAndValues.Any(keyValue => keyValue.Value == null))
            {
                throw new MissingOrInvalidPrimaryKeyException();
            }

            var existing = this.GetCurrentDataContext(false).GetObject(this.GetConcreteTypeFromDefinitionType(type), objectPropertyAndValues);

            if (existing != null)
            {
                IDataAccessObjectAdvanced obj = null;

                ActionUtils.IgnoreExceptions(() => obj = this.GetReference(type, primaryKey, primaryKeyType));

                throw new ObjectAlreadyExistsException(obj, null, "CreateDataAccessObject");
            }
            else
            {
                var retval = this.RuntimeDataAccessModelInfo.CreateDataAccessObject(type, this, true);

                retval.ToObjectInternal().SetPrimaryKeys(objectPropertyAndValues);
                retval.ToObjectInternal().FinishedInitializing();
                retval.ToObjectInternal().SubmitToCache();

                return(retval);
            }
        }
Exemplo n.º 2
0
        public override void DoRun()
        {
            var finished = false;

            ProcessTaskStateRequest();

            var source      = this.streamProvider.GetSourceStream();
            var destination = this.streamProvider.GetDestinationStream();

            try
            {
                try
                {
                    bytesReadMeter.RaiseValueChanged(0L);

                    ProcessTaskStateRequest();

                    bytesWrittenMeter.RaiseValueChanged(0L);

                    ProcessTaskStateRequest();

                    while (true)
                    {
                        var read = source.Read(buffer, 0, buffer.Length);

                        if (read == 0)
                        {
                            if (sourceLength != bytesRead)
                            {
                                sourceLength = bytesRead;
                                bytesReadMeter.RaiseMajorChange();
                                bytesWrittenMeter.RaiseMajorChange();
                            }

                            break;
                        }

                        lock (this)
                        {
                            bytesRead += read;
                        }

                        if (bytesRead > sourceLength)
                        {
                            sourceLength = bytesRead;
                        }

                        bytesReadMeter.RaiseValueChanged(bytesRead - read);

                        ProcessTaskStateRequest();

                        destination.Write(buffer, 0, read);

                        lock (this)
                        {
                            bytesWritten += read;
                        }

                        bytesWrittenMeter.RaiseValueChanged(bytesWritten - read);

                        ProcessTaskStateRequest();
                    }

                    finished = true;
                }
                catch (StopRequestedException)
                {
                    SetTaskState(TaskState.Stopped);
                }
            }
            finally
            {
                ActionUtils.IgnoreExceptions(destination.Flush);

                if (autoCloseSource)
                {
                    ActionUtils.IgnoreExceptions(source.Close);
                }

                if (autoCloseDestination)
                {
                    ActionUtils.IgnoreExceptions(destination.Close);
                }

                if (finished)
                {
                    SetTaskState(TaskState.Finished);
                }
                else
                {
                    SetTaskState(TaskState.Stopped);
                }
            }
        }
        public override void Process(Command command)
        {
            Stream input;
            var    options = (CommandOptions)this.LoadOptions((TextCommand)command);
            var    file    = this.Connection.FileSystemManager.ResolveFile(options.Uri);

            if (!file.Exists)
            {
                throw new FileNodeNotFoundException(file.Address);
            }

            using (input = file.GetContent().GetInputStream(FileShare.ReadWrite))
            {
                if (inputBuffer == null)
                {
                    inputBuffer = new byte[512];
                }

                byte[] buffer = inputBuffer;

                if (outputBuffer == null)
                {
                    int length;

                    length  = (int)(buffer.Length * (4d / 3d));
                    length += length % 4;

                    outputBuffer = new char[length];
                }

                var outbuffer = outputBuffer;

                var leftovers = 0;

                try
                {
                    for (; ;)
                    {
                        var x = input.Read(buffer, leftovers, buffer.Length - leftovers);

                        if (x == 0)
                        {
                            if (leftovers == 0)
                            {
                                break;
                            }

                            x         = leftovers;
                            leftovers = 0;
                        }
                        else if (x < 3)
                        {
                            leftovers = x;

                            continue;
                        }

                        if (x >= 3)
                        {
                            leftovers = x % 3;

                            x -= leftovers;
                        }

                        var y = Convert.ToBase64CharArray(buffer, 0, x, outbuffer, 0, Base64FormattingOptions.None);

                        Connection.WriteTextPartialBlock(outbuffer, 0, y);

                        if (leftovers != 0)
                        {
                            Array.Copy(buffer, x, buffer, 0, leftovers);
                        }
                    }
                }
                catch (Exception)
                {
                    ActionUtils.IgnoreExceptions(() => this.Connection.WriteTextBlock(""));

                    throw;
                }
            }

            Connection.WriteTextBlock("");
        }
        public override void Process(Command command)
        {
            var options = (CommandOptions)this.LoadOptions((TextCommand)command);

            var file = this.Connection.FileSystemManager.ResolveFile(options.Uri);

            var content = file.GetContent();

            Stream stream = null;

            for (int i = 0; i < 16; i++)
            {
                try
                {
                    stream =
                        content.OpenStream((FileMode)Enum.Parse(typeof(FileMode), options.Mode, true),
                                           (FileAccess)Enum.Parse(typeof(FileAccess), options.Access, true),
                                           (FileShare)Enum.Parse(typeof(FileShare), options.Share, true));

                    break;
                }
                catch (Exception)
                {
                    if (i == 9)
                    {
                        throw;
                    }

                    System.Threading.Thread.Sleep(150);
                }
            }

            EventHandler closeStream = (sender, eventArgs) => ActionUtils.IgnoreExceptions(delegate
            {
                if (stream != null)
                {
                    stream.Close();
                }
            });

            try
            {
                IList <object> list = new List <object>();

                Connection.RunLevel = new RandomAccessRunLevel(file, stream);

                this.Connection.Closed += closeStream;

                list.Add("canread");
                list.Add(stream.CanRead);
                list.Add("canwrite");
                list.Add(stream.CanWrite);
                list.Add("canseek");
                list.Add(stream.CanSeek);
                list.Add("sharing");
                list.Add(options.Share);

                if (stream.CanSeek)
                {
                    list.Add("length");
                    list.Add(stream.Length);
                }

                Connection.WriteOk
                (
                    list.ToArray()
                );

                if (options.WaitForReady)
                {
                    Connection.Flush();
                    Connection.ReadReady();
                }
            }
            catch
            {
                throw;
            }
        }
 public virtual void Dispose()
 {
     ActionUtils.IgnoreExceptions(() => this.Stream.Close());
 }
        protected internal static DriveInfo GetDriveInfo(string path)
        {
            string    symTarget;
            string    driveFormat = "";
            DriveInfo driveInfo;
            bool      unix = false;

            driveInfo = null;

            if (Environment.OSVersion.Platform == PlatformID.Win32NT ||
                Environment.OSVersion.Platform == PlatformID.Win32S ||
                Environment.OSVersion.Platform == PlatformID.Win32Windows ||
                Environment.OSVersion.Platform == PlatformID.WinCE)
            {
                if (path.Length != 3)
                {
                    return(null);
                }

                if (path[1] != ':')
                {
                    return(null);
                }

                if (!Char.IsLetter(path[0]))
                {
                    return(null);
                }

                path = path.Left(2) + "\\";
            }
            else if (Environment.OSVersion.Platform == PlatformID.Unix)
            {
                unix = true;
            }

            ActionUtils.IgnoreExceptions(() => driveInfo = new DriveInfo(path));

            if (unix && driveInfo != null)
            {
                try
                {
                    driveFormat = driveInfo.DriveFormat;
                }
                catch (IOException)
                {
                }
            }

            if (driveInfo == null || driveFormat == "Unknown")
            {
                if ((symTarget = Native.GetInstance().GetSymbolicLinkTarget(path)) != null)
                {
                    ActionUtils.IgnoreExceptions(() => driveInfo = new DriveInfo(symTarget));

                    if (unix && driveInfo != null)
                    {
                        try
                        {
                            driveFormat = driveInfo.DriveFormat;
                        }
                        catch (IOException)
                        {
                        }
                    }
                }
            }

            if (driveInfo == null || driveFormat == "Unknown")
            {
                return(null);
            }

            return(driveInfo);
        }