tannercollin / standardnotes-fs

Mount your Standard Notes as a filesystem.
GNU General Public License v3.0
148 stars 12 forks source link

Consider supporting Windows #15

Open Cobertos opened 5 years ago

Cobertos commented 5 years ago

Your upstream fusepy supports Windows using winfsp.

I was able to get standardnotes-fs to run on Windows but wasn't able to get it to actually work. Because I was originally using dummy gid and uid values, I would get permission denied errors (similar to this user doing a similar migration)

It seems the only change that needs to be made is to have Windows call it's equivalent of getuid() and getgid() and then register the conversion from Windows sid to uid/gid with winfsp. I tried my best but got stuck when trying to convert between the pywin32 sid object and the winfspy cffi binding sid object. They're not the same size and the latter doesn't expose anything do me to use and I'm not familiar enough with the intricacies of these frameworks to get further.

Here's what I ended up with, replaced a few lines in sn_fuse.py

from platform import system
_system = system()
        if _system == 'Windows' or _system.startswith('CYGWIN'):
            #Convert windows sid to uid and gid with WinFSP
            #use WinFSP bindings
            from win32security import GetTokenInformation, OpenProcessToken, TOKEN_QUERY, TokenUser
            from win32process import GetCurrentProcess
            from winfspy.operations import lib
            sid = GetTokenInformation(OpenProcessToken(GetCurrentProcess(), TOKEN_READ), TokenUser)
            lib.FspPosixMapSidToUid(sid[0], 2) #Errors, here, sid is a PySID (from pywin32), where lib FspPosixMapSidToUid expects a cffi PSID
            lib.FspPosixMapSidToUid(sid[0], 3)
            self.uid = 2
            self.gid = 3
        else:
            self.uid = os.getuid()
            self.gid = os.getgid()
tannercollin commented 5 years ago

This is a good idea! Unfortunately, I don't know anything about windows and don't have easy access to a system I can test on. I'll definitely need help on this one.

What about blasting away self.uid and self.gid? On Linux, this will make the notes appear owned by root:root but it makes no difference in usability because FUSE still allows reading and writing.

You might get those same permission denied errors from before, however. This would only help if winfsp sets it to defaults that work.

Cobertos commented 5 years ago

You mean like not even passing them to the dir_stat and node_stat dicts at all? Originally I tried setting both to -1 to get the service to run on Windows and mount a folder but I didn't try removing them completely. I'll have to try that the next time I give this a shot, it'd be nice to have this working on Windows even without proper permissions.

IIRC I also read about a flag in winfsp for setting default permissions uid and gid, perhaps I should figure out if it's possible to query/use those. I'll look into this as well.

tannercollin commented 5 years ago

@Cobertos did you get a chance to test removing self.uid and self.gid?

Cobertos commented 5 years ago

Nope, not yet. I've been buried in other projects but I should have some more time tonight.

On Fri, Feb 22, 2019, 12:45 AM Tanner Collin notifications@github.com wrote:

@Cobertos https://github.com/Cobertos did you get a chance to test removing self.uid and self.gid?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tannercollin/standardnotes-fs/issues/15#issuecomment-466281536, or mute the thread https://github.com/notifications/unsubscribe-auth/AGcjCFn078JWvQzbsI2r9p-t2QcG4rlKks5vP4QRgaJpZM4bALPM .

Cobertos commented 5 years ago

Tried blowing away the passing of st_gid and st_uid into the dict and I get the same error. The default parameters I was looking into are also only for the CLI invocation of winfsp and I don't think they're supported through fusepy.

Cobertos commented 5 years ago

So after looking at this more, I think the route needs to be either to add all the windows functions from win32api or just ConvertStringSidToSid to winsfpy.

I figured out how to use cffi and compiled my own module with ConvertStringSidToSid but apparently you're not allowed to mix types from two instances of ffi, so I'll probably have to locally clone [winsfpy] and make a PR to get this to work. I'll probably be back once I have time to do that.

tannercollin commented 5 years ago

Sounds good, thanks for looking at this!

AlJohri commented 4 years ago

I would also be really interested in Windows support. @Cobertos do you have any thoughts on where you left off with this?

Cobertos commented 4 years ago

@AlJohri

IIRC, using winsfpy and win32api separately get you 95% of the way there. You can get the current user SID with win32api code above, but there's no way to convert it to winsfpys internal SID object to store an SID to UID/GID mapping. Unfortunately wasn't able to get any insight from winsfpys developers https://github.com/Scille/winfspy/issues/1

Cloning winsfpy and adding a binding to convert from a string SID to it's internal SID object should be the final step of this problem (or perhaps there's a lower level solution by working with CFFI internals but I'm unaware). I think there's already bindings for certain win32 API calls in winsfpy so it should be as simple as adding another for ConvertStringSidToSid. Then, using the code in the OP, that should add the functionality to this library, where on Windows, it'll do the SID conversion to get the UID/GID to set the files with when passing it to fusepy.

I've since transitioned to using a different note app though, so I haven't put any more work into this.

tannercollin commented 4 years ago

@Cobertos which note app? I'm always interested in alternatives.

Edit: I see you're using Notion.so. What made you switch? Do you pay for it?

Cobertos commented 4 years ago

Notion.so, not selfhosted or e2e encrypted, but it does a lot of stuff right and the API has been a dream. The nested note structure just really works for my brain and the data model backing it is super easy to work with.

On Wed, Apr 22, 2020, 11:10 PM Tanner Collin notifications@github.com wrote:

@Cobertos https://github.com/Cobertos which note app? I'm always interested in alternatives.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tannercollin/standardnotes-fs/issues/15#issuecomment-618150844, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTSGCAI7JP7Z6XUAZ6AMMLRN6WSZANCNFSM4GYAWPGA .