NAME File::Lockfile::Emacs - Create/check/delete Emacs-style lockfiles VERSION This document describes version 0.001 of File::Lockfile::Emacs (from Perl distribution File-Lockfile-Emacs), released on 2025-03-11. SYNOPSIS use File::Lockfile::Emacs qw( emacs_lockfile_lock emacs_lockfile_get emacs_lockfile_locked emacs_lockfile_unlock ); # create an Emacs-style lockfile; return 304 status if the same process already # acquires the lock, returns 409 if other process is holding the lock. my $res = emacs_lockfile_lock(target_file => "target.txt"); if ($res->[0] == 200) { say "Locked" } elsif ($res->[0] == 304) { say "Already locked" } else { die "Can't lock target.txt: $res->[0] - $res->[1]" } # get information on the emacs lockfile of a file my $res = emacs_lockfile_get(target_file => "../target.txt"); if ($res->[0] != 200) { die "Can't check lockfile: $res->[0] - $res->[1]" } elsif ($res->[0] != 404) { die "Lockfile does not exist" } elsif ($res->[0] != 200) { die "Can't get: $res->[0] - $res->[1]" } say "user = $res->[2]{user}\nhost = $res->[2]{host}\npid = $res->[2]{pid}"; say "boot = $res->[2]{boot}" if $res->[2]{boot}; # check if there is an emacs lockfile to a file. return 200 status and boolean # value. my $res = check_emacs_locked(target_file => "../target.txt"); if ($res->[0] != 200) { die "Can't check lockfile: $res->[0] - $res->[1]" } say "File is ".($res->[2] ? "locked" : "NOT locked"). " by ".($res->[2]{"func.pid"} == $$ ? "us" : "pid $res->[3]{'func.pid'}"); # unlock a file. normally will only unlock if we (the same process) holds the # lock. my $res = emacs_lockfile_unlock(target_file => "../target.txt"); if ($res->[0] == 304) { say "File was not locked" } elsif ($res->[0] == 409) { say "Won't lock, file is not locked by us" } elsif ($res->[0] != 200) { die "Can't unlock: $res->[0] - $res->[1]" } else { say "Unlocked" } DESCRIPTION From Emacs documentation: When two users edit the same file at the same time, they are likely to interfere with each other. Emacs tries to prevent this situation from arising by recording a file lock when a file is being modified. Emacs can then detect the first attempt to modify a buffer visiting a file that is locked by another Emacs job, and ask the user what to do. The file lock is really a file, a symbolic link with a special name, stored in the same directory as the file you are editing. The name is constructed by prepending .# to the file name of the buffer. The target of the symbolic link will be of the form `user@host.pid:boot`, where `user` is replaced with the current username (from `user-login-name`), `host` with the name of the host where Emacs is running (from `system-name`), `pid` with Emacs’s process id, and `boot` with the time since the last reboot. `:boot` is omitted if the boot time is unavailable. (On file systems that do not support symbolic links, a regular file is used instead, with contents of the form `user@host.pid:boot`.) FUNCTIONS emacs_lockfile_get Usage: emacs_lockfile_get(%args) -> [$status_code, $reason, $payload, \%result_meta] Get information on an Emacs lockfile of a target file. This function is not exported by default, but exportable. Arguments ('*' denotes required arguments): * target_file* => *filename* Target file. Returns an enveloped result (an array). First element ($status_code) is an integer containing HTTP-like status code (200 means OK, 4xx caller error, 5xx function error). Second element ($reason) is a string containing error message, or something like "OK" if status is 200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth element (%result_meta) is called result metadata and is optional, a hash that contains extra information, much like how HTTP response headers provide additional metadata. Return value: (any) emacs_lockfile_lock Usage: emacs_lockfile_lock(%args) -> [$status_code, $reason, $payload, \%result_meta] Lock a file using Emacs-style lockfile. Will return 412 if target file does not exist (unless "force" option is set to true, in which case we proceed to locking anyway). Will return 304 if target file is already locked using Emacs-style lockfile by the same process as us. Will return 409 if target file is already locked using Emacs-style lockfile by another process (unless when "force" option is set to true, in which case will take over the lock). Will return 500 if there's an error in reading the lockfile. Will return 412 if we are not the same process that locks the file (unless "force" option is set to true, in which case we proceed to unlocking anyway). Will return 500 if there's an error in removing the lockfile. Will return 200 if everything goes ok. This function is not exported by default, but exportable. Arguments ('*' denotes required arguments): * force => *bool* (No description) * target_file* => *filename* Target file. Returns an enveloped result (an array). First element ($status_code) is an integer containing HTTP-like status code (200 means OK, 4xx caller error, 5xx function error). Second element ($reason) is a string containing error message, or something like "OK" if status is 200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth element (%result_meta) is called result metadata and is optional, a hash that contains extra information, much like how HTTP response headers provide additional metadata. Return value: (any) emacs_lockfile_locked Usage: emacs_lockfile_locked(%args) -> [$status_code, $reason, $payload, \%result_meta] Check whether a target file is locked using Emacs-style lockfile. This function is not exported by default, but exportable. Arguments ('*' denotes required arguments): * by_us => *bool* If set to true, only return true when lockfile is created by us; if false, then will only return true when lockfile is created by others. * target_file* => *filename* Target file. Returns an enveloped result (an array). First element ($status_code) is an integer containing HTTP-like status code (200 means OK, 4xx caller error, 5xx function error). Second element ($reason) is a string containing error message, or something like "OK" if status is 200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth element (%result_meta) is called result metadata and is optional, a hash that contains extra information, much like how HTTP response headers provide additional metadata. Return value: (any) emacs_lockfile_unlock Usage: emacs_lockfile_unlock(%args) -> [$status_code, $reason, $payload, \%result_meta] Unlock a file locked with Emacs-style lockfile. Will return 412 if target file does not exist (unless "force" option is set to true, in which case we proceed to unlocking anyway). Will return 304 if target file is not currently locked using Emacs-style lockfile. Will return 500 if there's an error in reading the lockfile. Will return 412 if we are not the same process that locks the file (unless "force" option is set to true, in which case we proceed to unlocking anyway). Will return 500 if there's an error in removing the lockfile. Will return 200 if everything goes ok. This function is not exported by default, but exportable. Arguments ('*' denotes required arguments): * force => *bool* (No description) * target_file* => *filename* Target file. Returns an enveloped result (an array). First element ($status_code) is an integer containing HTTP-like status code (200 means OK, 4xx caller error, 5xx function error). Second element ($reason) is a string containing error message, or something like "OK" if status is 200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth element (%result_meta) is called result metadata and is optional, a hash that contains extra information, much like how HTTP response headers provide additional metadata. Return value: (any) HOMEPAGE Please visit the project's homepage at . SOURCE Source repository is at . SEE ALSO AUTHOR perlancar CONTRIBUTING To contribute, you can send patches by email/via RT, or send pull requests on GitHub. Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via: % prove -l If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me. COPYRIGHT AND LICENSE This software is copyright (c) 2025 by perlancar . This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. BUGS Please report any bugs or feature requests on the bugtracker website When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.