Skip to content

Commit

Permalink
Disable autorefresh for local repositories (bsc#948982)
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Oct 6, 2016
1 parent dba8620 commit bfcf319
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 48 deletions.
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--color --format doc
77 changes: 29 additions & 48 deletions src/include/packager/repositories_include.rb
@@ -1,15 +1,13 @@
# encoding: utf-8

# File: packager/repositories_include.ycp
# File: packager/repositories_include.rb
#
# Author: Cornelius Schumacher <cschum@suse.de>
# Ladislav Slezak <lslezak@suse.cz>
# Lukas Ocilka <locilka@suse.cz>
#
# Purpose: Include file to be shared by yast2-packager and yast2-add-on
#
# $Id$
#
module Yast
module PackagerRepositoriesIncludeInclude
include Yast::Logger
Expand Down Expand Up @@ -118,22 +116,14 @@ def createSourceImpl(url, plaindir, download, preffered_name, force_alias)
idx = Ops.add(idx, 1)
end

autorefresh = true
schema = Builtins.tolower(Builtins.substring(url, 0, 3))

if schema == "cd:" || schema == "dvd"
autorefresh = false
Builtins.y2milestone("Disabling autorefresh for a CD/DVD service")
end

# use alias as the name if it's missing
if preffered_name == nil || preffered_name == ""
preffered_name = _alias
end

new_service = {
"alias" => _alias,
"autorefresh" => autorefresh,
"autorefresh" => autorefresh(url),
"enabled" => true,
"name" => preffered_name,
"url" => url
Expand Down Expand Up @@ -177,30 +167,6 @@ def createSourceImpl(url, plaindir, download, preffered_name, force_alias)
end

newSources = []
auto_refresh = true

# disable autorefresh for ISO images
iso_prefix = "iso:"
if Builtins.substring(url, 0, Builtins.size(iso_prefix)) == iso_prefix
Builtins.y2milestone(
"ISO image detected, disabling autorefresh (%1)",
URL.HidePassword(url)
)
auto_refresh = false
end

# CD or DVD repository?
cd_scheme = Builtins.contains(
["cd", "dvd"],
Builtins.tolower(Ops.get_string(URL.Parse(url), "scheme", ""))
)
if cd_scheme
Builtins.y2milestone(
"CD/DVD repository detected, disabling autorefresh (%1)",
URL.HidePassword(url)
)
auto_refresh = false
end

enter_again = false

Expand Down Expand Up @@ -275,7 +241,7 @@ def createSourceImpl(url, plaindir, download, preffered_name, force_alias)
# "base_urls" : list<string>, "prod_dir" : string, "type" : string ]
repo_prop = {}
Ops.set(repo_prop, "enabled", true)
Ops.set(repo_prop, "autorefresh", auto_refresh)
Ops.set(repo_prop, "autorefresh", autorefresh(url))
Ops.set(repo_prop, "name", name)
Ops.set(repo_prop, "prod_dir", Ops.get(repo, 1, "/"))
Ops.set(repo_prop, "alias", _alias)
Expand All @@ -295,17 +261,15 @@ def createSourceImpl(url, plaindir, download, preffered_name, force_alias)
repo_prop_log
)
newSources = Builtins.add(newSources, new_repo_id)
if cd_scheme
# for CD/DVD repo download the metadata immediately,
# the medium is in the drive right now, it can be changed later
# and accidentaly added a different repository
Builtins.y2milestone(
"Adding a CD or DVD repository, refreshing now..."
)

# for local repositories (e.g. CD/DVD) which have autorefresh disabled
# download the metadata immediately, the medium is in the drive right
# now, it can be changed later and accidentaly added a different repository
if !autorefresh(url)
log.info "Adding a local repository, refreshing it now..."
Pkg.SourceRefreshNow(new_repo_id)
end
end

end

# broken repository or wrong URL - enter the URL again
if enter_again
Expand Down Expand Up @@ -426,7 +390,7 @@ def StoreSource
)
)
Builtins.y2warning("Not searching for SLP repositories")
return :back
return :back
# New .slp agent has been added
# FIXME: lazy loading of agents will make this obsolete
else
Expand Down Expand Up @@ -465,7 +429,7 @@ def StoreSource
ret = createSource(url, plaindir, @download_meta, name)

case ret
when :again
when :again
:back
when :abort, :cancel
:abort
Expand Down Expand Up @@ -497,5 +461,22 @@ def EditDialog
ret = SourceDialogs.EditDialog
ret
end

# Evaluate the default autorefresh flag for the given repository URL.
# @param url [String] Repository URL
# @return [Boolean] The default autorefresh flag for the URL
def autorefresh(url)
log.info "Evaluating autorefresh flag for #{URL.HidePassword(url)}"
protocol = URL.Parse(url)["scheme"].downcase

# disable autorefresh for local repositories ,
# see https://github.com/openSUSE/libzypp/blob/master/zypp/Url.cc#L458
# for the local URL protocols defined by libzypp
local_protocols = [ "cd", "dvd", "dir", "hd", "iso", "file" ]
autorefresh = !local_protocols.include?(protocol)

log.info "Autorefresh flag for '#{protocol}' URL protocol: #{autorefresh}"
autorefresh
end
end
end
35 changes: 35 additions & 0 deletions test/repositories_include_test.rb
@@ -0,0 +1,35 @@
#! /usr/bin/env rspec

require_relative "./test_helper"

# just a wrapper class for the repositories_include.rb
class RepositoryIncludeTester
extend Yast::I18n
Yast.include self, "packager/repositories_include.rb"
end

describe RepositoryIncludeTester do
describe ".autorefresh" do
# local protocols
[ "cd", "dvd", "dir", "hd", "iso", "file" ].each do |protocol|
url = "#{protocol}://foo/bar"
it "returns false for local '#{url}' URL" do
expect(described_class.autorefresh(url)).to eq(false)
end
end

# remote protocols
# see https://github.com/openSUSE/libzypp/blob/master/zypp/Url.cc#L464
[ "http", "https", "nfs", "nfs4", "smb", "cifs", "ftp", "sftp", "tftp" ].each do |protocol|
url = "#{protocol}://foo/bar"
it "returns true for remote '#{url}' URL" do
expect(described_class.autorefresh(url)).to eq(true)
end
end

it "handles uppercase URLs correctly" do
expect(described_class.autorefresh("FTP://FOO/BAR")).to eq(true)
expect(described_class.autorefresh("DVD://FOO/BAR")).to eq(false)
end
end
end

0 comments on commit bfcf319

Please sign in to comment.