make file writable if necessary

This commit is contained in:
Reinhard Pointner 2017-04-02 00:54:34 +08:00
parent 2e221d98dc
commit daef4da23b
1 changed files with 17 additions and 5 deletions

View File

@ -67,15 +67,27 @@ public class XattrMetaInfo {
} }
try { try {
return cache.computeIfAbsent(file, element -> compute.apply(xattr(file))); return cache.computeIfAbsent(file, element -> compute.apply(xattr(file))); // read only
} catch (Throwable e) { } catch (Throwable e) {
debug.warning(cause("Failed to read xattr", e)); debug.warning(cause("Failed to read xattr", e));
} }
return null; return null;
} }
private MetaAttributes xattr(File file) throws Exception { private File writable(File f) throws Exception {
return new MetaAttributes(file); // make file writable if necessary
if (!f.canWrite()) {
if (f.setWritable(true)) {
debug.finest("Grant write permissions: " + f);
} else {
debug.warning("Failed to grant write permissions: " + f);
}
}
return f;
}
private MetaAttributes xattr(File f) throws Exception {
return new MetaAttributes(f);
} }
public synchronized void setMetaInfo(File file, Object model, String original) { public synchronized void setMetaInfo(File file, Object model, String original) {
@ -85,7 +97,7 @@ public class XattrMetaInfo {
} }
// set creation date to episode / movie release date // set creation date to episode / movie release date
Resource<MetaAttributes> xattr = Resource.lazy(() -> xattr(file)); Resource<MetaAttributes> xattr = Resource.lazy(() -> xattr(writable(file)));
if (useCreationDate) { if (useCreationDate) {
try { try {
@ -127,7 +139,7 @@ public class XattrMetaInfo {
if (useExtendedFileAttributes) { if (useExtendedFileAttributes) {
try { try {
xattr(file).clear(); xattr(writable(file)).clear();
} catch (Throwable e) { } catch (Throwable e) {
debug.warning(cause("Failed to clear xattr", e)); debug.warning(cause("Failed to clear xattr", e));
} }