Skip to content

Commit f507bd2

Browse files
committed
Merge branch 'dev' into master
2 parents c7d9144 + 44ce576 commit f507bd2

18 files changed

Lines changed: 465 additions & 24 deletions

File tree

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
using System;
34
using System.Collections.Generic;
45
using NuGet.Server.Core.Infrastructure;
56

67
namespace NuGet.Server.V2.Samples.OwinHost
78
{
89
public class DictionarySettingsProvider : ISettingsProvider
910
{
10-
readonly Dictionary<string, bool> _settings;
11+
readonly Dictionary<string, object> _settings;
1112

12-
public DictionarySettingsProvider(Dictionary<string, bool> settings)
13+
public DictionarySettingsProvider(Dictionary<string, object> settings)
1314
{
1415
_settings = settings;
1516
}
@@ -18,8 +19,13 @@ public DictionarySettingsProvider(Dictionary<string, bool> settings)
1819
public bool GetBoolSetting(string key, bool defaultValue)
1920
{
2021
System.Diagnostics.Debug.WriteLine("getSetting: " + key);
21-
return _settings.ContainsKey(key) ? _settings[key] : defaultValue;
22+
return _settings.ContainsKey(key) ? Convert.ToBoolean(_settings[key]) : defaultValue;
2223

2324
}
25+
26+
public string GetStringSetting(string key, string defaultValue)
27+
{
28+
return _settings.ContainsKey(key) ? Convert.ToString(_settings[key]) : defaultValue;
29+
}
2430
}
2531
}

samples/NuGet.Server.V2.Samples.OwinHost/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static void Main(string[] args)
2424

2525
// Set up a common settingsProvider to be used by all repositories.
2626
// If a setting is not present in dictionary default value will be used.
27-
var settings = new Dictionary<string, bool>();
27+
var settings = new Dictionary<string, object>();
2828
settings.Add("enableDelisting", false); //default=false
2929
settings.Add("enableFrameworkFiltering", false); //default=false
3030
settings.Add("ignoreSymbolsPackages", true); //default=false
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
8+
namespace NuGet.Server.Core
9+
{
10+
/// <summary>
11+
/// A file system implementation that persists nothing. This is intended to be used with
12+
/// <see cref="OptimizedZipPackage"/> so that package files are never actually extracted anywhere on disk.
13+
/// </summary>
14+
public class NullFileSystem : IFileSystem
15+
{
16+
public static NullFileSystem Instance { get; } = new NullFileSystem();
17+
18+
public Stream CreateFile(string path) => Stream.Null;
19+
public bool DirectoryExists(string path) => true;
20+
public bool FileExists(string path) => false;
21+
public string GetFullPath(string path) => null;
22+
public Stream OpenFile(string path) => Stream.Null;
23+
24+
public ILogger Logger { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
25+
public string Root => throw new NotSupportedException();
26+
public void AddFile(string path, Stream stream) => throw new NotSupportedException();
27+
public void AddFile(string path, Action<Stream> writeToStream) => throw new NotSupportedException();
28+
public void AddFiles(IEnumerable<IPackageFile> files, string rootDir) => throw new NotSupportedException();
29+
public void DeleteDirectory(string path, bool recursive) => throw new NotSupportedException();
30+
public void DeleteFile(string path) => throw new NotSupportedException();
31+
public void DeleteFiles(IEnumerable<IPackageFile> files, string rootDir) => throw new NotSupportedException();
32+
public DateTimeOffset GetCreated(string path) => throw new NotSupportedException();
33+
public IEnumerable<string> GetDirectories(string path) => throw new NotSupportedException();
34+
public IEnumerable<string> GetFiles(string path, string filter, bool recursive) => throw new NotSupportedException();
35+
public DateTimeOffset GetLastAccessed(string path) => throw new NotSupportedException();
36+
public DateTimeOffset GetLastModified(string path) => throw new NotSupportedException();
37+
public void MakeFileWritable(string path) => throw new NotSupportedException();
38+
public void MoveFile(string source, string destination) => throw new NotSupportedException();
39+
}
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.IO;
6+
7+
namespace NuGet.Server.Core
8+
{
9+
public static class PackageFactory
10+
{
11+
public static IPackage Open(string fullPackagePath)
12+
{
13+
if (string.IsNullOrEmpty(fullPackagePath))
14+
{
15+
throw new ArgumentNullException(nameof(fullPackagePath));
16+
}
17+
18+
var directoryName = Path.GetDirectoryName(fullPackagePath);
19+
var fileName = Path.GetFileName(fullPackagePath);
20+
21+
var fileSystem = new PhysicalFileSystem(directoryName);
22+
23+
return new OptimizedZipPackage(
24+
fileSystem,
25+
fileName,
26+
NullFileSystem.Instance);
27+
}
28+
}
29+
}

src/NuGet.Server.Core/Infrastructure/DefaultSettingsProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ public bool GetBoolSetting(string key, bool defaultValue)
88
{
99
return defaultValue;
1010
}
11+
12+
public string GetStringSetting(string key, string defaultValue)
13+
{
14+
return defaultValue;
15+
}
1116
}
1217
}

src/NuGet.Server.Core/Infrastructure/ISettingsProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ namespace NuGet.Server.Core.Infrastructure
55
public interface ISettingsProvider
66
{
77
bool GetBoolSetting(string key, bool defaultValue);
8+
string GetStringSetting(string key, string defaultValue);
89
}
910
}

src/NuGet.Server.Core/Infrastructure/ServerPackageRepository.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public ServerPackageRepository(
5858
_runBackgroundTasks = true;
5959
_settingsProvider = settingsProvider ?? new DefaultSettingsProvider();
6060
_logger = logger ?? new TraceLogger();
61-
_serverPackageCache = InitializeServerPackageStore();
61+
_serverPackageCache = InitializeServerPackageCache();
6262
_serverPackageStore = new ServerPackageStore(
6363
_fileSystem,
6464
new ExpandedPackageRepository(_fileSystem, hashProvider),
@@ -81,7 +81,7 @@ internal ServerPackageRepository(
8181
_runBackgroundTasks = runBackgroundTasks;
8282
_settingsProvider = settingsProvider ?? new DefaultSettingsProvider();
8383
_logger = logger ?? new TraceLogger();
84-
_serverPackageCache = InitializeServerPackageStore();
84+
_serverPackageCache = InitializeServerPackageCache();
8585
_serverPackageStore = new ServerPackageStore(
8686
_fileSystem,
8787
innerRepository,
@@ -105,9 +105,39 @@ internal ServerPackageRepository(
105105
private bool EnableFileSystemMonitoring =>
106106
_settingsProvider.GetBoolSetting("enableFileSystemMonitoring", true);
107107

108-
private ServerPackageCache InitializeServerPackageStore()
108+
private string CacheFileName => _settingsProvider.GetStringSetting("cacheFileName", null);
109+
110+
private ServerPackageCache InitializeServerPackageCache()
109111
{
110-
return new ServerPackageCache(_fileSystem, Environment.MachineName.ToLowerInvariant() + ".cache.bin");
112+
return new ServerPackageCache(_fileSystem, ResolveCacheFileName());
113+
}
114+
115+
private string ResolveCacheFileName()
116+
{
117+
var fileName = CacheFileName;
118+
const string suffix = ".cache.bin";
119+
120+
if (String.IsNullOrWhiteSpace(fileName))
121+
{
122+
// Default file name
123+
return Environment.MachineName.ToLowerInvariant() + suffix;
124+
}
125+
126+
if (fileName.LastIndexOfAny(Path.GetInvalidFileNameChars()) > 0)
127+
{
128+
var message = string.Format(Strings.Error_InvalidCacheFileName, fileName);
129+
130+
_logger.Log(LogLevel.Error, message);
131+
132+
throw new InvalidOperationException(message);
133+
}
134+
135+
if (fileName.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
136+
{
137+
return fileName;
138+
}
139+
140+
return fileName + suffix;
111141
}
112142

113143
/// <summary>
@@ -236,7 +266,7 @@ private void AddPackagesFromDropFolderWithoutLocking()
236266
try
237267
{
238268
// Create package
239-
var package = new OptimizedZipPackage(_fileSystem, packageFile);
269+
var package = PackageFactory.Open(_fileSystem.GetFullPath(packageFile));
240270

241271
if (!CanPackageBeAddedWithoutLocking(package, shouldThrow: false))
242272
{

src/NuGet.Server.Core/NuGet.Server.Core.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
</Compile>
5959
<Compile Include="Core\Constants.cs" />
6060
<Compile Include="Core\FrameworkNameExtensions.cs" />
61+
<Compile Include="Core\NullFileSystem.cs" />
6162
<Compile Include="Core\PackageExtensions.cs" />
63+
<Compile Include="Core\PackageFactory.cs" />
6264
<Compile Include="DataServices\IgnoreCaseForPackageIdInterceptor.cs" />
6365
<Compile Include="DataServices\NormalizeVersionInterceptor.cs" />
6466
<Compile Include="DataServices\ODataPackage.cs" />

src/NuGet.Server.Core/Strings.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/NuGet.Server.Core/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="Error_InvalidCacheFileName" xml:space="preserve">
121+
<value>Configured cache file name '{0}' is invalid. Keep it simple; No paths allowed.</value>
122+
</data>
120123
<data name="Error_PackageAlreadyExists" xml:space="preserve">
121124
<value>Package {0} already exists. The server is configured to not allow overwriting packages that already exist.</value>
122125
</data>

0 commit comments

Comments
 (0)