Пример #1
0
        public Result CreateSaveDataFileSystemBySystemSaveDataId(ref SaveDataAttribute attribute, ref SaveDataCreationInfo creationInfo)
        {
            if (!IsSystemSaveDataId(attribute.SaveDataId))
            {
                return(ResultFs.InvalidArgument.Log());
            }

            SaveDataCreationInfo newCreationInfo = creationInfo;

            if (creationInfo.OwnerId == TitleId.Zero)
            {
                // todo: Assign the current program's ID
                // throw new NotImplementedException();
            }

            // Missing permission checks

            if (attribute.Type == SaveDataType.SystemBcat)
            {
                newCreationInfo.OwnerId = SystemTitleIds.Bcat;
            }

            SaveMetaCreateInfo metaCreateInfo   = default;
            OptionalHashSalt   optionalHashSalt = default;

            return(CreateSaveDataFileSystemImpl(ref attribute, ref newCreationInfo, ref metaCreateInfo,
                                                ref optionalHashSalt, false));
        }
Пример #2
0
        public Result CreateSaveDataFileSystem(ref SaveDataAttribute attribute, ref SaveDataCreationInfo creationInfo,
                                               ref SaveMetaCreateInfo metaCreateInfo)
        {
            OptionalHashSalt hashSalt = default;

            return(CreateUserSaveDataFileSystem(ref attribute, ref creationInfo, ref metaCreateInfo, ref hashSalt));
        }
Пример #3
0
        public Result CreateSaveDataFileSystemWithHashSalt(ref SaveDataAttribute attribute, ref SaveDataCreationInfo creationInfo,
                                                           ref SaveMetaCreateInfo metaCreateInfo, ref HashSalt hashSalt)
        {
            var hashSaltCopy = new OptionalHashSalt
            {
                IsSet    = true,
                HashSalt = hashSalt
            };

            return(CreateUserSaveDataFileSystem(ref attribute, ref creationInfo, ref metaCreateInfo, ref hashSaltCopy));
        }
Пример #4
0
        public Result CreateSaveDataFileSystem(ulong saveDataId, ref SaveDataAttribute attribute,
                                               ref SaveDataCreationInfo creationInfo, U8Span rootPath, OptionalHashSalt hashSalt, bool something)
        {
            // Use directory save data for now

            Result rc = OpenSaveDataDirectory(out IFileSystem fileSystem, creationInfo.SpaceId, string.Empty, false);

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

            return(fileSystem.EnsureDirectoryExists(GetSaveDataIdPath(saveDataId)));
        }
Пример #5
0
 private Result CreateUserSaveDataFileSystem(ref SaveDataAttribute attribute, ref SaveDataCreationInfo creationInfo,
                                             ref SaveMetaCreateInfo metaCreateInfo, ref OptionalHashSalt hashSalt)
 {
     return(CreateSaveDataFileSystemImpl(ref attribute, ref creationInfo, ref metaCreateInfo, ref hashSalt, false));
 }
Пример #6
0
        private Result CreateSaveDataFileSystemImpl(ref SaveDataAttribute attribute, ref SaveDataCreationInfo creationInfo,
                                                    ref SaveMetaCreateInfo metaCreateInfo, ref OptionalHashSalt hashSalt, bool something)
        {
            ulong  saveDataId     = 0;
            bool   isDeleteNeeded = false;
            Result rc;

            SaveDataIndexerReader reader = default;

            try
            {
                if (attribute.SaveDataId == FileSystemServer.SaveIndexerId)
                {
                    saveDataId = FileSystemServer.SaveIndexerId;
                    rc         = FsProxyCore.DoesSaveDataExist(out bool saveExists, creationInfo.SpaceId, saveDataId);

                    if (rc.IsSuccess() && saveExists)
                    {
                        return(ResultFs.PathAlreadyExists.Log());
                    }

                    isDeleteNeeded = true;
                }
                else
                {
                    rc = FsServer.SaveDataIndexerManager.GetSaveDataIndexer(out reader, creationInfo.SpaceId);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    SaveDataAttribute indexerKey = attribute;

                    if (attribute.SaveDataId != 0 && attribute.UserId == UserId.Zero)
                    {
                        saveDataId = attribute.SaveDataId;

                        rc = reader.Indexer.AddSystemSaveData(ref indexerKey);
                    }
                    else
                    {
                        if (attribute.Type != SaveDataType.System &&
                            attribute.Type != SaveDataType.SystemBcat)
                        {
                            if (reader.Indexer.IsFull())
                            {
                                return(ResultKvdb.TooLargeKeyOrDbFull.Log());
                            }
                        }

                        rc = reader.Indexer.Add(out saveDataId, ref indexerKey);
                    }

                    if (ResultFs.SaveDataPathAlreadyExists.Includes(rc))
                    {
                        return(ResultFs.PathAlreadyExists.LogConverted(rc));
                    }

                    isDeleteNeeded = true;

                    rc = reader.Indexer.SetState(saveDataId, SaveDataState.Creating);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    SaveDataSpaceId indexerSpaceId = GetSpaceIdForIndexer(creationInfo.SpaceId);

                    rc = reader.Indexer.SetSpaceId(saveDataId, indexerSpaceId);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    // todo: calculate size
                    long size = 0;

                    rc = reader.Indexer.SetSize(saveDataId, size);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    rc = reader.Indexer.Commit();
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }
                }

                rc = FsProxyCore.CreateSaveDataFileSystem(saveDataId, ref attribute, ref creationInfo, SaveDataRootPath,
                                                          hashSalt, false);

                if (rc.IsFailure())
                {
                    if (!ResultFs.PathAlreadyExists.Includes(rc))
                    {
                        return(rc);
                    }

                    rc = DeleteSaveDataFileSystemImpl2(creationInfo.SpaceId, saveDataId);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    rc = FsProxyCore.CreateSaveDataFileSystem(saveDataId, ref attribute, ref creationInfo, SaveDataRootPath,
                                                              hashSalt, false);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }
                }

                if (metaCreateInfo.Type != SaveDataMetaType.None)
                {
                    rc = FsProxyCore.CreateSaveDataMetaFile(saveDataId, creationInfo.SpaceId, metaCreateInfo.Type,
                                                            metaCreateInfo.Size);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    if (metaCreateInfo.Type == SaveDataMetaType.Thumbnail)
                    {
                        rc = FsProxyCore.OpenSaveDataMetaFile(out IFile metaFile, saveDataId, creationInfo.SpaceId,
                                                              metaCreateInfo.Type);

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

                            ReadOnlySpan <byte> metaFileData = stackalloc byte[0x20];

                            rc = metaFile.Write(0, metaFileData, WriteOption.Flush);
                            if (rc.IsFailure())
                            {
                                return(rc);
                            }
                        }
                    }
                }

                if (attribute.SaveDataId == FileSystemServer.SaveIndexerId || something)
                {
                    isDeleteNeeded = false;

                    return(Result.Success);
                }

                rc = reader.Indexer.SetState(saveDataId, SaveDataState.Normal);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                rc = reader.Indexer.Commit();
                if (rc.IsFailure())
                {
                    return(rc);
                }

                isDeleteNeeded = false;

                return(Result.Success);
            }
            finally
            {
                // Revert changes if an error happened in the middle of creation
                if (isDeleteNeeded)
                {
                    DeleteSaveDataFileSystemImpl2(creationInfo.SpaceId, saveDataId).IgnoreResult();

                    if (reader.IsInitialized && saveDataId != FileSystemServer.SaveIndexerId)
                    {
                        rc = reader.Indexer.GetBySaveDataId(out SaveDataIndexerValue value, saveDataId);

                        if (rc.IsSuccess() && value.SpaceId == creationInfo.SpaceId)
                        {
                            reader.Indexer.Delete(saveDataId).IgnoreResult();
                            reader.Indexer.Commit().IgnoreResult();
                        }
                    }
                }

                reader.Dispose();
            }
        }