Пример #1
0
        public static int Run(Options opts)
        {
            RunHelpers(opts);

            Logger.Info("Opening image \"{0}\"", opts.Path);
            var imageStream = OpenPath(opts.Path,
                                       FileMode.OpenOrCreate,
                                       FileAccess.ReadWrite,
                                       FileShare.None);

            if (imageStream == null)
            {
                Logger.Error("Failed to create image/access disk!");
                WaitForUserExit();
                return(INVALID_ARGUMENT);
            }

            if (opts.Offset > 0)
            {
                imageStream = new OffsetableStream(imageStream, opts.Offset);
            }

            if (FormatStream(opts.FileSystem, imageStream, opts.Size, "nDiscUtils Image") == null)
            {
                return(INVALID_ARGUMENT);
            }

            Cleanup(imageStream);
            WaitForUserExit();
            return(SUCCESS);
        }
Пример #2
0
        public static int Run(Options opts)
        {
            RunHelpers(opts);

            Logger.Info("Opening image \"{0}\"", opts.Path);
            var imageStream = OpenPath(opts.Path,
                                       FileMode.Open,
                                       FileAccess.Read | (opts.ReadOnly ? 0 : FileAccess.Write),
                                       FileShare.None);

            if (imageStream == null)
            {
                Logger.Error("Failed to open image/access disk!");
                WaitForUserExit();
                return(INVALID_ARGUMENT);
            }

            if (opts.Offset > 0)
            {
                imageStream = new OffsetableStream(imageStream, opts.Offset);
            }

            if (imageStream is FixedLengthStream fixedStream)
            {
                var geometry = FindGeometry(fixedStream);
                fixedStream.SetLength(geometry.TotalSectorsLong * geometry.BytesPerSector);
            }

            MountStream(imageStream, opts);

            Cleanup(imageStream);
            WaitForUserExit();
            return(SUCCESS);
        }
Пример #3
0
        public static int Run(Options opts)
        {
            RunHelpers(opts);

            switch (opts.FileSystem)
            {
            case "SquashFS":
            case "ISO":
                break;

            default:
                Logger.Error("Requested file system is not supported (Requested {0}, supported: ISO, SquashFS)", opts.FileSystem);
                WaitForUserExit();
                return(INVALID_ARGUMENT);
            }

            Logger.Info("Gathering files image \"{0}\"", opts.Path);
            var fileList = new LinkedList <FileInfo>();
            var fileSize = 0L;

            Action <DirectoryInfo> recursiveFileIndexer = null;

            recursiveFileIndexer = new Action <DirectoryInfo>((parentDir) =>
            {
                try
                {
                    foreach (var file in parentDir.GetFiles())
                    {
                        fileSize += file.Length + ((file.Length % 4096) == 0 ? 0 : 4096 - (file.Length % 4096));
                        fileList.AddLast(file);
                    }
                }
                catch (IOException) { }
                catch (UnauthorizedAccessException) { }

                try
                {
                    foreach (var subDir in parentDir.GetDirectories())
                    {
                        Logger.Verbose("Advancing recursion into \"{0}\"", subDir.FullName);
                        recursiveFileIndexer(subDir);
                    }
                }
                catch (IOException) { }
                catch (UnauthorizedAccessException) { }
            });

            recursiveFileIndexer(new DirectoryInfo(opts.Directory));

            Logger.Info("Creating image \"{0}\"", opts.Path);
            var imageStream = OpenPath(opts.Path,
                                       FileMode.Create,
                                       FileAccess.ReadWrite,
                                       FileShare.None);

            if (opts.Offset > 0)
            {
                imageStream = new OffsetableStream(imageStream, opts.Offset);
            }

            switch (opts.FileSystem)
            {
            case "SquashFS":
            {
                var builder = new SquashFileSystemBuilder();
                LoopFiles((path, stream) => builder.AddFile(path, stream), opts.Directory, fileList);

                Logger.Info("Finishing building SquashFS image...");
                builder.Build(imageStream);
                break;
            }

            case "ISO":
            {
                var builder = new CDBuilder();
                LoopFiles((path, stream) => builder.AddFile(path, stream), opts.Directory, fileList);

                Logger.Info("Finishing building ISO image...");
                builder.Build(imageStream);
                break;
            }

            default:
                return(INVALID_ARGUMENT);
            }

            Logger.Info("Done!");

            Cleanup(imageStream);
            WaitForUserExit();
            return(SUCCESS);
        }
Пример #4
0
        public static int Run(Options opts)
        {
            RunHelpers(opts);

            if (opts.Paths.Count() < 2)
            {
                Logger.Error("A RAID with only one disk sounds a bit useless, doesn't it?");
                return(INVALID_ARGUMENT);
            }

            if (!kRaidTypes.Keys.Any(t => t.Equals(opts.RaidType, StringComparison.OrdinalIgnoreCase)))
            {
                Logger.Error("Could not determined RAID type of \"{0}\"", opts.RaidType);
                Logger.Error("Supported RAID types are: {0}", string.Join(", ", kRaidTypes.Keys));
                return(INVALID_ARGUMENT);
            }

            var raidType = kRaidTypes[opts.RaidType.ToLower()];
            AbstractSoftRaidStream raidStream = null;

            var rawOffsets = opts.Offsets;
            var offsets    = new List <long>();

            foreach (var rawOffset in opts.Offsets)
            {
                offsets.Add(ParseSizeString(rawOffset));
            }

            if (raidType == "0")
            {
                raidStream = new SoftRaid0Stream();
            }
            else if (raidType == "1")
            {
                raidStream = new SoftRaid1Stream();
            }
            else if (raidType == "jbod")
            {
                raidStream = new SoftJbodStream();
                if (opts.JbodSizes.Count() != (opts.Paths.Count() - 1))
                {
                    Logger.Error("A JBOD-array requires a amount of [ Paths - 1 ] -j/--jbod options to be set");
                    return(INVALID_ARGUMENT);
                }
            }
            else
            {
                Logger.Error("RAID-type {0} is not implemented yet", raidType);
                return(NOT_IMPLEMENTED);
            }

            var jbodSizes = new List <long>();

            foreach (var rawJbodSize in opts.JbodSizes)
            {
                jbodSizes.Add(ParseSizeString(rawJbodSize));
            }

            raidStream.StripeSize = opts.StripeSize;

            var paths        = new List <string>(opts.Paths);
            var jbodSizeLeft = opts.Size;

            for (int i = 0; i < paths.Count; i++)
            {
                var pathStream = OpenPathUncached(paths[i], FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
                var offset     = 0L;

                if (offsets.Count > 0)
                {
                    if (i < offsets.Count)
                    {
                        offset = offsets[i];
                    }
                    else
                    {
                        offset = offsets[offsets.Count - 1];
                    }
                }

                if (raidType == "jbod")
                {
                    var jbodSize = 0L;

                    if (i != paths.Count - 1)
                    {
                        jbodSize = jbodSizes[i];
                    }
                    else
                    {
                        jbodSize = jbodSizeLeft;
                    }

                    jbodSizeLeft -= jbodSize;
                    pathStream    = new FixedLengthStream(pathStream, jbodSize, true);
                }

                if (offset > 0)
                {
                    pathStream = new OffsetableStream(pathStream, offset);
                }

                raidStream.AddStream(pathStream);
            }

            raidStream.Open();

            if (FormatStream(opts.FileSystem, raidStream, opts.Size, "nDiscUtils Image") == null)
            {
                return(INVALID_ARGUMENT);
            }

            Cleanup(raidStream);
            WaitForUserExit();
            return(SUCCESS);
        }
Пример #5
0
        public static int Run(Options opts)
        {
            RunHelpers(opts);

            if (opts.Paths.Count() < 2)
            {
                Logger.Error("A RAID with only one disk sounds a bit useless, doesn't it?");
                return(INVALID_ARGUMENT);
            }

            if (!MakeRaidImage.kRaidTypes.Keys.Any(t => t.Equals(opts.RaidType, StringComparison.OrdinalIgnoreCase)))
            {
                Logger.Error("Could not determined RAID type of \"{0}\"", opts.RaidType);
                Logger.Error("Supported RAID types are: {0}", string.Join(", ", MakeRaidImage.kRaidTypes.Keys));
                return(INVALID_ARGUMENT);
            }

            var raidType = MakeRaidImage.kRaidTypes[opts.RaidType.ToLower()];
            AbstractSoftRaidStream raidStream = null;

            var rawOffsets = opts.Offsets;
            var offsets    = new List <long>();

            foreach (var rawOffset in opts.Offsets)
            {
                offsets.Add(ParseSizeString(rawOffset));
            }

            if (raidType == "0")
            {
                raidStream = new SoftRaid0Stream();
            }
            else if (raidType == "1")
            {
                raidStream = new SoftRaid1Stream();
            }
            else if (raidType == "jbod")
            {
                raidStream = new SoftJbodStream();
                if (opts.JbodSizes.Count() != opts.Paths.Count())
                {
                    Logger.Error("A JBOD-array requires a amount of [ Paths ] -j/--jbod options to be set");
                    return(INVALID_ARGUMENT);
                }
            }
            else
            {
                Logger.Error("RAID-type {0} is not implemented yet", raidType);
                return(NOT_IMPLEMENTED);
            }

            raidStream.StripeSize = opts.StripeSize;

            var paths = new List <string>(opts.Paths);

            for (int i = 0; i < paths.Count; i++)
            {
                var pathStream = OpenPathUncached(paths[i], FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
                var offset     = 0L;

                if (offsets.Count > 0)
                {
                    if (i < offsets.Count)
                    {
                        offset = offsets[i];
                    }
                    else
                    {
                        offset = offsets[offsets.Count - 1];
                    }
                }

                if (raidType == "jbod")
                {
                    pathStream = new FixedLengthStream(pathStream, ParseSizeString(opts.JbodSizes.ElementAt(i)), true);
                }

                if (offset > 0)
                {
                    pathStream = new OffsetableStream(pathStream, offset);
                }

                if (pathStream is FixedLengthStream fixedStream)
                {
                    var geometry = FindGeometry(fixedStream);
                    fixedStream.SetLength(geometry.TotalSectorsLong * geometry.BytesPerSector);
                }

                raidStream.AddStream(pathStream);
            }

            raidStream.Open();

            MountStream(raidStream, opts);

            Cleanup(raidStream);
            WaitForUserExit();
            return(SUCCESS);
        }
Пример #6
0
        public static int Run(Options opts)
        {
            RunHelpers(opts);

            if (opts.Partition < 0)
            {
                Logger.Error("Invalid partition index (Expected number greater than 0)");
                WaitForUserExit();
                return(INVALID_ARGUMENT);
            }

            Logger.Info("Opening image \"{0}\"", opts.Path);
            var imageStream = OpenPath(opts.Path,
                                       FileMode.Open,
                                       FileAccess.Read | (opts.ReadOnly ? 0 : FileAccess.Write),
                                       FileShare.None);

            if (imageStream == null)
            {
                Logger.Error("Failed to open image/access disk!");
                WaitForUserExit();
                return(INVALID_ARGUMENT);
            }

            if (opts.Offset > 0)
            {
                imageStream = new OffsetableStream(imageStream, opts.Offset);
            }

            var partitionTable = FindPartitionTable(imageStream);

            if (opts.Partition >= partitionTable.Count)
            {
                Logger.Error("Invalid partition index (Expected number lower than than {0})", partitionTable.Count);
                WaitForUserExit();
                return(INVALID_ARGUMENT);
            }

            var partition = partitionTable[opts.Partition];
            {
                var type = new StringBuilder();

                if (partition.GuidType == null || partition.GuidType == Guid.Empty)
                {
                    type.AppendFormat("0x{0:X2}", partition.BiosType);
                }
                else
                {
                    type.AppendFormat("{0}", partition.GuidType);
                }

                type.AppendFormat(" ({0})", partition.TypeAsString);

                Logger.Info("Partition: {0}; 0x{1:X}-0x{2:X}", type, partition.FirstSector, partition.LastSector);
            }

            var geometry = FindGeometry(imageStream);
            var size     = partition.SectorCount * geometry.BytesPerSector;

            if (imageStream is FixedLengthStream fixedStream)
            {
                fixedStream.SetLength(geometry.TotalSectorsLong * geometry.BytesPerSector);
            }

            using (Stream dPartitionStream = new FixedLengthStream(partition.Open(), size))
            {
                var partitionStream = dPartitionStream;

                if (opts.Offset > 0)
                {
                    partitionStream = new OffsetableStream(partitionStream, opts.Offset);
                }

                MountStream(partitionStream, opts);
            }

            Cleanup(imageStream);
            WaitForUserExit();
            return(SUCCESS);
        }