Locks

A Lock can be used by your custom-developed applications to implement file locking and concurrency features. These locks are advisory, meaning that while a lock can be created, it does not prevent other API requests from being processed concurrently. You are responsible for checking locks prior to accessing a file.

The lock feature is designed to emulate the locking functionality provided by WebDAV. For a deeper understanding of how the lock mechanism works, refer to the WebDAV specification, which outlines how these endpoints function.

Files.com's WebDAV offering and desktop app leverage this locking API to manage concurrent file operations, ensuring consistency when multiple users or systems interact with the same files. It is not used within the Files.com web interface.

List Locks by Path

SDK Method

lock.ListFor()

Return Object

[]*Lock

Authorization Requirement

Available to all authenticated keys or sessions.

LockListForParams Fields

FieldDescription
Path
string
Required
Path to operate on.
IncludeChildren
boolean
Include locks from children objects?

Additional Arguments

Example Request

import (
    "fmt"
    "errors"

    files_sdk "github.com/Files-com/files-sdk-go/v3"
    "github.com/Files-com/files-sdk-go/v3/lock"
)

lockIterator, err := lock.ListFor(files_sdk.LockListForParams{Path: "path"})
if err != nil {
    var respErr files_sdk.ResponseError
    if errors.As(err, &respErr) {
        fmt.Println("Response Error Occurred (" + respErr.Type + "): " + respErr.ErrorMessage)
    } else {
        fmt.Printf("Unexpected Error: %s\n", err.Error())
    }
}

for lockIterator.Next() {
    lock := lockIterator.lock()
}
err = lockIterator.Err()
if err != nil {
    var respErr files_sdk.ResponseError
    if errors.As(err, &respErr) {
        fmt.Println("Response Error Occurred (" + respErr.Type + "): " + respErr.ErrorMessage)
    } else {
        fmt.Printf("Unexpected Error: %s\n", err.Error())
    }
}

Create Lock

SDK Method

lock.Create()

Return Object

Lock

Authorization Requirement

Available to all authenticated keys or sessions.

LockCreateParams Fields

FieldDescription
Path
string
Required
Path
AllowAccessByAnyUser
boolean
Can lock be modified by users other than its creator?
Exclusive
boolean
Is lock exclusive?
Recursive
boolean
Does lock apply to subfolders?
Timeout
int64
Lock timeout in seconds

Example Request

import (
    "fmt"
    "errors"

    files_sdk "github.com/Files-com/files-sdk-go/v3"
    "github.com/Files-com/files-sdk-go/v3/lock"
)

lock, err := lock.Create(files_sdk.LockCreateParams{Path: "locked_file"})
if err != nil {
    var respErr files_sdk.ResponseError
    if errors.As(err, &respErr) {
        fmt.Println("Response Error Occurred (" + respErr.Type + "): " + respErr.ErrorMessage)
    } else {
        fmt.Printf("Unexpected Error: %s\n", err.Error())
    }
}

Delete Lock

SDK Method

lock.Delete()

Return Object

No return value.

Authorization Requirement

Available to all authenticated keys or sessions.

LockDeleteParams Fields

FieldDescription
Path
string
Required
Path
Token
string
Required
Lock token

Example Request

import (
    "fmt"
    "errors"

    files_sdk "github.com/Files-com/files-sdk-go/v3"
    "github.com/Files-com/files-sdk-go/v3/lock"
)

err := lock.Delete(files_sdk.LockDeleteParams{
  Path: "locked_file",
  Token: "token"
})
if err != nil {
    var respErr files_sdk.ResponseError
    if errors.As(err, &respErr) {
        fmt.Println("Response Error Occurred (" + respErr.Type + "): " + respErr.ErrorMessage)
    } else {
        fmt.Printf("Unexpected Error: %s\n", err.Error())
    }
}

The Lock Object

Some of the methods above return a Lock object. The attributes of this object are listed below.

AttributeDescription
Path
string
Path. This must be slash-delimited, but it must neither start nor end with a slash. Maximum of 5000 characters.
Timeout
int64
Lock timeout in seconds
Depth
string
Recursive
boolean
Does lock apply to subfolders?
Owner
string
Owner of the lock. This can be any arbitrary string.
Scope
string
Exclusive
boolean
Is lock exclusive?
Token
string
Lock token. Use to release lock.
Type
string
AllowAccessByAnyUser
boolean
Can lock be modified by users other than its creator?
UserId
int64
Lock creator user ID
Username
string
Lock creator username