Ejemplo n.º 1
0
        protected override Result DoGetEntryType(out DirectoryEntryType entryType, U8Span path)
        {
            UnsafeHelpers.SkipParamInit(out entryType);

            Unsafe.SkipInit(out FsPath normalizedPath);

            Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);

            if (rc.IsFailure())
            {
                return(rc);
            }

            if (FileTable.TryOpenFile(normalizedPath, out SaveFileInfo _))
            {
                entryType = DirectoryEntryType.File;
                return(Result.Success);
            }

            if (FileTable.TryOpenDirectory(normalizedPath, out SaveFindPosition _))
            {
                entryType = DirectoryEntryType.Directory;
                return(Result.Success);
            }

            return(ResultFs.PathNotFound.Log());
        }
Ejemplo n.º 2
0
        protected override Result DoDeleteFile(U8Span path)
        {
            Unsafe.SkipInit(out FsPath normalizedPath);

            Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);

            if (rc.IsFailure())
            {
                return(rc);
            }

            if (!FileTable.TryOpenFile(normalizedPath, out SaveFileInfo fileInfo))
            {
                return(ResultFs.PathNotFound.Log());
            }

            if (fileInfo.StartBlock != int.MinValue)
            {
                AllocationTable.Free(fileInfo.StartBlock);
            }

            FileTable.DeleteFile(normalizedPath);

            return(Result.Success);
        }
Ejemplo n.º 3
0
        private Result ResolveFullPath(out string fullPath, U8Span path, bool checkCaseSensitivity)
        {
            UnsafeHelpers.SkipParamInit(out fullPath);

            Unsafe.SkipInit(out FsPath normalizedPath);

            Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);

            if (rc.IsFailure())
            {
                return(rc);
            }

            fullPath = PathTools.Combine(_rootPath, normalizedPath.ToString());

            if (_mode == PathMode.CaseSensitive && checkCaseSensitivity)
            {
                rc = CheckPathCaseSensitively(fullPath);
                if (rc.IsFailure())
                {
                    return(rc);
                }
            }

            return(Result.Success);
        }
Ejemplo n.º 4
0
        protected override Result DoDeleteDirectoryRecursively(U8Span path)
        {
            Unsafe.SkipInit(out FsPath normalizedPath);

            Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);

            if (rc.IsFailure())
            {
                return(rc);
            }

            rc = CleanDirectoryRecursively(normalizedPath);
            if (rc.IsFailure())
            {
                return(rc);
            }

            rc = DeleteDirectory(normalizedPath);
            if (rc.IsFailure())
            {
                return(rc);
            }

            return(Result.Success);
        }
Ejemplo n.º 5
0
        void ParseFile(string projectDirectory, string filePath, ConcurrentDictionary <string, TemplateItem> templateItems)
        {
            var referencePath = PathNormalizer.MakeRelativePath(projectDirectory, filePath);

            DebugHelpers.WriteLine("FileNuggetFinder.ParseFile -- {0}", filePath);
            // Lookup any/all nuggets in the file and for each add a new template item.
            using var fs           = I18NUtility.Retry(() => File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read), 3);
            using var streamReader = new StreamReader(fs);

            _nuggetParser.ParseString(streamReader.ReadToEnd(), delegate(string nuggetString, int pos, Nugget nugget, string iEntity)
            {
                var referenceContext = _localizationOptions.DisableReferences
                    ? ReferenceContext.Create("Disabled references", iEntity, 0)
                    : ReferenceContext.Create(referencePath, iEntity, pos);
                var fileName = Path.GetFileNameWithoutExtension(filePath);
                // If we have a file like "myfile.aspx.vb" then the fileName will be "myfile.aspx" resulting in split
                // .pot files. So remove all extensions, so that we just have the actual name to deal with.
                fileName = fileName.IndexOf('.') > -1 ? fileName.Split('.')[0] : fileName;

                AddNewTemplateItem(
                    fileName,
                    referenceContext,
                    nugget,
                    templateItems);
                // Done.
                return(null); // null means we are not modifying the entity.
            });
        }
Ejemplo n.º 6
0
        public ItemInterface CreateNewFile(string localPath, string mimeType = "application/unknown")
        {
            var dinfo      = Directory.GetParent(localPath);
            var parentPath =
                dinfo
                .FullName
                // Remove the C:\\
                .Substring(dinfo.Root.Name.Length);

            var parent = this.FindItemFromPath(parentPath);

            if (parent == null && PathNormalizer.Normalize(localPath).Split('/').Length > 2)
            {// Not root
                parent = CreateNewFile(parentPath, "application/vnd.google-apps.folder");
            }

            var       itemName = Path.GetFileName(localPath);
            DriveFile body     = new DriveFile();

            body.Name     = itemName;
            body.MimeType = mimeType;
            if (parent != null)
            {
                if (body.Parents == null)
                {
                    body.Parents = new List <string>();
                }
                body.Parents.Add(parent.ID);
            }

            var createRequest = Service().Files.Create(body);
            var res           = createRequest.Execute();

            return(new ItemInterface(res, this));
        }
Ejemplo n.º 7
0
        void scanDir(string dir)
        {
            while (paused) /* wait */; {
            }

            string[] subDirs = Directory.GetDirectories(dir);
            string[] files   = Directory.GetFiles(dir);

            foreach (var raw_file in files)
            {
                string file = PathNormalizer.Normalize(raw_file);

                var lastModDate     = modLog.GetMultisyncModDate(file);
                var realLastModDate = File.GetLastWriteTime(file);

                bool moddedByMultisync = lastModDate == realLastModDate;
                if (!moddedByMultisync &&
                    file != syncStack.SynchronizingItemPath)
                {
                    modLog.UpdateFileMultisyncModDate(file);
                    this.callback(file);
                }
            }

            foreach (var subdir in subDirs)
            {
                scanDir(subdir);
            }
        }
Ejemplo n.º 8
0
        public ItemInterface FindItemFromPath(string fullPath, bool rootFirst = true)
        {
            fullPath = PathNormalizer.Normalize(fullPath);
            string[] items = PathNormalizer.Normalize(fullPath).Split("/");
            //Page page = this.RunQueryAsPage($"name = '{items[0]}'");
            ItemInterface current =
                rootFirst ? this.GetFileById("root") : this.GetFileById(this.FindID(items[0]).ID);

            for (int i = 1; i < items.Length; i++)
            {
                string item   = items[i];
                var    result = this.GetDirectoryContent(current.ItemIdentifier);
                foreach (var resultFile in result.Items)
                {
                    if (resultFile.Name == item)
                    {
                        current = resultFile;
                        break;
                    }
                }
            }

            string check = current.GetFullName();
            bool   equal = check == fullPath;

            if (!equal)
            {
                return(null);
            }

            return(current);
        }
Ejemplo n.º 9
0
        public Result Initialize(U8Span rootPath)
        {
            if (StringUtils.GetLength(rootPath, PathTools.MaxPathLength + 1) > PathTools.MaxPathLength)
            {
                return(ResultFs.TooLongPath.Log());
            }

            Span <byte> normalizedPath = stackalloc byte[PathTools.MaxPathLength + 2];

            Result rc = PathNormalizer.Normalize(normalizedPath, out long normalizedPathLen, rootPath, PreserveUnc, false);

            if (rc.IsFailure())
            {
                return(rc);
            }

            // Ensure a trailing separator
            if (!PathNormalizer.IsSeparator(normalizedPath[(int)normalizedPathLen - 1]))
            {
                Debug.Assert(normalizedPathLen + 2 <= normalizedPath.Length);

                normalizedPath[(int)normalizedPathLen]     = StringTraits.DirectorySeparator;
                normalizedPath[(int)normalizedPathLen + 1] = StringTraits.NullTerminator;
                normalizedPathLen++;
            }

            byte[] buffer = new byte[normalizedPathLen + 1];
            normalizedPath.Slice(0, (int)normalizedPathLen).CopyTo(buffer);
            RootPath = new U8String(buffer);

            return(Result.Success);
        }
Ejemplo n.º 10
0
        public static void Normalize_PreserveUncOptionOff_DoesNotPreserveUncPath()
        {
            var normalizer = new PathNormalizer("//aa/bb/..".ToU8Span(), PathNormalizer.Option.None);

            Assert.Equal(Result.Success, normalizer.Result);
            Assert.Equal(@"/aa", normalizer.Path.ToString());
        }
Ejemplo n.º 11
0
        public static void Normalize_PreserveUncOptionOn_PreservesUncPath()
        {
            var normalizer = new PathNormalizer("//aa/bb/..".ToU8Span(), PathNormalizer.Option.PreserveUnc);

            Assert.Equal(Result.Success, normalizer.Result);
            Assert.Equal(@"\\aa/bb", normalizer.Path.ToString());
        }
Ejemplo n.º 12
0
        public static void Normalize_PreserveTailSeparatorOption_IgnoresMissingTailSeparator()
        {
            var normalizer = new PathNormalizer("/a/./b".ToU8Span(), PathNormalizer.Option.PreserveTailSeparator);

            Assert.Equal(Result.Success, normalizer.Result);
            Assert.Equal("/a/b", normalizer.Path.ToString());
        }
Ejemplo n.º 13
0
        public void NormalizesToNFC()
        {
            var result = PathNormalizer.NormalizeToNFC("/\u0041\u030A");

            Assert.True(result.IsNormalized(NormalizationForm.FormC));
            Assert.Equal("/\u00C5", result);
        }
Ejemplo n.º 14
0
        public static void Ctor_EmptyPathWithAcceptEmptyOption_ReturnsEmptyPathWithSuccess()
        {
            var normalizer = new PathNormalizer("".ToU8Span(), PathNormalizer.Option.AcceptEmpty);

            Assert.Equal(Result.Success, normalizer.Result);
            Assert.True(normalizer.Path.IsEmpty());
        }
Ejemplo n.º 15
0
        private Result CheckSubPath(U8Span path1, U8Span path2)
        {
            Unsafe.SkipInit(out FsPath normalizedPath1);
            Unsafe.SkipInit(out FsPath normalizedPath2);

            Result rc = PathNormalizer.Normalize(normalizedPath1.Str, out _, path1, false, false);

            if (rc.IsFailure())
            {
                return(rc);
            }

            rc = PathNormalizer.Normalize(normalizedPath2.Str, out _, path2, false, false);
            if (rc.IsFailure())
            {
                return(rc);
            }

            if (PathUtility.IsSubPath(normalizedPath1, normalizedPath2))
            {
                return(ResultFs.DirectoryNotRenamable.Log());
            }

            return(Result.Success);
        }
Ejemplo n.º 16
0
        public static void Normalize_MountNameOptionOn_ParsesMountName()
        {
            var normalizer = new PathNormalizer("mount:/a/./b".ToU8Span(), PathNormalizer.Option.HasMountName);

            Assert.Equal(Result.Success, normalizer.Result);
            Assert.Equal("mount:/a/b", normalizer.Path.ToString());
        }
 public void TestGetDriveLetter()
 {
     Assert.AreEqual(@"E:\", PathNormalizer.GetDriveLetter(@"E:\My\Sub\Folder"));
     Assert.AreEqual(@"E:\", PathNormalizer.GetDriveLetter(@"E:\My\Sub\Folder\myfile.txt"));
     Assert.AreEqual(@"E:", PathNormalizer.GetDriveLetter(@"E:"));
     Assert.AreEqual(@"E:\", PathNormalizer.GetDriveLetter(@"E:\My\Sub\Fo:lder"));
     Assert.AreEqual(@"E:\", PathNormalizer.GetDriveLetter(@"E:\My\Sub\Fo/lder"));
 }
Ejemplo n.º 18
0
        public void RemovesDotSegments(string input, string expected)
        {
            var data   = Encoding.ASCII.GetBytes(input);
            var length = PathNormalizer.RemoveDotSegments(new Span <byte>(data));

            Assert.True(length >= 1);
            Assert.Equal(expected, Encoding.ASCII.GetString(data, 0, length));
        }
Ejemplo n.º 19
0
    public unsafe int DoubleDotSegments()
    {
        _doubleDotSegmentsAscii.CopyTo(_doubleDotSegmentsBytes, 0);

        fixed(byte *start = _doubleDotSegmentsBytes)
        {
            return(PathNormalizer.RemoveDotSegments(start, start + _doubleDotSegments.Length));
        }
    }
Ejemplo n.º 20
0
        public bool ModifiedFile(string path)
        {
            path = PathNormalizer.Normalize(path);

            var modDate     = ModLog.GetMultisyncModDate(path);
            var fileModDate = File.GetLastWriteTime(path);

            return(modDate == fileModDate);
        }
Ejemplo n.º 21
0
        public static void Normalize_PathAlreadyNormalized_ReturnsSameBuffer()
        {
            var originalPath = "/a/b".ToU8Span();
            var normalizer   = new PathNormalizer(originalPath, PathNormalizer.Option.PreserveTailSeparator);

            Assert.Equal(Result.Success, normalizer.Result);

            // Compares addresses and lengths of the buffers
            Assert.True(originalPath.Value == normalizer.Path.Value);
        }
Ejemplo n.º 22
0
        public void Upload(string localPath, string absolutePath)
        {
            string drivePath =
                PathNormalizer.Normalize(absolutePath).Replace(
                    PathNormalizer.Normalize(localPath), String.Empty);

            var item = drive.CreateNewFile(drivePath);

            //ModLog.SetFileLastWriteTime(absolutePath, new DateTime(DateTime.Now.Ticks + 10000));
            this.SyncWithState(item, SynchronizingItem.SyncState.Uploading);
        }
Ejemplo n.º 23
0
        private Result ResolveFullPath(Span <byte> outPath, U8Span relativePath)
        {
            if (RootPath.Length + StringUtils.GetLength(relativePath, PathTools.MaxPathLength + 1) > outPath.Length)
            {
                return(ResultFs.TooLongPath.Log());
            }

            // Copy root path to the output
            RootPath.Value.CopyTo(outPath);

            // Copy the normalized relative path to the output
            return(PathNormalizer.Normalize(outPath.Slice(RootPath.Length - 2), out _, relativePath, PreserveUnc, false));
        }
Ejemplo n.º 24
0
        private void GotoFolder(Path workdir, String filename)
        {
            Path dir = PathNormalizer.compose(workdir, filename);

            if (workdir.getParent().Equals(dir))
            {
                BrowserController.SetWorkdir(dir, workdir);
            }
            else
            {
                BrowserController.SetWorkdir(dir);
            }
        }
        public IFSEntryPointer GetFileInfo(string filename)
        {
            var fullpath = PathNormalizer.ConcatPath(SourcePath, filename);

            if (FileSystem.File.Exists(fullpath))
            {
                return(new PassthroughFile(FileSystem.FileInfo.FromFileName(fullpath)));
            }
            else if (FileSystem.Directory.Exists(fullpath))
            {
                return(new PassthroughDirectory(FileSystem.DirectoryInfo.FromDirectoryName(fullpath)));
            }
            return(null);
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Check if the file should be downloaded or uploaded (FILE ONLY)
        /// </summary>
        /// <param name="item">The item to check</param>
        /// <returns>DownloadOrUpload</returns>
        private DownloadOrUpload CheckDownloadOrUpload(string basePath, ItemInterface item)
        {
            string finalPath = PathNormalizer.Normalize(
                Path.Combine(basePath, item.GetFullName())
                );

            if (item == null)
            {
                return(DownloadOrUpload.Upload);
            }

            if (item.IsFolder())
            {
                throw new Exception("Item should be a file, not folder!");
            }

            // If reached here, is a file
            if (File.Exists(finalPath))
            {
                var itemHash  = item.MD5();
                var localHash = HashMD5(finalPath);

                if (itemHash == localHash)
                {
                    return(DownloadOrUpload.Nothing);
                }

                else
                {
                    var driveMod = item.LastMod;
                    var localMod = File.GetLastWriteTime(finalPath);

                    if (driveMod > localMod)
                    {
                        // Drive's item is more recent, let's download it
                        return(DownloadOrUpload.Download);
                    }
                    else if (driveMod < localMod)
                    {
                        // Local item is more recent, let's upload it
                        return(DownloadOrUpload.Update);
                    }
                }
            }
            bool exists = File.Exists(finalPath);

            // File does not exists, let's download
            return(DownloadOrUpload.Download);
        }
Ejemplo n.º 27
0
        protected override Result DoCleanDirectoryRecursively(U8Span path)
        {
            Unsafe.SkipInit(out FsPath normalizedPath);

            Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);

            if (rc.IsFailure())
            {
                return(rc);
            }

            FileSystemExtensions.CleanDirectoryRecursivelyGeneric(this, normalizedPath.ToString());

            return(Result.Success);
        }
Ejemplo n.º 28
0
        protected override Result DoCreateDirectory(U8Span path)
        {
            Unsafe.SkipInit(out FsPath normalizedPath);

            Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);

            if (rc.IsFailure())
            {
                return(rc);
            }

            FileTable.AddDirectory(normalizedPath);

            return(Result.Success);
        }
Ejemplo n.º 29
0
        private bool TryValidatePath(ReadOnlySpan <char> pathSegment)
        {
            // Must start with a leading slash
            if (pathSegment.Length == 0 || pathSegment[0] != '/')
            {
                ResetAndAbort(new ConnectionAbortedException(CoreStrings.FormatHttp2StreamErrorPathInvalid(RawTarget)), Http2ErrorCode.PROTOCOL_ERROR);
                return(false);
            }

            var pathEncoded = pathSegment.Contains('%');

            // Compare with Http1Connection.OnOriginFormTarget

            // URIs are always encoded/escaped to ASCII https://tools.ietf.org/html/rfc3986#page-11
            // Multibyte Internationalized Resource Identifiers (IRIs) are first converted to utf8;
            // then encoded/escaped to ASCII  https://www.ietf.org/rfc/rfc3987.txt "Mapping of IRIs to URIs"

            try
            {
                const int MaxPathBufferStackAllocSize = 256;

                // The decoder operates only on raw bytes
                Span <byte> pathBuffer = pathSegment.Length <= MaxPathBufferStackAllocSize
                                         // A constant size plus slice generates better code
                                         // https://github.com/dotnet/aspnetcore/pull/19273#discussion_r383159929
                    ? stackalloc byte[MaxPathBufferStackAllocSize].Slice(0, pathSegment.Length)
                                         // TODO - Consider pool here for less than 4096
                                         // https://github.com/dotnet/aspnetcore/pull/19273#discussion_r383604184
                    : new byte[pathSegment.Length];

                for (var i = 0; i < pathSegment.Length; i++)
                {
                    var ch = pathSegment[i];
                    // The header parser should already be checking this
                    Debug.Assert(32 < ch && ch < 127);
                    pathBuffer[i] = (byte)ch;
                }

                Path = PathNormalizer.DecodePath(pathBuffer, pathEncoded, RawTarget, QueryString.Length);

                return(true);
            }
            catch (InvalidOperationException)
            {
                ResetAndAbort(new ConnectionAbortedException(CoreStrings.FormatHttp2StreamErrorPathInvalid(RawTarget)), Http2ErrorCode.PROTOCOL_ERROR);
                return(false);
            }
        }
Ejemplo n.º 30
0
        public FileTemplateSource(IFileInfo fileInfo, string relativeFilePath)
        {
            if (fileInfo == null)
            {
                throw new ArgumentNullException(nameof(fileInfo));
            }

            if (string.IsNullOrEmpty(relativeFilePath))
            {
                throw new ArgumentNullException(nameof(relativeFilePath));
            }

            this.FileInfo    = fileInfo;
            this.FilePath    = PathNormalizer.GetNormalizedPath(fileInfo.PhysicalPath);
            this.TemplateKey = relativeFilePath;
        }