Example #1
0
        private static void ImportFile(IDataReader reader,
                                       long volumeID,
                                       long minFileID,
                                       string rootPath,
                                       MetadataStore metaData,
                                       VolumeDatabase db,
                                       BufferedVolumeItemWriter writer,
                                       long[] counters)
        {
            FileSystemVolumeItem item;

            if ((string)reader["type"] == "directory")
            {
                item = new DirectoryVolumeItem(db);
                counters[TOTAL_DIRS]++;
            }
            else
            {
                item = new FileVolumeItem(db);
                long size = (long)reader["size"];

                ((FileVolumeItem)item).SetFileVolumeItemFields(size, null);
                counters[TOTAL_FILES]++;
                counters[TOTAL_SIZE] += size;
            }

            string path = (string)reader["path"];

            Debug.Assert(path.StartsWith("file://"), "path starts with 'file://'");

            string name = (string)reader["name"];

            string location = DecoderUtility.UrlDecode(path);

            location = location.Substring(rootPath.Length);
            location = location.Substring(0, location.Length - name.Length - 1);

            if (location.Length == 0)
            {
                location = "/";
            }

            long itemID   = 2 + (long)reader["id"] - minFileID;           // id 1 is the root item
            long parentID = Math.Max(1, 2 + (long)reader["idparent"] - minFileID);

            item.SetFileSystemVolumeItemFields(location,
                                               DateTime.MinValue,
                                               VolumeDatabase.ID_NONE);
            item.SetVolumeItemFields(volumeID,
                                     itemID,
                                     parentID,
                                     name,
                                     Util.ReplaceDBNull <string>(reader["mime"], null),
                                     metaData,
                                     Util.ReplaceDBNull <string>(reader["comment"], null),
                                     null);

            writer.Write(item);
        }
Example #2
0
        private void ImportFile(XmlNode node,
                                string comment,
                                long volumeID,
                                long parentID,
                                long itemID,
                                Stack <string> path,
                                MetadataStore metaData)
        {
            FileSystemVolumeItem item;
            string   location = "/" + string.Join("/", path.Reverse());
            string   name     = node.Attributes["name"].Value;
            string   mimeType;
            DateTime lastWriteTime;

            if (node.Name == "directory")
            {
                item     = new DirectoryVolumeItem(targetDb);
                mimeType = VolumeScanner.FilesystemVolumeScanner.MIME_TYPE_DIRECTORY;
                counters[TOTAL_DIRS]++;
            }
            else
            {
                item = new FileVolumeItem(targetDb);
                // prepend a non-existing path to ensure the file doesn't actually exist
                // in the current environment directory
                mimeType = MimeType.GetMimeTypeForFile(mimePathPrefix + name);
                long size = ConvertSize(node.Attributes["size"].Value);

                ((FileVolumeItem)item).SetFileVolumeItemFields(size, null);
                counters[TOTAL_FILES]++;
                counters[TOTAL_SIZE] += size;
            }

            if (!DateTime.TryParseExact(node.Attributes["time"].Value, DATETIME_FORMAT, ci.DateTimeFormat,
                                        DateTimeStyles.None, out lastWriteTime))
            {
                lastWriteTime = DateTime.MinValue;
            }

            item.SetFileSystemVolumeItemFields(location,
                                               lastWriteTime,
                                               VolumeDatabase.ID_NONE);
            item.SetVolumeItemFields(volumeID,
                                     itemID,
                                     parentID,
                                     name,
                                     mimeType,
                                     metaData,
                                     comment,
                                     null);

            writer.Write(item);
        }
        private long InsertFile(string rootPath,
                                FileInfo file,
                                BufferedVolumeItemWriter writer,
                                long parentID,
                                string mimeType,
                                MetadataStore metaData,
                                string hash)
        {
            /* if scanner has no db associated, just update the counters
             * and return
             */
            if (!this.HasDB)
            {
                Interlocked.Increment(ref VolumeInfo.files);
                Interlocked.Add(ref VolumeInfo.size, file.Length);
                return(VolumeDatabase.ID_NONE);
            }

            DateTime lastWriteTime = GetLastWriteTime(file);

            FileVolumeItem item = GetNewVolumeItem <FileVolumeItem>(parentID,
                                                                    file.Name,
                                                                    mimeType,
                                                                    metaData,
                                                                    VolumeItemType.FileVolumeItem);

            item.SetFileSystemVolumeItemFields(GetLocation(file.FullName, rootPath),
                                               lastWriteTime,
                                               VolumeDatabase.ID_NONE);

            item.SetFileVolumeItemFields(file.Length, hash);
            //item.Name = file.Name; // set the items name (defined on VolumeItem baseclass)

            writer.Write(item);

            Interlocked.Increment(ref VolumeInfo.files);
            Interlocked.Add(ref VolumeInfo.size, file.Length);

            if (!Options.DiscardSymLinks)
            {
                symLinkHelper.AddFile(file.FullName, item.ItemID);
            }

            return(item.ItemID);
        }
Example #4
0
        private static void ImportFile(IDataReader reader,
		                               long volumeID,
		                               long minFileID,
		                               string rootPath,
		                               MetadataStore metaData,
		                               VolumeDatabase db,
		                               BufferedVolumeItemWriter writer,
		                               long[] counters)
        {
            FileSystemVolumeItem item;

            if ((string)reader["type"] == "directory") {
                item = new DirectoryVolumeItem(db);
                counters[TOTAL_DIRS]++;
            } else {
                item = new FileVolumeItem(db);
                long size = (long)reader["size"];

                ((FileVolumeItem)item).SetFileVolumeItemFields(size, null);
                counters[TOTAL_FILES]++;
                counters[TOTAL_SIZE] += size;
            }

            string path = (string)reader["path"];
            Debug.Assert(path.StartsWith("file://"), "path starts with 'file://'");

            string name = (string)reader["name"];

            string location = DecoderUtility.UrlDecode(path);
            location = location.Substring(rootPath.Length);
            location = location.Substring(0, location.Length - name.Length - 1);

            if (location.Length == 0)
                location = "/";

            long itemID = 2 + (long)reader["id"] - minFileID; // id 1 is the root item
            long parentID = Math.Max(1, 2 + (long)reader["idparent"] - minFileID);

            item.SetFileSystemVolumeItemFields(location,
                                               DateTime.MinValue,
                                               VolumeDatabase.ID_NONE);
            item.SetVolumeItemFields(volumeID,
                                     itemID,
                                     parentID,
                                     name,
                                     Util.ReplaceDBNull<string>(reader["mime"], null),
                                     metaData,
                                     Util.ReplaceDBNull<string>(reader["comment"], null),
                                     null);

            writer.Write(item);
        }
Example #5
0
        internal override void ScanningThreadMain(PlatformIO.DriveInfo drive,
                                                  AudioCdVolume volume,
                                                  BufferedVolumeItemWriter writer)
        {
            if (Options.ComputeHashs)
            {
                SendScannerWarning(S._("Hashcode generation not implemented for audio cds yet."));

                volume.IsHashed = false;
            }

            AudioCdRootVolumeItem root = GetNewVolumeItem <AudioCdRootVolumeItem>(VolumeDatabase.ID_NONE,
                                                                                  "/",
                                                                                  null,
                                                                                  MetadataStore.Empty,
                                                                                  VolumeItemType.AudioCdRootVolumeItem);

            LocalDisc localdisc = LocalDisc.GetFromDevice(drive.Device);

            if (localdisc == null)
            {
                throw new ApplicationException("Could not read contents of the audio cd");
            }

            TimeSpan[] durations = localdisc.GetTrackDurations();
            List <AudioTrackVolumeItem> items = new List <AudioTrackVolumeItem>();

            for (int i = 0; i < durations.Length; i++)
            {
                AudioTrackVolumeItem item = GetNewVolumeItem <AudioTrackVolumeItem>(root.ItemID,
                                                                                    "Track " + (i + 1),
                                                                                    MIME_TYPE_AUDIO_TRACK,
                                                                                    MetadataStore.Empty,
                                                                                    VolumeItemType.AudioTrackVolumeItem);
                item.SetAudioTrackVolumeItemFields(durations[i]);

                items.Add(item);

                VolumeInfo.Tracks++;
                VolumeInfo.Duration = VolumeInfo.Duration.Add(durations[i]);
            }

            // retrieve musicbrainz metadata
            // (the metadata field of AudioTrackVolumeItems is set
            // depending on the EnableMusicBrainz flag)
            if (Options.EnableMusicBrainz)
            {
                try {
                    // may throw MusicBrainzNotFoundException
                    Release release = Release.Query(localdisc).PerfectMatch();

                    CheckForCancellationRequest();

                    if (release == null)
                    {
                        SendScannerWarning(S._("No MusicBrainz metadata available for this disc."));
                    }
                    else
                    {
                        var tracks = release.GetTracks();

                        if (tracks.Count != items.Count)
                        {
                            SendScannerWarning(S._("The trackcount retrieved from MusicBrainz does not match the trackcount of the local disc. Skipped."));
                        }
                        else
                        {
                            string albumTitle  = release.GetTitle();
                            int    releaseYear = GetReleaseYear(release);

                            for (int i = 0; i < tracks.Count; i++)
                            {
                                items[i].Name     = tracks[i].GetTitle();
                                items[i].MetaData = GetMetadata(tracks[i], albumTitle, releaseYear);
                            }

                            volume.Title = albumTitle;

                            // preset category
                            ReleaseType rtype = release.GetReleaseType();
                            if (rtype == ReleaseType.Album ||
                                rtype == ReleaseType.EP ||
                                rtype == ReleaseType.Compilation ||
                                rtype == ReleaseType.Remix)
                            {
                                volume.Category = PRESELECTED_CATEGORY;
                            }
                        }
                    }
                } catch (MusicBrainzNotFoundException) {
                    SendScannerWarning(S._("Error connecting to MusicBrainz server."));
                }
            }

            volume.SetAudioCdVolumeFields(VolumeInfo.Tracks, VolumeInfo.Duration);

            // write items
            if (this.HasDB)
            {
                writer.Write(root);

                foreach (AudioTrackVolumeItem item in items)
                {
                    writer.Write(item);
                }
            }
        }
            public void InsertSymLinkItems(BufferedVolumeItemWriter writer, long volumeID)
            {
                if (symLinkItems.Count == 0)
                {
                    return;
                }

                /* if scanner has no db associated, just update the counters
                 * and return */
                if (!scanner.HasDB)
                {
                    foreach (SymLinkItem sli in symLinkItems)
                    {
                        if (sli.isDir)
                        {
                            Interlocked.Increment(ref scanner.VolumeInfo.directories);
                        }
                        else
                        {
                            Interlocked.Increment(ref scanner.VolumeInfo.files);
                        }

                        // TODO :
                        // increase totalsize by size of symlinks too? (not size of target!)
                        // or are symlinks as big as dirs, those aren't respected as well..
                        //Interlocked.Add(ref VolumeInfo.size, sli.size);
                    }
                    return;
                }

                // make sure all files/dirs have been written to the database
                // before searching for symlink targets.
                writer.Flush();

                foreach (SymLinkItem sli in symLinkItems)
                {
                    scanner.CheckForCancellationRequest();

                    long itemID;
                    if (!files.TryGetValue(sli.fullTargetPath, out itemID))
                    {
                        /* may throw ScanCancelledException */
                        scanner.SendScannerWarning(string.Format(S._("Failed to resolve target item for symlink '{0}'."),
                                                                 sli.fullPath));
                    }
                    else
                    {
                        SearchCriteriaGroup g = new SearchCriteriaGroup(MatchRule.AllMustMatch);
                        g.AddSearchCriteria(new IDSearchCriteria(volumeID, IDSearchField.VolumeID, CompareOperator.Equal));
                        g.AddSearchCriteria(new IDSearchCriteria(itemID, IDSearchField.ItemID, CompareOperator.Equal));

                        // query target item.
                        // async BeginItemSearch() won't work here
                        // (active transaction prevents other threads from accessing the database)
                        VolumeItem[] queriedItems = scanner.Database.SearchItem(g);

                        FileSystemVolumeItem targetItem = (FileSystemVolumeItem)queriedItems[0];
                        FileSystemVolumeItem newItem;

                        if (targetItem is FileVolumeItem)
                        {
                            newItem = scanner.GetNewVolumeItem <FileVolumeItem>(sli.parentID,
                                                                                sli.name,
                                                                                targetItem.MimeType,
                                                                                targetItem.MetaData,
                                                                                VolumeItemType.FileVolumeItem);

                            ((FileVolumeItem)newItem).SetFileVolumeItemFields(((FileVolumeItem)targetItem).Size,
                                                                              ((FileVolumeItem)targetItem).Hash);

                            Interlocked.Increment(ref scanner.VolumeInfo.files);
                        }
                        else                             // DirectoryVolumeItem
                        {
                            newItem = scanner.GetNewVolumeItem <DirectoryVolumeItem>(sli.parentID,
                                                                                     sli.name,
                                                                                     targetItem.MimeType,
                                                                                     targetItem.MetaData,
                                                                                     VolumeItemType.DirectoryVolumeItem);

                            Interlocked.Increment(ref scanner.VolumeInfo.directories);
                        }

                        newItem.SetFileSystemVolumeItemFields(sli.location,
                                                              targetItem.LastWriteTime,
                                                              targetItem.ItemID);

                        writer.Write(newItem);

                        // TODO :
                        // increase totalsize by size of symlinks too? (not size of target!)
                        // or are symlinks as big as dirs, those aren't respected as well..
                        //Interlocked.Add(ref VolumeInfo.size, sli.size);
                        if (Global.EnableDebugging)
                        {
                            Debug.WriteLine("Successfully resolved and saved symlink item: {0}/{1} -> {2}/{3}",
                                            (sli.location == PATH_SEPARATOR.ToString() ? "" : sli.location),
                                            sli.name,
                                            (targetItem.Location == PATH_SEPARATOR.ToString() ? "" : targetItem.Location),
                                            (targetItem.Name == PATH_SEPARATOR.ToString() ? "" : targetItem.Name));
                        }
                    }             // end if
                }                 // end foreach
            }
        private long InsertDir(string rootPath,
                               DirectoryInfo dir,
                               BufferedVolumeItemWriter writer,
                               long parentID)
        {
            /* if scanner has no db associated, just update the counters
             * and return */
            if (!this.HasDB)
            {
                // TODO :
                // increase dircounter for symlink to dirs as well?
                // nautilus refers to selected symlinks to dirs as dirs too.
                Interlocked.Increment(ref VolumeInfo.directories);
                return(VolumeDatabase.ID_NONE);
            }

            string location;
            string name;

            /* if parentID is ID_NONE, the directory is the volumes root dir
             * -> location = null, name = "/" (analog System.IO.DirectoryInfo)
             */
            if (parentID == VolumeDatabase.ID_NONE)
            {
                location = null;
                name     = PATH_SEPARATOR.ToString();
            }
            else
            {
                location = GetLocation(dir.FullName, rootPath);
                name     = dir.Name;
            }

            DateTime lastWriteTime = GetLastWriteTime(dir);

            DirectoryVolumeItem item = GetNewVolumeItem <DirectoryVolumeItem>(parentID,
                                                                              name,
                                                                              MIME_TYPE_DIRECTORY,
                                                                              MetadataStore.Empty,
                                                                              VolumeItemType.DirectoryVolumeItem);

            item.SetFileSystemVolumeItemFields(location, lastWriteTime, VolumeDatabase.ID_NONE);
            //item.Name = name; // set the items name (defined on VolumeItem baseclass)

//			  if (isSymlink) {
//				  /* don't dump symlink dirs directly into the database,
//				   * they're required to have a target item assigned.
//				   * target items are resolved in an additional step.
//				   */
//				   symLinkItems.add(symLinkTarget, item);
//			  } else {
            writer.Write(item);
//			  }

            // TODO :
            // increase dircounter for symlink to dirs as well?
            // nautilus refers to selected symlinks to dirs as dirs too.
            Interlocked.Increment(ref VolumeInfo.directories);

            if (!Options.DiscardSymLinks)
            {
                symLinkHelper.AddFile(dir.FullName, item.ItemID);
            }

            return(item.ItemID);
        }