Forum Xamarin.Mac

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Extended Attributes

Hi,
How can i access to the extended attributes from Xamarin?
Same as i can see with xattr from command line.
or using #include <sys/xattr.h>

is there a nuget or something for that?

also how can i get the file size as i see it in finder (size on disk) instead of the file size i see in finder itself.

Thank you for your help

Posts

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    You will likely need to p/invoke into the C API or shell out to the command line tool.

    I don't know of a nuget that handles this, but one might exist.

  • evgeny_shteinevgeny_shtein Member ✭✭

    p/invoke you mean to write something of my own in c and insert it as a native framework?

    I tried to shell out, but i need to run this on many files and it takes a lot of time.

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    No, you can call C functions from C# with p/invoke

    I assume you need these functions:

    ssize_t listxattr(const char *path, char *list, size_t size);
    int setxattr(const char *path, const char *name, const void *value, size_t size, int flags);
    

    However, binding thing may be a bit non-trivial, give that listxattr allocating inside of the list and so on.

    You could write a small native tool that does the specific bulk xattr work and shell one time into it, if your job is well defined.

  • evgeny_shteinevgeny_shtein Member ✭✭
    edited January 2020

    @ChrisHamons said:
    No, you can call C functions from C# with p/invoke

    I assume you need these functions:

    ssize_t listxattr(const char *path, char *list, size_t size);
    int setxattr(const char *path, const char *name, const void *value, size_t size, int flags);
    

    However, binding thing may be a bit non-trivial, give that listxattr allocating inside of the list and so on.

    You could write a small native tool that does the specific bulk xattr work and shell one time into it, if your job is well defined.

    if someone will need

            [DllImport("libextension.dylib")]
            public static extern long listxattr(string path, byte[] list, ulong size, ulong options);
    
            private List<string> parseAttributes(List<byte> data)
            {
                var attributes = new List<string>();
                int start = 0;
                for (int i = 0; i < data.Count; i ++)
                {
                    if (data[i]==0)
                    {
                        var attribute = System.Text.Encoding.UTF8.GetString(data.GetRange(start, i-start).ToArray());
                        attributes.Add(attribute);
                        start = i+1;
                    }
                }
    
                return attributes;
            }
    

    usage

                var s = listxattr(path, null, 0, 0);
                if (s>0)
                {
                    byte[] attribs = new byte[s];
                    var s2 = listxattr(path, attribs, (ulong)s, 0);
                    List<string> att = parseAttributes(attribs.ToList());
                }
    
Sign In or Register to comment.