---
name: "lfc2_python"
section: "3"
platform: "linux"
package: "python-lfc"
version: "1.10.0-2+b3"
release: "Debian 10 (Buster)"
platforms: ["linux"]
url: "https://new.carta.tech/lfc2_python"
---

# lfc2\_python(3)

New python interface to the LFC client API lfc2thr - Thread enabled version of Python interface to the LFC

## NAME

lfc2 - New python interface to the LFC client API\
 lfc2thr - Thread enabled version of Python interface to the LFC

## SYNOPSIS

**import lfc2 as lfc**\

**import lfc2thr as lfc**\
 **lfc.init()**

## DESCRIPTION

The lfc module permits you to access the LFC client interface from python programs. The lfc module is a swig wrapping of the standard C interface. For detailed descriptions of each function see the individual man page of each function.

The lfcthr module is a version of the lfc module supporting multi-threaded  Python clients. Its usage is similar to the usage of the lfc module except the obligatory initialisation call lfcthr.init() in the main program before  threads are started.

## FUNCTIONS

**lfc\_aborttrans ( )**

**lfc\_access ( string** *path***,** **int** *amode* **) -\> bool** *access*

**lfc\_accessr ( string** *sfn***,** **int** *amode* **) -\> bool** *access*

**lfc\_addreplica ( string** *guid***,** **struct lfc\_fileid \****file\_uniqueid***,** **string** *server***,** **string** *sfn***,** **char** *status***,** **char** *f\_type***,** **string** *poolname***,** **string** *fs* **)**

**lfc\_addreplicax ( string** *guid***,** **struct lfc\_fileid \****file\_uniqueid***,** **string** *server***,** **string** *sfn***,** **char** *status***,** **char** *f\_type***,** **string** *poolname***,** **string** *fs***,** **char** *r\_type***,** **string** *setname* **)**

**lfc\_chdir ( string** *path* **)**

**lfc\_chmod ( string** *path***,** **mode\_t** *mode* **)**

**lfc\_chown ( string** *path***,** **uid\_t** *new\_uid***,** **gid\_t** *new\_gid* **)**

**lfc\_client\_setAuthorizationId ( uid\_t** *uid***,** **gid\_t** *gid***,** **string** *mech***,** **string** *id* **)**

**lfc\_client\_setVOMS\_data ( string** *voname***,** **stringlist** *vomsdata* **)**

**lfc\_closedir ( lfc\_DIR** *dirp* **)**

**lfc\_creatg ( string** *path***,** **string** *guid***,** **mode\_t** *mode* **)**

**lfc\_delcomment ( string** *path* **)**

**lfc\_delete ( string** *path* **)**

**lfc\_delfilesbyguid ( ListOfString** *guids***,** **int** *force* **) -\> ListOfInt** *result*

**lfc\_delfilesbyname ( ListOfString** *names***,** **int***force* **) -\> ListOfInt** *results*

**lfc\_delfilesbypattern ( string** *path***,** **string** *pattern***,** **int** *force* **) -\> ListOf struct lfc\_filestatus** *results*

**lfc\_delreplica ( string** *guid***,** **struct lfc\_fileid \****file\_uniqueid***,** **string** *sfn* **)**

**lfc\_delreplicas ( ListOfString** *sfns***,** **string** *se* **) -\> ListOfInt** *results*

**lfc\_delreplicasbysfn ( ListOfString** *sfns***,** **ListOfString** *guids* **) -\> ListOfInt** *results*

**lfc\_endsess ( )**

**lfc\_endtrans ( )**

**lfc\_getacl ( string** *path***,** **) -\> ListOf struct lfc\_acl** *acls*

**lfc\_getcomment ( string** *path* **) -\> string** *comment*

**lfc\_getcwd ( ) -\> string** *dir*

**lfc\_getifcevers ( ) -\> string** *version*

**lfc\_getlinks ( string** *path***,** **string** *guid* **) -\> ListOf struct lfc\_linkinfo** *links*

**lfc\_getreplica ( string** *path***,** **string** *guid***,** **string** *se* **) -\> ListOf struct lfc\_filereplica** *replicas*

**lfc\_getreplicas ( ListOfString** *guids***,** **string** *se* **) -\> ListOf struct lfc\_filereplicas** *replicas*

**lfc\_getreplicasl ( ListOfString** *paths***,** **string** *se* **) -\> ListOf struct lfc\_filereplicas** *replicas*

**lfc\_getreplicass ( ListOfString** *paths***,** **) -\> ListOf struct lfc\_filereplicas** *replicas*

**lfc\_getreplicax ( string** *path***,** **string** *guid***,** **string** *se* **) -\> ListOf struct lfc\_filereplicax** *replicas*

**lfc\_lchown ( string** *path***,** **uid\_t** *new\_uid***,** **gid\_t** *new\_gid* **)**

**lfc\_listreplicax ( string** *poolname***,** **string** *server***,** **string** *fs***,** **int** *flags***,** **lfc\_list \* listp** **) -\> struct lfc\_filereplica** *replica*

**lfc\_listrepset ( string** *setname***,** **int** *flags***,** **lfc\_list \****listp* **) -\> struct lfc\_filereplicax** *replica*

**lfc\_lstat ( string** *path* **) -\> struct lfc\_filestat** *statbuf*

**lfc\_mkdir ( string** *path***,** **mode\_t** *mode* **)**

**lfc\_mkdirg ( string** *path***,** **string** *guid***,** **mode\_t** *mode* **)**

**lfc\_modreplica ( string** *sfn***,** **string** *setname***,** **string** *poolname***,** **string** *server* **)**

**lfc\_modreplicax ( string** *sfn***,** **string** *setname***,** **string** *poolname***,** **string** *server***,** **string** *fs***,** **char** *r\_type* **)**

**lfc\_opendir ( string** *path* **) -\> lfc\_DIR** *dir*

**lfc\_opendirg ( string** *path***,** **string** *guid* **) -\> lfc\_DIR** *dir*

**lfc\_opendirxg ( string** *server***,** **string** *path***,** **string** *guid* **) -\> lfc\_DIR** *dir*

**lfc\_ping ( string** *server* **) -\> string** *info*

**lfc\_readdir ( lfc\_DIR** *dirp* **) -\> struct dirent** *dirent*

**lfc\_readdirc ( lfc\_DIR** *dirp* **) -\> struct lfc\_direncomm** *dirent*

**lfc\_readdirg ( lfc\_DIR** *dirp* **) -\> struct lfc\_direnstatg** *dirent*

**lfc\_readdirx ( lfc\_DIR** *dirp* **) -\> struct lfc\_direnstat** *dirent*

**lfc\_readdirxc ( lfc\_DIR** *dirp* **) -\> struct lfc\_direnstatc** *dirent*

**lfc\_readdirxp ( lfc\_DIR** *dirp***,** **string** *pattern***,** **string** *se* **= None ) -\> struct lfc\_direnrep** *dirent*

**lfc\_readdirxr ( lfc\_DIR** *dirp***,** **string** *se* **= None ) -\> struct lfc\_direnrep** *dirent*

**lfc\_readlink ( string** *path* **) -\> string** *link*

**lfc\_registerfiles ( ListOf struct lfc\_filereg** *files* **) -\> ListOfInt** *results*

**lfc\_rename ( string** *oldpath***,** **string** *newpath* **)**

**lfc\_rewinddir ( lfc\_DIR** *dirp* **)**

**lfc\_rmdir ( string** *path* **)**

**lfc\_setacl ( string** *path***,** **ListOf struct lfc\_acl** *acls* **)**

**lfc\_setatime ( string** *path***,** **struct lfc\_fileid** *file\_uniqueid* **)**

**lfc\_setcomment ( string** *path***,** **string** *comment* **)**

**lfc\_setfsize ( string** *path***,** **struct lfc\_fileid** *file\_uniqueid***,** **long** *filesize* **)**

**lfc\_setfsizec ( string** *path***,** **struct lfc\_fileid** *file\_uniqueid***,** **long** *filesize***,** **string** *csumtype***,** **string** *csumvalue* **)**

**lfc\_setfsizeg ( string** *guid***,** **long** *filesize***,** **string** *csumtype***,** **string** *csumvalue* **)**

**lfc\_setptime ( string** *sfn***,** **long** *ptime* **)**

**lfc\_setratime ( string** *sfn* **)**

**lfc\_setrltime ( string** *sfn***,** **long** *ltime* **)**

**lfc\_setrstatus ( string** *sfn***,** **char** *status* **)**

**lfc\_setrtype ( string** *sfn***,** **char** *type* **)**

**lfc\_startsess ( string** *server***,** **string** *comment* **)**

**lfc\_starttrans ( string** *server***,** **string** *comment* **)**

**lfc\_stat ( string** *path* **) -\> struct lfc\_filestat** *statbuf*

**lfc\_statg ( string** *path***,** **string** *guid* **) -\> struct lfc\_filestatg** *statbuf*

**lfc\_statr ( string** *sfn)* **) -\> struct lfc\_filestatg** *statbuf*

**lfc\_symlink ( string** *target***,** **string** *linkname* **)**

**lfc\_umask ( mode\_t** *cmask* **) -\> mode\_t** *oldmask*

**lfc\_undelete ( string** *path* **)**

**lfc\_unlink ( string** *path* **)**

**lfc\_utime ( string** *path***,** **struct utimbuf** *times* **)**

**lfc\_entergrpmap ( gid\_t** *gid***,** **string** *groupname* **)**

**lfc\_enterusrmap ( uid\_t** *uid***,** **string** *username* **)**

**lfc\_getgrpbygid ( gid\_t** *gid* **) -\> string** *group*

**lfc\_getgrpbygids ( ListOfgid\_t** *gids* **) -\> ListOfString** *groups*

**lfc\_getgrpbynam ( string** *groupname* **) -\> gid\_t** *gid*

**lfc\_getgrpmap ( ) -\> ListOf struct lfc\_groupinfo** *infos*

**lfc\_getidmap ( string** *username***,** **ListOfString** *vo\_roles* **) -\> uid\_t** *uid***,**  **ListOf gid\_t** *gids*

**lfc\_getusrbynam ( string** *username* **) -\> uid\_t** *uid*

**lfc\_getusrbyuid ( uid\_t** *uid* **) -\> string** *user*

**lfc\_getusrmap ( ) -\> ListOf struct lfc\_userinfo** *userinfo*

**lfc\_modifygrpmap ( gid\_t** *gid***,** **string** *newname* **)**

**lfc\_modifyusrmap ( uid\_t** *uid***,** **string** *newname* **)**

**lfc\_rmgrpmap ( gid\_t** *gid***,** **string** *groupname* **)**

**lfc\_rmusrmap ( uid\_t** *uid***,** **string** *username* **)**

## STRUCTURES

Sometimes you need to specify a structure as an input argument (eg. *struct lfc\_acl*, *struct lfc\_list*, etc.). For that purpose, the module includes structure constructors, named exactly as the structure.

Thus, to create *struct lfc\_acl* or *struct lfc\_list*, you have to do the following:

```
    lfcacl = lfc.lfc_acl()
    lfclist = lfc.lfc_list()
```

If you want to see how to use it in a real case, please have a look at examples.

## ERRORS

As you can see, no error code is returned. When an error occurs, an exception is raised with a description of the error.

## EXAMPLE

```
#!/usr/bin/python
import sys
import traceback
import lfc2 as lfc
"""
# stat an existing entry in the LFC and print the GUID
"""
name = "/grid/dteam/my.test"
try:
   stat = lfc.lfc_statg(name,"")
except Exception:
   traceback.print_exc()
   sys.exit(1)
guid = stat.guid
print "The GUID for " + name + " is " + guid
```

## EXAMPLE

```
#!/usr/bin/python
import sys
import traceback
import lfc2 as lfc
"""
# setting and retrieving a comment on a file
"""
file = "/grid/dteam/my.test"
comment = "MyComment"
try:
   lfc.lfc_setcomment(file,comment)
   comment = lfc.lfc_getcomment(file)
   print "Read back comment " + comment
except Exception:
   traceback.print_exc()
   sys.exit(1)
```

## EXAMPLE

```
#!/usr/bin/python
"""
# Using the lfc_readdirxr method
"""
import sys
import traceback
import lfc2 as lfc
name = "/grid/dteam/my.test"
try:
   dir = lfc.lfc_opendir(name)
   while 1:
      entry = lfc.lfc_readdirxr(dir)
      if entry == None:
         break
      print entry.d_name
      for i in range(entry.nbreplicas):
         print " ==> %s" % entry.rep[i].sfn
   lfc.lfc_closedir(dir)
except TypeError, x:
   print " ==> None"
except Exception:
   traceback.print_exc()
   sys.exit(1)
```

## EXAMPLE

```
#!/usr/bin/python
import sys
import traceback
import lfc2 as lfc
"""
# Using the lfc_getreplica method
"""
file = "/grid/dteam/my.test"
try:
   list = lfc.lfc_getreplica(file, "", "")
except Exception:
   traceback.print_exc()
   sys.exit(1)
for i in list:
   print i.host
   print i.sfn
```

## EXAMPLE

```
#!/usr/bin/python
import sys
import traceback
import lfc2 as lfc
"""
# Using the lfc_getacl and lfc_setacl methods to add a user ACL
"""
file = "/grid/dteam/my.test"
try:
   acls_list = lfc.lfc_getacl(file)
except Exception:
   traceback.print_exc()
   sys.exit(1)
for i in acls_list:
        print i.a_type
        print i.a_id
        print i.a_perm
# When adding a first ACL for a given user, you also need to add the mask
# When adding the second user ACL, it is not necessary anymore
acl_user = lfc.lfc_acl()
acl_mask = lfc.lfc_acl()
acl_user.a_type = lfc.CNS_ACL_USER
acl_user.a_id = 18701		# user id
acl_user.a_perm = lfc.S_IROTH | lfc.S_IXOTH
acl_mask.a_type = lfc.CNS_ACL_MASK
acl_mask.a_id = 0			# no user id specified
acl_mask.a_perm = lfc.S_IROTH | lfc.S_IXOTH
acls_list.append(acl_user)
acls_list.append(acl_mask)
try:
   lfc.lfc_setacl(file, acls_list)
except Exception:
   traceback.print_exc()
   sys.exit(1)
```

## EXAMPLE

```
#!/usr/bin/python
import sys
import traceback
import lfc2 as lfc
"""
# Using the lfc_getacl and lfc_setacl methods to remove a user ACL
"""
file = "/grid/dteam/my.test"
try:
   acls_list = lfc.lfc_getacl(file)
except Exception:
   traceback.print_exc()
   sys.exit(1)
for i in acls_list:
        print i.a_type
        print i.a_id
        print i.a_perm
del acls_list[1]	# delete a given user ACL from the list of ACLs
try:
   lfc.lfc_setacl(file, acls_list)
except Exception:
   traceback.print_exc()
   sys.exit(1)
```

## EXAMPLE

```
#!/usr/bin/env python
import lfc2thr as lfc
import os
from threading import Thread
class slave(Thread):
   def __init__ (self):
      Thread.__init__(self)
   def run(self):
        ....
        result = lfc.lfc_getreplica("", guid, "")
        ....
        return
if __name__ == '__main__':
    os.environ['LFC_HOST'] = 'my_lfc.cern.ch'
    #	Threaded library initialisation
    lfc.init()
    ....
    #	Start up of threads
    for i in xrange(totalNumberOfSlaves):
        slv = slave(i)
        slv.start()
    ....
```

## EXAMPLE

```
#!/usr/bin/python
import sys
import traceback
import lfc2 as lfc
"""
# Using the lfc_getusrmap method
"""
try:
   list = lfc.lfc_getusrmap()
except Exception:
   traceback.print_exc()
   sys.exit(1)
for i in list:
   print i.userid + " " + i.username
```

## EXAMPLE

```
#!/usr/bin/python
import sys
import traceback
import lfc2 as lfc
"""
# Using the lfc_getgrpmap method
"""
try:
   list = lfc.lfc_getgrpmap()
except Exception:
   traceback.print_exc()
   sys.exit(1)
for i in list:
   print i.gid + " " + i.groupname
```

## EXAMPLE

```
#!/usr/bin/python
import sys
import traceback
import lfc2 as lfc
"""
# Using the lfc_getidmap method
"""
userdn = "user certificate DN"
vo_roles = ["dteam"]
try:
   uid, gids = lfc.lfc_getidmap (userdn, vo_roles)
except Exception:
   traceback.print_exc()
   sys.exit(1)
print "uid=", uid
print "gids=", gids
```

## SEE ALSO

**LFC C interface man pages**
