Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #7: Add controlfile (control file) directive to be able to add custom control file in the config.tar.gz section #8

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 85 additions & 33 deletions DotnetMakeDeb/Deb/DebPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace DotnetMakeDeb.Deb
internal class DebPackage
{
private readonly List<string> createdDirs = new List<string>();
private readonly List<DebFileItem> controlFileItems = new List<DebFileItem>();
private readonly List<DebFileItem> fileItems = new List<DebFileItem>();
private readonly Dictionary<string, string> variables = new Dictionary<string, string>();

Expand Down Expand Up @@ -279,21 +280,57 @@ public void ReadSpecification(string fileName)
continue;
}

m = Regex.Match(line, @"^controlfile\s*:\s*(\S+)(?:\s+(text))?(?:\s+([0-9]+)(?:\s+([0-9]+)\s+([0-9]+))?)?\s*$", RegexOptions.IgnoreCase);
if (m.Success)
{
// Add data file as control file
var fileItem = new DebFileItem
{
SourcePath = ResolveVariables(m.Groups[1].Value.Trim()),
DestPath = null,
IsConfig = false,
IsDirectory = false,
IsText = m.Groups[2].Value == "text"
};
if (!Path.IsPathRooted(fileItem.SourcePath))
{
// Interpret non-rooted paths relative to the base path
fileItem.SourcePath = Path.Combine(srcBasePath, fileItem.SourcePath);
}
if (m.Groups[3].Length > 0)
fileItem.Mode = Convert.ToInt32(ResolveVariables(m.Groups[3].Value), 8);
if (m.Groups[4].Length > 0)
fileItem.UserId = Convert.ToInt32(ResolveVariables(m.Groups[4].Value));
if (m.Groups[5].Length > 0)
fileItem.GroupId = Convert.ToInt32(ResolveVariables(m.Groups[5].Value));
foreach (var fi in ResolveFileItems(fileItem))
{
var existingItem = controlFileItems.FirstOrDefault(x => x.SourcePath == fi.SourcePath);
if (existingItem != null)
controlFileItems.Remove(existingItem);
controlFileItems.Add(fi);
}
inDescription = false;
continue;
}

m = Regex.Match(line, @"^file\s*:\s*(\S+)\s+(\S+)(?:\s+(text))?(?:\s+([0-9]+)(?:\s+([0-9]+)\s+([0-9]+))?)?\s*$", RegexOptions.IgnoreCase);
if (m.Success)
{
// Add data file
var fileItem = new DebFileItem
{
SourcePath = ResolveVariables(m.Groups[1].Value.Trim())
SourcePath = ResolveVariables(m.Groups[1].Value.Trim()),
DestPath = ResolveVariables(m.Groups[2].Value.Trim()),
IsConfig = false,
IsDirectory = false,
IsText = m.Groups[3].Value == "text"
};
if (!Path.IsPathRooted(fileItem.SourcePath))
{
// Interpret non-rooted paths relative to the base path
fileItem.SourcePath = Path.Combine(srcBasePath, fileItem.SourcePath);
}
fileItem.DestPath = ResolveVariables(m.Groups[2].Value.Trim());
fileItem.IsText = m.Groups[3].Value == "text";
if (m.Groups[4].Length > 0)
fileItem.Mode = Convert.ToInt32(ResolveVariables(m.Groups[4].Value), 8);
if (m.Groups[5].Length > 0)
Expand All @@ -316,22 +353,23 @@ public void ReadSpecification(string fileName)
// Add data file as conffile
var fileItem = new DebFileItem
{
SourcePath = ResolveVariables(m.Groups[1].Value.Trim())
SourcePath = ResolveVariables(m.Groups[1].Value.Trim()),
DestPath = ResolveVariables(m.Groups[2].Value.Trim()),
IsConfig = true,
IsDirectory = false,
IsText = m.Groups[3].Value == "text"
};
if (!Path.IsPathRooted(fileItem.SourcePath))
{
// Interpret non-rooted paths relative to the base path
fileItem.SourcePath = Path.Combine(srcBasePath, fileItem.SourcePath);
}
fileItem.DestPath = ResolveVariables(m.Groups[2].Value.Trim());
fileItem.IsText = m.Groups[3].Value == "text";
if (m.Groups[4].Length > 0)
fileItem.Mode = Convert.ToInt32(ResolveVariables(m.Groups[4].Value), 8);
if (m.Groups[5].Length > 0)
fileItem.UserId = Convert.ToInt32(ResolveVariables(m.Groups[5].Value));
if (m.Groups[6].Length > 0)
fileItem.GroupId = Convert.ToInt32(ResolveVariables(m.Groups[6].Value));
fileItem.IsConfig = true;
foreach (var fi in ResolveFileItems(fileItem))
{
var existingItem = fileItems.FirstOrDefault(x => x.SourcePath == fi.SourcePath);
Expand All @@ -348,7 +386,11 @@ public void ReadSpecification(string fileName)
// Add empty directory
var fileItem = new DebFileItem
{
DestPath = ResolveVariables(m.Groups[1].Value.Trim()).TrimEnd('/')
SourcePath = null,
DestPath = ResolveVariables(m.Groups[1].Value.Trim()).TrimEnd('/'),
IsConfig = false,
IsDirectory = true,
IsText = false
};
if (m.Groups[2].Length > 0)
fileItem.Mode = Convert.ToInt32(ResolveVariables(m.Groups[2].Value), 8);
Expand All @@ -358,7 +400,6 @@ public void ReadSpecification(string fileName)
fileItem.UserId = Convert.ToInt32(ResolveVariables(m.Groups[3].Value));
if (m.Groups[4].Length > 0)
fileItem.GroupId = Convert.ToInt32(ResolveVariables(m.Groups[4].Value));
fileItem.IsDirectory = true;
fileItems.Add(fileItem);
inDescription = false;
continue;
Expand Down Expand Up @@ -468,6 +509,10 @@ public void WritePackage(Stream outStream)
{
AddFile(postRmFileName, 0, 0, 493 /* 0755 */, postRmIsText);
}
foreach (var controlFileItem in controlFileItems)
{
AddFile(controlFileItem.SourcePath, controlFileItem.UserId, controlFileItem.GroupId, controlFileItem.Mode, controlFileItem.IsText);
}

WriteControl();

Expand Down Expand Up @@ -787,18 +832,20 @@ private List<DebFileItem> ResolveFileItems(DebFileItem fileItem)
var fi = new DebFileItem
{
SourcePath = fileName,
DestPath = fileItem.DestPath.TrimEnd('/') + "/" + relFileName,
DestPath = fileItem.DestPath == null ? null : fileItem.DestPath.TrimEnd('/') + "/" + relFileName,
UserId = fileItem.UserId,
GroupId = fileItem.GroupId,
Mode = fileItem.Mode,
IsConfig = fileItem.IsConfig
IsConfig = fileItem.IsConfig,
IsDirectory = fileItem.IsDirectory,
IsText = fileItem.IsText
};
fileItems.Add(fi);
}
}
else
{
if (fileItem.DestPath.EndsWith("/"))
if (fileItem.DestPath?.EndsWith("/") ?? false)
{
fileItem.DestPath += Path.GetFileName(fileItem.SourcePath);
}
Expand All @@ -814,31 +861,31 @@ private List<DebFileItem> ResolveFileItems(DebFileItem fileItem)
internal class DebControlParams
{
/// <summary>The name of the package.</summary>
public string Package;
public string Package { get; set; }
/// <summary>The version of the package.</summary>
public string Version;
public string Version { get; set; }
/// <summary>Convert the specified or overridden version from SemVer to Debian conventions.</summary>
public bool ConvertFromSemVer;
public bool ConvertFromSemVer { get; set; }
/// <summary>The section of the package.</summary>
public string Section;
public string Section { get; set; }
/// <summary>The priority of the package.</summary>
public string Priority;
public string Priority { get; set; }
/// <summary>The hardware architecture of the package.</summary>
public string Architecture;
public string Architecture { get; set; }
/// <summary>The package dependencies of the package.</summary>
public string Depends;
public string Depends { get; set; }
/// <summary>The package pre-dependencies of the package.</summary>
public string PreDepends;
public string PreDepends { get; set; }
/// <summary>The other packages that the package conflicts with.</summary>
public string Conflicts;
public string Conflicts { get; set; }
/// <summary>The total estimated size of the installed package in KiB.</summary>
public long InstalledSize;
public long InstalledSize { get; set; }
/// <summary>The name and e-mail address of the package maintainer in RFC 822 format.</summary>
public string Maintainer;
public string Maintainer { get; set; }
/// <summary>The website URL of the package.</summary>
public string Homepage;
public string Homepage { get; set; }
/// <summary>The description of the package, in the original control file multi-line syntax.</summary>
public string Description;
public string Description { get; set; }

/// <summary>
/// Gets the version that was converted from SemVer to Debian, if configured, or the original version.
Expand All @@ -862,21 +909,26 @@ public string ConvertedVersion
/// </summary>
internal class DebFileItem
{
public DebFileItem()
{
Mode = 420 /* 0644 */;
}

/// <summary>The full path of the source file to include.</summary>
public string SourcePath;
public string SourcePath { get; set; }
/// <summary>The full path of the destination file to write, not including the root slash.</summary>
public string DestPath;
public string DestPath { get; set; }
/// <summary>The user ID of the file.</summary>
public int UserId;
public int UserId { get; set; }
/// <summary>The group ID of the file.</summary>
public int GroupId;
public int GroupId { get; set; }
/// <summary>The mode of the file (decimal).</summary>
public int Mode = 420 /* 0644 */;
public int Mode { get; set; }
/// <summary>Indicates whether the file is a configuration file.</summary>
public bool IsConfig;
public bool IsConfig { get; set; }
/// <summary>Indicates whether the file is a directory.</summary>
public bool IsDirectory;
public bool IsDirectory { get; set; }
/// <summary>Indicates whether the line endings should be converted to Unix format.</summary>
public bool IsText;
public bool IsText { get; set; }
}
}