Exemple #1
0
        public static Result FromSpan(out FsPath fsPath, ReadOnlySpan <byte> path)
        {
            UnsafeHelpers.SkipParamInit(out fsPath);

            // Ensure null terminator even if the creation fails for safety
            fsPath.Str[MaxLength] = 0;

            var  sb         = new U8StringBuilder(fsPath.Str);
            bool overflowed = sb.Append(path).Overflowed;

            return(overflowed ? ResultFs.TooLongPath.Log() : Result.Success);
        }
Exemple #2
0
        public static Result CopyFile(IFileSystem destFs, IFileSystem sourceFs, ReadOnlySpan <byte> destParentPath,
                                      ReadOnlySpan <byte> sourcePath, ref DirectoryEntry dirEntry, Span <byte> copyBuffer)
        {
            IFile srcFile = null;
            IFile dstFile = null;

            try
            {
                Result rc = sourceFs.OpenFile(out srcFile, new U8Span(sourcePath), OpenMode.Read);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                FsPath dstPath    = default;
                int    dstPathLen = StringUtils.Concat(dstPath.Str, destParentPath);
                dstPathLen = StringUtils.Concat(dstPath.Str, dirEntry.Name, dstPathLen);

                if (dstPathLen > FsPath.MaxLength)
                {
                    throw new ArgumentException();
                }

                rc = destFs.CreateFile(dstPath, dirEntry.Size, CreateFileOptions.None);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                rc = destFs.OpenFile(out dstFile, dstPath, OpenMode.Write);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                long fileSize = dirEntry.Size;
                long offset   = 0;

                while (offset < fileSize)
                {
                    rc = srcFile.Read(out long bytesRead, offset, copyBuffer, ReadOption.None);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    rc = dstFile.Write(offset, copyBuffer.Slice(0, (int)bytesRead), WriteOption.None);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    offset += bytesRead;
                }

                return(Result.Success);
            }
            finally
            {
                srcFile?.Dispose();
                dstFile?.Dispose();
            }
        }