Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Windows: get owner and group of a file

Reply
Thread Tools

Windows: get owner and group of a file

 
 
kai rosenthal
Guest
Posts: n/a
 
      12-06-2006
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

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      12-06-2006
kai rosenthal wrote:

> 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
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-06-2006
Tim Golden wrote:

> 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>

 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      12-06-2006
>> with ls -l on windows I get
>> -rw-r--r-- 1 500 everyone 320 Nov 09 09:35 myfile

>
> 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



 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      12-06-2006
Fredrik Lundh <(E-Mail Removed)> wrote:

> Tim Golden wrote:
>
>> 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"...


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.
 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      12-06-2006
"kai rosenthal" <(E-Mail Removed)> wrote:

> 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\AdministratorsOI)(CI)(IO)F
NT AUTHORITY\SYSTEM:F
NT AUTHORITY\SYSTEMOI)(CI)(IO)F
MYPC\Duncan:F
CREATOR OWNEROI)(CI)(IO)F
BUILTIN\Users:R
BUILTIN\UsersOI)(CI)(IO)(special access
GENERIC_READ
GENERIC_EXECUTE

BUILTIN\UsersCI)(special access
FILE_APPEND_DATA

BUILTIN\UsersCI)(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

 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      12-07-2006

kai rosenthal wrote:
> 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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
**Important message from Group owner and Moderator** kashamb@yahoo.com HTML 0 01-14-2008 02:16 PM
who is owner of this group? JK VHDL 2 12-18-2007 03:42 PM
Group owner, MAC OS X executables? Charles Sullivan Computer Support 9 11-13-2006 02:44 AM
how to get the owner and time information of a file in the UNIX? jojo Java 20 09-13-2005 04:28 PM
Newbie to group -- Hello -- Owner of Kodak DX6340 Midnight Moocher Digital Photography 5 05-13-2004 01:10 PM



Advertisments