2014-04-19 02:30:29 -04:00
|
|
|
package net.filebot;
|
2012-10-23 15:05:55 -04:00
|
|
|
|
2017-04-01 11:56:35 -04:00
|
|
|
import static net.filebot.Logging.*;
|
2012-10-23 15:05:55 -04:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2014-01-08 13:43:27 -05:00
|
|
|
import java.nio.file.Path;
|
2012-10-23 15:05:55 -04:00
|
|
|
import java.util.AbstractMap;
|
|
|
|
import java.util.LinkedHashSet;
|
2014-04-25 02:59:18 -04:00
|
|
|
import java.util.List;
|
2012-10-23 15:05:55 -04:00
|
|
|
import java.util.Set;
|
|
|
|
|
2014-04-25 02:59:18 -04:00
|
|
|
import com.sun.jna.Platform;
|
|
|
|
|
2018-12-26 15:35:38 -05:00
|
|
|
import net.filebot.platform.bsd.ExtAttrView;
|
2017-03-06 07:52:04 -05:00
|
|
|
import net.filebot.platform.mac.MacXattrView;
|
2018-12-27 06:51:24 -05:00
|
|
|
import net.filebot.util.DefaultXattrView;
|
|
|
|
import net.filebot.util.PlainFileXattrView;
|
|
|
|
import net.filebot.util.XattrView;
|
2016-08-09 12:49:17 -04:00
|
|
|
|
2012-10-23 15:05:55 -04:00
|
|
|
public class MetaAttributeView extends AbstractMap<String, String> {
|
2014-01-08 13:43:27 -05:00
|
|
|
|
2018-12-27 06:51:24 -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
|
|
|
|
2018-12-27 06:51:24 -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-04-17 11:32:24 -04:00
|
|
|
}
|
2012-10-23 15:05:55 -04:00
|
|
|
}
|
2014-01-08 13:43:27 -05:00
|
|
|
|
2012-10-23 15:05:55 -04:00
|
|
|
@Override
|
|
|
|
public String get(Object key) {
|
2017-04-01 11:56:35 -04:00
|
|
|
return get(key.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
public String get(String key) {
|
2012-10-23 15:05:55 -04:00
|
|
|
try {
|
2018-12-27 06:51:24 -05:00
|
|
|
return fs.read(key);
|
2017-04-01 11:56:35 -04:00
|
|
|
} catch (IOException e) {
|
2018-12-27 06:51:24 -05:00
|
|
|
debug.warning(e::toString);
|
2012-10-23 15:05:55 -04:00
|
|
|
}
|
2014-04-25 02:59:18 -04:00
|
|
|
return null;
|
2012-10-23 15:05:55 -04:00
|
|
|
}
|
2014-01-08 13:43:27 -05:00
|
|
|
|
2012-10-23 15:05:55 -04:00
|
|
|
@Override
|
|
|
|
public String put(String key, String value) {
|
|
|
|
try {
|
2018-12-27 06:51:24 -05:00
|
|
|
if (value == null || value.isEmpty()) {
|
|
|
|
fs.delete(key);
|
|
|
|
} else {
|
|
|
|
fs.write(key, value);
|
2012-10-23 15:05:55 -04:00
|
|
|
}
|
2017-04-01 11:56:35 -04:00
|
|
|
} catch (IOException e) {
|
2014-07-17 12:28:52 -04:00
|
|
|
throw new RuntimeException(e);
|
2012-10-23 15:05:55 -04:00
|
|
|
}
|
2014-04-25 02:59:18 -04:00
|
|
|
|
2012-10-23 15:05:55 -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 {
|
2018-12-27 06:51:24 -05:00
|
|
|
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 {
|
2018-12-27 06:51:24 -05:00
|
|
|
return fs.list();
|
2014-04-25 02:59:18 -04:00
|
|
|
}
|
|
|
|
|
2012-10-23 15:05:55 -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()) {
|
2012-10-23 15:05:55 -04:00
|
|
|
entries.add(new AttributeEntry(name));
|
|
|
|
}
|
|
|
|
return entries;
|
2017-04-01 11:56:35 -04:00
|
|
|
} catch (IOException e) {
|
2014-07-17 12:28:52 -04:00
|
|
|
throw new RuntimeException(e);
|
2012-10-23 15:05:55 -04:00
|
|
|
}
|
|
|
|
}
|
2014-01-08 13:43:27 -05:00
|
|
|
|
2012-10-23 15:05:55 -04:00
|
|
|
private class AttributeEntry implements Entry<String, String> {
|
2014-01-08 13:43:27 -05:00
|
|
|
|
2012-10-23 15:05:55 -04:00
|
|
|
private final String name;
|
2014-01-08 13:43:27 -05:00
|
|
|
|
2012-10-23 15:05:55 -04:00
|
|
|
public AttributeEntry(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
2014-01-08 13:43:27 -05:00
|
|
|
|
2012-10-23 15:05:55 -04:00
|
|
|
@Override
|
|
|
|
public String getKey() {
|
|
|
|
return name;
|
|
|
|
}
|
2014-01-08 13:43:27 -05:00
|
|
|
|
2012-10-23 15:05:55 -04:00
|
|
|
@Override
|
|
|
|
public String getValue() {
|
|
|
|
return get(name);
|
|
|
|
}
|
2014-01-08 13:43:27 -05:00
|
|
|
|
2012-10-23 15:05:55 -04:00
|
|
|
@Override
|
|
|
|
public String setValue(String value) {
|
|
|
|
return put(name, value);
|
|
|
|
}
|
2014-01-08 13:43:27 -05:00
|
|
|
|
2012-10-23 15:05:55 -04:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return getKey() + "=" + getValue();
|
|
|
|
}
|
|
|
|
}
|
2014-01-08 13:43:27 -05:00
|
|
|
|
2012-10-23 15:05:55 -04:00
|
|
|
}
|