filebot/source/net/filebot/MetaAttributeView.java

131 lines
2.7 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot;
2017-04-01 11:56:35 -04:00
import static net.filebot.Logging.*;
import java.io.File;
import java.io.IOException;
2014-01-08 13:43:27 -05:00
import java.nio.file.Path;
import java.util.AbstractMap;
import java.util.LinkedHashSet;
2014-04-25 02:59:18 -04:00
import java.util.List;
import java.util.Set;
2014-04-25 02:59:18 -04:00
import com.sun.jna.Platform;
import net.filebot.platform.bsd.ExtAttrView;
import net.filebot.platform.mac.MacXattrView;
import net.filebot.util.DefaultXattrView;
import net.filebot.util.PlainFileXattrView;
import net.filebot.util.XattrView;
2016-08-09 12:49:17 -04:00
public class MetaAttributeView extends AbstractMap<String, String> {
2014-01-08 13:43:27 -05:00
private final boolean FORCE_XATTR_STORE = System.getProperty("net.filebot.xattr.store") != null;
private XattrView fs;
2014-04-25 02:59:18 -04:00
2014-01-08 13:43:27 -05:00
public MetaAttributeView(File file) throws IOException {
2017-04-01 11:56:35 -04:00
// resolve symlinks
Path path = file.toPath().toRealPath();
2014-01-08 13:43:27 -05:00
if (FORCE_XATTR_STORE) {
fs = new PlainFileXattrView(path);
} else if (Platform.isWindows() || Platform.isLinux()) {
fs = new DefaultXattrView(path);
} else if (Platform.isMac()) {
fs = new MacXattrView(path);
} else if (Platform.isFreeBSD() || Platform.isOpenBSD() || Platform.isNetBSD()) {
fs = new ExtAttrView(path);
} else {
fs = new DefaultXattrView(path);
}
}
2014-01-08 13:43:27 -05:00
@Override
public String get(Object key) {
2017-04-01 11:56:35 -04:00
return get(key.toString());
}
public String get(String key) {
try {
return fs.read(key);
2017-04-01 11:56:35 -04:00
} catch (IOException e) {
debug.warning(e::toString);
}
2014-04-25 02:59:18 -04:00
return null;
}
2014-01-08 13:43:27 -05:00
@Override
public String put(String key, String value) {
try {
if (value == null || value.isEmpty()) {
fs.delete(key);
} else {
fs.write(key, value);
}
2017-04-01 11:56:35 -04:00
} catch (IOException e) {
throw new RuntimeException(e);
}
2014-04-25 02:59:18 -04:00
return null; // since we don't know the old value
}
2014-01-08 13:43:27 -05:00
2017-04-01 12:33:40 -04:00
@Override
public void clear() {
try {
for (String key : fs.list()) {
fs.delete(key);
2017-04-01 12:33:40 -04:00
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
2014-04-25 02:59:18 -04:00
public List<String> list() throws IOException {
return fs.list();
2014-04-25 02:59:18 -04:00
}
@Override
public Set<Entry<String, String>> entrySet() {
try {
Set<Entry<String, String>> entries = new LinkedHashSet<Entry<String, String>>();
2017-04-01 12:33:40 -04:00
for (String name : list()) {
entries.add(new AttributeEntry(name));
}
return entries;
2017-04-01 11:56:35 -04:00
} catch (IOException e) {
throw new RuntimeException(e);
}
}
2014-01-08 13:43:27 -05:00
private class AttributeEntry implements Entry<String, String> {
2014-01-08 13:43:27 -05:00
private final String name;
2014-01-08 13:43:27 -05:00
public AttributeEntry(String name) {
this.name = name;
}
2014-01-08 13:43:27 -05:00
@Override
public String getKey() {
return name;
}
2014-01-08 13:43:27 -05:00
@Override
public String getValue() {
return get(name);
}
2014-01-08 13:43:27 -05:00
@Override
public String setValue(String value) {
return put(name, value);
}
2014-01-08 13:43:27 -05:00
@Override
public String toString() {
return getKey() + "=" + getValue();
}
}
2014-01-08 13:43:27 -05:00
}