Skip to content

use xattr to trace file instead of inode #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ source "https://rubygems.org"
group :test do
gem 'rspec'
gem 'stud', "~> 0.0.18"
gem 'uuid', "~> 2.3.8"
gem 'ffi-xattr', "~> 0.1.2"
end
21 changes: 21 additions & 0 deletions lib/filewatch/watch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
require "filewatch/winhelper"
end
require "ffi-xattr"
require "uuid"

module FileWatch
class Watch
Expand All @@ -19,6 +21,9 @@ def initialize(opts={})
@watching = []
@exclude = []
@files = Hash.new { |h, k| h[k] = Hash.new }

@xattr = opts[:xattr] || 'uuid'
@uuid = UUID.new if @xattr
end # def initialize

public
Expand Down Expand Up @@ -46,6 +51,22 @@ def inode(path,stat)
if @iswindows
fileId = Winhelper.GetWindowsUniqueFileIdentifier(path)
inode = [fileId, stat.dev_major, stat.dev_minor]
elsif @xattr
begin
xattr = Xattr.new(path)

fileId = xattr[@xattr]

if !fileId
fileId = @uuid.generate

xattr[@xattr] = fileId
end

inode = [fileId, stat.dev_major, stat.dev_minor]
rescue
inode = [stat.ino.to_s, stat.dev_major, stat.dev_minor]
end
else
inode = [stat.ino.to_s, stat.dev_major, stat.dev_minor]
end
Expand Down
9 changes: 9 additions & 0 deletions spec/tail_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'filewatch/tail'
require 'stud/temporary'
require 'ffi-xattr'

describe FileWatch::Tail do

Expand All @@ -21,6 +22,14 @@
it "reads new lines off the file" do
expect { |b| subject.subscribe(&b) }.to yield_successive_args([file_path, "line1"], [file_path, "line2"])
end

it "file contains xattr 'uuid'" do
subject.subscribe {|_,_| }
stat = File::Stat.new(file_path)
sincedb_id = subject.sincedb_record_uid(file_path,stat)[0]

expect(Xattr.new(file_path)['uuid']).to eq(sincedb_id)
end
end

context "when watching a file" do
Expand Down