Detecting Symbolic Links and Pipes in Mono

How can I use Mono.Unix on Linux?

You need to do what the error message says. Unintuitively, Mono.Unix lives in Mono.Posix.dll, so you need to add that as reference.

Mono cannot access file even though linux shell can without root

Guesses:

  • You unexport it first, then export it and immediately access it, I had in the past several time problems on some OS e. g. deleting a file and immediately creating it again, add a waiting time (not really clean code, but if it works)
  • check the difference of the files/folders with linux command "ls -la" or more
  • call the shell command, that worked, from C# via e. g. Process class
  • Some few times Mono did not work like C# on Windows, a workaround had to be found
  • try to open a stream to these files and leave them open? Or even a pipe is possible to these files?

How would you create a hard link (Linux) from within an application without passing arguments to the ln utility directly?

I assuming you mean, how do I create a hard link without shelling out the the ln command.

Short answer,

using Mono.Unix.Native;
Mono.Unix.Native.Syscall.link(string oldname, string newname).

Ultimately you do this by invoking the link() function

Looking at the man page: link() creates a new link (also known as a hard link) to an existing file. The linkat() function is also documented which might be a better choice for you.

int link(const char *oldpath, const char *newpath);

Clearly, not a C# friendly system call (they rarely are). So, as far as calling it from C# is concerned, you can either go through creating the necessary steps to call it from C# (translating the .h constants, etc.) -- or you can just call link() directly from a C/C++ interface module in your project, and then call that interface from your C# code. Since Unix system calls are not C++ based, you don't run into name mangling problems.

To call external functions, you use pinvoke technology (also included in Microsofts .Net framework). A bit long too replicate everything in that reference, but the key features are pretty simple.

Link to the shared library using DllImport, and declare the external functions you need. E.g.

[DllImport ("libc.so")]
public static int link(string oldpath, string newpath)

You might also find an os interface module written in C# that translates system call errors into more natural C# exceptions is a better choice in the long term. To be honest, linking the .so file and invoking the Linux system calls is not that hard and you might conclude you prefer this approach over a separate C/C++ module.

If you ever have to migrate your mono code to a windows platform, using an os interface module will simplify your life. BTW, you can also create hard links on Windows on NTFS file systems, but few Windows people use them, much less expect them.

And finally, the real answer: The Mono.Unix NameSpace is a nice choice for most system calls, and link() is not an exception. If you look in the SysCall class of Mono.Unix.Native you should find the link() method.

using Mono.Unix.Native;
SysCall.Link(ExistingName, NewLinkName);

Unfortunately, the Mono docs do not use a RESTful interface, so I can't give you are direct link the correct page. Start at and then drill down using the tree interface

I thought a teach you to fish answer was more appropriate in this case. You could download the mono native source and see exactly how mono does it.

Noticed the comment by Diego - His comment answer was include within my longer answer.

How can I compare (directory) paths in C#?

From this answer, this method can handle a few edge cases:

public static string NormalizePath(string path)
{
return Path.GetFullPath(new Uri(path).LocalPath)
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
.ToUpperInvariant();
}

More details in the original answer. Call it like:

bool pathsEqual = NormalizePath(path1) == NormalizePath(path2);

Should work for both file and directory paths.

Module's not resolving in typescript monorepo with Next.js projects

I had tried the provided answers and unfortunately they didn't work for me. What did end up fixing it, after reading through some documentation, was a simple tsconfig change in app-1:

{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"*": ["*", "../../app-2/src/*"], // try to resolve in the current baseUrl, if not use the fallback.
"@apps/app-2/*": ["../../app-2/src/*"], // reference app-2 imports inside app-1 like "import X from '@apps/app-2/components'"
}
}
}

Note that since these are both Next.js projects sharing code with each other, I had to use next-transpile-modules and wrapped each next.config.js in the withTM function as outlined in their docs



Related Topics



Leave a reply



Submit