Example #1
0
        /// <summary>
        /// Deserializes all .item files below the item's children folder and all deeper directories.
        /// Also traverses shortened paths.
        /// </summary>
        /// <param name="itemFile"></param>
        /// <param name="syncItem"></param>
        /// <param name="serializationFolder"></param>
        /// <param name="maxDepth"></param>
        /// <returns></returns>
#pragma warning disable 618
        internal static List <SyncItem> DeserializeAll(this FileInfo itemFile, SyncItem syncItem, DirectoryInfo serializationFolder, int maxDepth)
#pragma warning restore 618
        {
            if (maxDepth <= 0)
            {
#pragma warning disable 618
                return(new List <SyncItem>());

#pragma warning restore 618
            }

            Assert.ArgumentNotNull(itemFile, "itemFile");

#pragma warning disable 618
            var result = new List <SyncItem>();
#pragma warning restore 618

            // Find descendants in direct subfolder
            if (itemFile.Directory != null)
            {
                var childItemsFolder = new DirectoryInfo(itemFile.Directory.FullName + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(itemFile.Name));
                if (childItemsFolder.Exists)
                {
                    foreach (var childItemFile in childItemsFolder.GetFiles("*.item", SearchOption.AllDirectories))
                    {
                        var childSyncItem = childItemFile.Deserialize();
                        result.AddRange(childItemFile.DeserializeAll(childSyncItem, serializationFolder, maxDepth - 1));
                        result.Add(childSyncItem);
                    }
                }
            }

            // Find descendants in shortened paths
            var linkFiles = ShortenedPathsDictionary.GetLocationsFromLinkFiles(serializationFolder);
            if (!linkFiles.ContainsKey(syncItem.ItemPath))
            {
                return(result);
            }

            var truePath = new DirectoryInfo(linkFiles[syncItem.ItemPath]);
            if (!truePath.Exists)
            {
                return(result);
            }

            foreach (var childItemFile in truePath.GetFiles("*.item", SearchOption.AllDirectories))
            {
                var childSyncItem = childItemFile.Deserialize();
                result.AddRange(childItemFile.DeserializeAll(childSyncItem, serializationFolder, maxDepth - 1));
                result.Add(childSyncItem);
            }

            return(result);
        }
        public static string FindFilePath(this ID id, string serializationFolderName)
        {
            lock (PathSets)
            {
                // find or create the set of paths for the serialization folder
                SerializedIdToPathSet pathSet;
                if (PathSets.ContainsKey(serializationFolderName))
                {
                    pathSet = PathSets[serializationFolderName];
                }
                else
                {
                    pathSet = new SerializedIdToPathSet();
                    var serializationFolder = Deserializer.GetSerializationFolder(serializationFolderName);

                    // Add filepaths for shortened paths
                    foreach (
                        var shortenedItemsFolder in ShortenedPathsDictionary.GetLocationsFromLinkFiles(serializationFolder).Values)
                    {
                        pathSet.FilePaths.Push(shortenedItemsFolder);
                    }

                    // Add filepath for root of regular content
                    pathSet.FilePaths.Push(serializationFolder.FullName);

                    PathSets.Add(serializationFolderName, pathSet);
                }

                // get the individual item if already found
                if (pathSet.Paths.ContainsKey(id))
                {
                    return(pathSet.Paths[id]);
                }

                while (pathSet.FilePaths.Any())
                {
                    var filePath = pathSet.FilePaths.Pop();
                    foreach (var subdirectory in Directory.GetDirectories(filePath))
                    {
                        pathSet.FilePaths.Push(subdirectory);
                    }

                    string foundFile = null;
                    foreach (var file in Directory.GetFiles(filePath, "*.item"))
                    {
                        using (var sr = new StreamReader(file))
                        {
                            sr.ReadLine();
                            sr.ReadLine();
                            var itemIdStr = sr.ReadLine().Substring(4);
                            if (!ID.IsID(itemIdStr))
                            {
                                continue;
                            }

                            var itemId = ID.Parse(itemIdStr);
                            if (pathSet.Paths.ContainsKey(itemId))
                            {
                                throw new DuplicateIdException(itemId, file, pathSet.Paths[itemId]);
                            }

                            pathSet.Paths.Add(itemId, file);
                            if (itemId == id)
                            {
                                foundFile = file;
                            }
                        }
                    }

                    if (foundFile != null)
                    {
                        return(foundFile);
                    }
                }

                return(null);
            }
        }