示例#1
0
        /// <inheritdoc />
        public async Task <IBackgroundTransfer> CreateAsync(
            IUnixDirectoryEntry targetDirectory,
            string fileName,
            Stream data,
            CancellationToken cancellationToken)
        {
            var targetInfo = ((UnixDirectoryEntry)targetDirectory).Info;
            var fileInfo   = new UnixFileInfo(UnixPath.Combine(targetInfo.FullName, fileName));
            var stream     = fileInfo.Open(FileMode.CreateNew, FileAccess.Write, FilePermissions.DEFFILEMODE);

            /* Must be ConfigureAwait(true) to stay in the same synchronization context. */
            await data.CopyToAsync(stream, 81920, cancellationToken)
            .ConfigureAwait(true);

            return(null);
        }
        void InitFileLock()
        {
            var lockDirectory = GetFileLockDirectory();

            if (!Directory.Exists(lockDirectory))
            {
                Directory.CreateDirectory(lockDirectory);
            }
            var lockFile = Path.Combine(lockDirectory, Identifier);

            FirstInstanceFileInfo = new UnixFileInfo(lockFile);
            try {
                FirstInstanceFileStream = FirstInstanceFileInfo.Open(
                    OpenFlags.O_CREAT | OpenFlags.O_EXCL,
                    FilePermissions.S_IRWXU
                    );
                IsFirstInstance = true;
            } catch (Exception) {
                IsFirstInstance = false;
            }

            if (IsFirstInstance)
            {
                // managed shutdown
                AppDomain.CurrentDomain.ProcessExit += (sender, e) => {
                    ReleaseFileLock();
                };

                // signal handlers
                UnixSignal[] shutdown_signals =
                {
                    new UnixSignal(Signum.SIGINT),
                    new UnixSignal(Signum.SIGTERM),
                };
                UnixSignalThread = new Thread(() => {
                    UnixSignal.WaitAny(shutdown_signals);
                    ReleaseFileLock();
                });
                UnixSignalThread.IsBackground = true;
                UnixSignalThread.Start();
            }
        }