Windows: get owner and group of a file

K

kai rosenthal

Hello,

with ls -l on windows I get
-rw-r--r-- 1 500 everyone 320 Nov 09 09:35 myfile

How can I get on windows with a standard python 2.2 (without windows
extensions) the information "500" and "everyone" (owner and group)?
Also I cannot use popen('ls -l').

With
import stat
stat_info = os.lstat(myfile)
owner = "%-8s" % stat_info.st_uid
group = "%-8s" % stat_info.st_gid
I get 0 for owner and group.

Thanks for your hints, Kai
 
D

Diez B. Roggisch

kai said:
Hello,

with ls -l on windows I get
-rw-r--r-- 1 500 everyone 320 Nov 09 09:35 myfile

How can I get on windows with a standard python 2.2 (without windows
extensions) the information "500" and "everyone" (owner and group)?
Also I cannot use popen('ls -l').

Are you by any chance running cygwin? That comes with ls, but windows
doesn't. So you need to add the appropriate cygwin binary dirs to you path.
But you probably won't want that anyway, as thus your code would only run
on a machine with cygwin installed.
With
import stat
stat_info = os.lstat(myfile)
owner = "%-8s" % stat_info.st_uid
group = "%-8s" % stat_info.st_gid
I get 0 for owner and group.

I'm not sure if stat-calls are fully supported on windows - the windows
rights management is considerably different from unixish ones.

From the docs:

"""
For backward compatibility, the return value of stat() is also accessible as
a tuple of at least 10 integers giving the most important (and portable)
members of the stat structure, in the order st_mode, st_ino, st_dev,
st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime. More items
may be added at the end by some implementations. The standard module stat
defines functions and constants that are useful for extracting information
from a stat structure. (On Windows, some items are filled with dummy
values.)
"""

Note the last sentence. You should try and look what win32 has to offer.

Diez
 
F

Fredrik Lundh

Tim said:
Wow. Python 2.2. No extensions. Not even popen (). You don't
want much, do you? I *think* the answer is that you can't.

does the "group" concept even exist on Windows ? cannot recall I've
ever seen "ls -l" print anything but "everyone"...

</F>
 
T

Tim Chase

with ls -l on windows I get
Are you by any chance running cygwin? That comes with ls, but
windows doesn't.

Another alternative might be mounting their Windows-formatted
drive from within a *nix-like OS. These permissions are usually
set via the /etc/fstab mounting options for the drive. I don't
remember what the defaults are, as I have mine set up to take a
forced set of uid/gid for users/groups specific to my system.
Because at least FAT32 doesn't have this idea of groups (mapping
to NTFS is an entirely different ball of wax), you can specify a
default mask for everything or specify the directory mask
separately from the file mask.

man mount

will give more details on such goods.

-tkc
 
D

Duncan Booth

Fredrik Lundh said:
does the "group" concept even exist on Windows ? cannot recall I've
ever seen "ls -l" print anything but "everyone"...

Domain users have a 'primary group'. Actually, contrary to what I said in
the previous post I'm not sure that files have the concept. It may just be
users and the actual group permissions then get stored on the file. If so
and if you assigned any other groups permission on the file you may not be
able to distinguish which is the original group for the file and which was
added later.
 
D

Duncan Booth

kai rosenthal said:
Hello,

with ls -l on windows I get
-rw-r--r-- 1 500 everyone 320 Nov 09 09:35 myfile

How can I get on windows with a standard python 2.2 (without windows
extensions) the information "500" and "everyone" (owner and group)?
Also I cannot use popen('ls -l').

With
import stat
stat_info = os.lstat(myfile)
owner = "%-8s" % stat_info.st_uid
group = "%-8s" % stat_info.st_gid
I get 0 for owner and group.

Thanks for your hints, Kai

You can get the owner by doing os.popen('dir /q') and parsing the output,
but it is a string not a number (which I guess is why stat/lstat can't
return a value). Internally the ownber and primary group are stored as
security identifier (SID) values: a SID is a variable length structure.

Running the CACLS command on a file will give you the full permission
settings for that file. They are a lot more complex than the simple rwx
settings from unix. e.g.

C:\temp>cacls t.py
C:\temp\t.py BUILTIN\Administrators:F
NT AUTHORITY\SYSTEM:F
MYPC\Duncan:F
BUILTIN\Users:R


C:\temp>cacls .
C:\temp BUILTIN\Administrators:F
BUILTIN\Administrators:(OI)(CI)(IO)F
NT AUTHORITY\SYSTEM:F
NT AUTHORITY\SYSTEM:(OI)(CI)(IO)F
MYPC\Duncan:F
CREATOR OWNER:(OI)(CI)(IO)F
BUILTIN\Users:R
BUILTIN\Users:(OI)(CI)(IO)(special access:)
GENERIC_READ
GENERIC_EXECUTE

BUILTIN\Users:(CI)(special access:)
FILE_APPEND_DATA

BUILTIN\Users:(CI)(special access:)
FILE_WRITE_DATA

So far as I know, to get the primary group for a file you will need to call
the win32 function GetSecurityInfo asking for the
GROUP_SECURITY_INFORMATION. This will give you an appropriate security
descriptor. See http://msdn2.microsoft.com/en-us/library/aa379561.aspx
 
M

MRAB

kai said:
Hello,

with ls -l on windows I get
-rw-r--r-- 1 500 everyone 320 Nov 09 09:35 myfile

How can I get on windows with a standard python 2.2 (without windows
extensions) the information "500" and "everyone" (owner and group)?
Also I cannot use popen('ls -l').

With
import stat
stat_info = os.lstat(myfile)
owner = "%-8s" % stat_info.st_uid
group = "%-8s" % stat_info.st_gid
I get 0 for owner and group.

Thanks for your hints, Kai
If you can't use os.popen('ls -l'), can you instead use os.system('ls
-l >C:\\Temp\\result.txt') and then parse result.txt?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top