protected override DateAndSequenceArchive GenerateArchiveFileInfo(FileInfo archiveFile, FileNameTemplate fileTemplate)
        {
            int sequenceNumber  = ExtractArchiveNumberFromFileName(archiveFile.FullName);
            var creationTimeUtc = FileCharacteristicsHelper.ValidateFileCreationTime(archiveFile, (f) => f.GetCreationTimeUtc(), (f) => f.GetLastWriteTimeUtc()).Value;

            return(new DateAndSequenceArchive(archiveFile.FullName, creationTimeUtc, string.Empty, sequenceNumber > 0 ? sequenceNumber : 0));
        }
Esempio n. 2
0
        private void UpdateCreationTime()
        {
            FileInfo fileInfo = new FileInfo(this.FileName);

            if (fileInfo.Exists)
            {
                this.CreationTimeUtc = FileCharacteristicsHelper.ValidateFileCreationTime(fileInfo, (f) => f.GetCreationTimeUtc(), (f) => f.GetLastWriteTimeUtc()).Value;
            }
            else
            {
                File.Create(this.FileName).Dispose();
                this.CreationTimeUtc = DateTime.UtcNow;

#if !SILVERLIGHT
                // Set the file's creation time to avoid being thwarted by Windows' Tunneling capabilities (https://support.microsoft.com/en-us/kb/172190).
                File.SetCreationTimeUtc(this.FileName, this.CreationTimeUtc);
#endif
            }
        }
Esempio n. 3
0
        public DateTime?GetFileCreationTimeSource(string filePath, bool fallback)
        {
            var      appender = GetAppender(filePath);
            DateTime?result   = null;

            if (appender != null)
            {
                try
                {
                    result = FileCharacteristicsHelper.ValidateFileCreationTime(appender, (f) => f.GetFileCreationTimeUtc(), (f) => f.CreationTimeUtc, (f) => f.GetFileLastWriteTimeUtc());
                    if (result.HasValue)
                    {
                        // Check if cached value is still valid, and update if not (Will automatically update CreationTimeSource)
                        DateTime cachedTimeUtc = appender.CreationTimeUtc;
                        if (result.Value != cachedTimeUtc)
                        {
                            appender.CreationTimeUtc = result.Value;
                        }
                        return(appender.CreationTimeSource);
                    }
                }
                catch (Exception ex)
                {
                    InternalLogger.Error(ex, "Failed to get file creation time for file '{0}'.", appender.FileName);
                    InvalidateAppender(appender.FileName);
                    throw;
                }
            }
            if (result == null && fallback)
            {
                var fileInfo = new FileInfo(filePath);
                if (fileInfo.Exists)
                {
                    result = FileCharacteristicsHelper.ValidateFileCreationTime(fileInfo, (f) => f.GetCreationTimeUtc(), (f) => f.GetLastWriteTimeUtc()).Value;
                    return(Time.TimeSource.Current.FromSystemTime(result.Value));
                }
            }

            return(result);
        }
Esempio n. 4
0
        protected override DateAndSequenceArchive GenerateArchiveFileInfo(FileInfo archiveFile, FileNameTemplate fileTemplate)
        {
            if (fileTemplate?.EndAt > 0)
            {
                string filename    = archiveFile.Name;
                int    templatePos = 0;
                for (int i = 0; i < filename.Length; ++i)
                {
                    char fileNameChar = filename[i];

                    if (templatePos >= fileTemplate.Template.Length)
                    {
                        if (char.IsLetter(fileNameChar))
                        {
                            return(null);    // reached end of template, but still letters
                        }
                        break;
                    }

                    char templateChar;
                    if (templatePos < fileTemplate.EndAt && i >= fileTemplate.BeginAt)
                    {
                        // Inside wildcard, skip validation of non-letters
                        if (char.IsLetter(fileNameChar))
                        {
                            templatePos = fileTemplate.EndAt;
                            do
                            {
                                if (templatePos >= fileTemplate.Template.Length)
                                {
                                    return(null);    // reached end of template, but still letters
                                }
                                templateChar = fileTemplate.Template[templatePos];
                                ++templatePos;
                            } while (!char.IsLetter(templateChar));
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        templateChar = fileTemplate.Template[templatePos];
                        ++templatePos;
                    }

                    if (fileNameChar == templateChar || char.ToLowerInvariant(fileNameChar) == char.ToLowerInvariant(templateChar))
                    {
                        continue;
                    }

                    if (templateChar == '*' && !char.IsLetter(fileNameChar))
                    {
                        break;  // Reached archive-seq-no, lets call it a day
                    }

                    return(null); // filename is not matching file-template
                }
            }

            int sequenceNumber = ExtractArchiveNumberFromFileName(archiveFile.FullName);

            InternalLogger.Trace("FileTarget: extracted sequenceNumber: {0} from file '{1}'", sequenceNumber, archiveFile.FullName);
            var creationTimeUtc = FileCharacteristicsHelper.ValidateFileCreationTime(archiveFile, (f) => f.GetCreationTimeUtc(), (f) => f.GetLastWriteTimeUtc()).Value;

            return(new DateAndSequenceArchive(archiveFile.FullName, creationTimeUtc, string.Empty, sequenceNumber > 0 ? sequenceNumber : 0));
        }
        public UnixMultiProcessFileAppender(string fileName, ICreateFileParameters parameters) : base(fileName, parameters)
        {
            UnixFileInfo fileInfo   = null;
            bool         fileExists = false;

            try
            {
                fileInfo   = new UnixFileInfo(fileName);
                fileExists = fileInfo.Exists;
            }
            catch
            {
            }

            int fd = Syscall.open(fileName, OpenFlags.O_CREAT | OpenFlags.O_WRONLY | OpenFlags.O_APPEND, (FilePermissions)(6 | (6 << 3) | (6 << 6)));

            if (fd == -1)
            {
                if (Stdlib.GetLastError() == Errno.ENOENT && parameters.CreateDirs)
                {
                    string dirName = Path.GetDirectoryName(fileName);
                    if (!Directory.Exists(dirName) && parameters.CreateDirs)
                    {
                        Directory.CreateDirectory(dirName);
                    }

                    fd = Syscall.open(fileName, OpenFlags.O_CREAT | OpenFlags.O_WRONLY | OpenFlags.O_APPEND, (FilePermissions)(6 | (6 << 3) | (6 << 6)));
                }
            }
            if (fd == -1)
            {
                UnixMarshal.ThrowExceptionForLastError();
            }

            try
            {
                _file = new UnixStream(fd, true);

                long filePosition = _file.Position;
                if (fileExists || filePosition > 0)
                {
                    if (fileInfo != null)
                    {
                        CreationTimeUtc = FileCharacteristicsHelper.ValidateFileCreationTime(fileInfo, (f) => File.GetCreationTimeUtc(f.FullName), (f) => { f.Refresh(); return(f.LastStatusChangeTimeUtc); }, (f) => DateTime.UtcNow).Value;
                    }
                    else
                    {
                        CreationTimeUtc = FileCharacteristicsHelper.ValidateFileCreationTime(fileName, (f) => File.GetCreationTimeUtc(f), (f) => DateTime.UtcNow).Value;
                    }
                }
                else
                {
                    // We actually created the file and eventually concurrent processes
                    CreationTimeUtc = DateTime.UtcNow;
                    File.SetCreationTimeUtc(FileName, CreationTimeUtc);
                }
            }
            catch
            {
                Syscall.close(fd);
                throw;
            }
        }