Skip to content
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
40 changes: 39 additions & 1 deletion src/freenet/clients/http/FirstTimeWizardNewToadlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import freenet.clients.http.wizardsteps.DATASTORE_SIZE;
import freenet.config.Config;
import freenet.config.ConfigException;
import freenet.config.InvalidConfigValueException;
import freenet.config.NodeNeedRestartException;
import freenet.config.Option;
import freenet.l10n.NodeL10n;
import freenet.node.*;
Expand All @@ -21,6 +23,7 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

import static java.util.concurrent.TimeUnit.SECONDS;

Expand All @@ -33,6 +36,10 @@ public class FirstTimeWizardNewToadlet extends WebTemplateToadlet {
public static final String TOADLET_URL = "/wiz/";

private static final long MIN_STORAGE_LIMIT = Node.MIN_STORE_SIZE * 5 / 4; // min store size + 10% for client cache + 10% for slashdot cache
private static final String CLIENT_CACHE_TYPE_KEY = "clientCacheType";
private static final String STORE_TYPE_KEY = "storeType";
private static final String STORE_TYPE_VALUE_RAM = "ram";
private static final String STORE_TYPE_SALT_HASH = "salt-hash";

private final NodeClientCore core;

Expand Down Expand Up @@ -117,6 +124,8 @@ private class FormModel {

private String bandwidthMonthlyLimit = "500";

private String donateDiskSpace = "checked";

private String storageLimit;

private final String minStorageLimit = String.format(Locale.ENGLISH, "%.2f", (float) MIN_STORAGE_LIMIT / DatastoreUtil.oneGiB);
Expand All @@ -132,6 +141,13 @@ private class FormModel {
private Map<String, String> errors = new HashMap<>();

FormModel() {
String storeType = config.get("node").getString(STORE_TYPE_KEY);
String cacheType = config.get("node").getString(CLIENT_CACHE_TYPE_KEY);
donateDiskSpace = (
Objects.equals(storeType, STORE_TYPE_VALUE_RAM)
&& Objects.equals(cacheType, STORE_TYPE_VALUE_RAM))
? ""
: "checked" ;
float storage = 100;
@SuppressWarnings("unchecked")
Option<Long> sizeOption = (Option<Long>) config.get("node").getOption("storeSize");
Expand Down Expand Up @@ -168,6 +184,7 @@ private class FormModel {
downloadLimit = request.getPartAsStringFailsafe("downLimit", 100);
uploadLimit = request.getPartAsStringFailsafe("upLimit", 100);
bandwidthMonthlyLimit = request.getPartAsStringFailsafe("monthlyLimit", 100);
donateDiskSpace = request.getPartAsStringFailsafe("donateDiskSpace", 20);
storageLimit = request.getPartAsStringFailsafe("storage", 100);
setPassword = request.getPartAsStringFailsafe("setPassword", 20);
password = request.getPartAsStringFailsafe("password", SecurityLevelsToadlet.MAX_PASSWORD_LENGTH + 1);
Expand Down Expand Up @@ -273,6 +290,7 @@ private Map<String, Object> toModel() {
model.put("uploadLimit", uploadLimit);
model.put("bandwidthMonthlyLimit", bandwidthMonthlyLimit);
model.put("minBandwidthMonthlyLimit", String.format("%.2f", BandwidthLimit.minMonthlyLimit));
model.put("donateDiskSpace", donateDiskSpace);
model.put("storageLimit", storageLimit);
model.put("minStorageLimit", minStorageLimit);
if (!isPasswordAlreadySet) {
Expand Down Expand Up @@ -318,7 +336,27 @@ private void save() {
Logger.error(this, "Should not happen, please report! " + e, e);
}

DATASTORE_SIZE.setDatastoreSize(storageLimit + "GiB", config, this);
try {
if (donateDiskSpace.isEmpty()) {
// set a fixed size: large RAM stores aren't useful. Storage limit must be hidden if donate disk space is disabled.
DATASTORE_SIZE.setDatastoreSize( "128MiB", false, config, this);
try {
config.get("node").set(STORE_TYPE_KEY, STORE_TYPE_VALUE_RAM);
} catch (NodeNeedRestartException e) {
// ignore: can only happen when switching from ram store to salt-hash.
}
try {
config.get("node").set(CLIENT_CACHE_TYPE_KEY, STORE_TYPE_VALUE_RAM);
} catch (NodeNeedRestartException e) {
// ignore: can only happen when switching from ram store to salt-hash.
}
} else {
// sets the store to salt-hash internally
DATASTORE_SIZE.setDatastoreSize(storageLimit + "GiB", config, this);
}
} catch (InvalidConfigValueException e) {
Logger.warning(this, "Should not happen, please report! " + e, e);
}

if (!isPasswordAlreadySet) {
try {
Expand Down
3 changes: 2 additions & 1 deletion src/freenet/clients/http/staticfiles/js/firsttimewizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ slideToggler = (function() {
instance = (new Date).getTime() - init;
step = start + disp * instance / time;

if (instance <= time) {
if (time > 1 && instance <= time) {
el.style.height = step + 'px';
} else {
el.style.cssText = "display: " + (end === 0 ? 'none' : 'block');
Expand Down Expand Up @@ -166,4 +166,5 @@ function initMonthlyLimit() {

checkChange('knowSomeone', data = {'checked': 'checkDarknet', 'unchecked': 'noDarknet'});
checkChange('setPassword', data = {'checked': 'givePassword', 'unchecked': ''});
checkChange('donateDiskSpace', data = {'checked': 'chooseStoreSize', 'unchecked': ''});
initMonthlyLimit();
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ var mobileMenu = function() {
}

function attach() {
navbarDomElement.appendChild(hamburgerContent);
hamburgerDomElement = document.getElementById(hamburgerId);
customAddEventListener(hamburgerDomElement, 'click', toggleCssClasses);
if (navbarDomElement) {
navbarDomElement.appendChild(hamburgerContent);
hamburgerDomElement = document.getElementById(hamburgerId);
customAddEventListener(hamburgerDomElement, 'click', toggleCssClasses);
}
}

function detach() {
Expand Down Expand Up @@ -99,7 +101,9 @@ var toggleInnerMenu = function() {
var notSelectedLinkList = document.querySelectorAll('#navlist > .navlist-not-selected > a');

// add active class for selected element after load page
selectedList[0].className += " active";
if (selectedList.length > 0) {
selectedList[0].className += " active";
}

// function toggle (add and remove active class)
function toggle(selectedItem) {
Expand Down
4 changes: 4 additions & 0 deletions src/freenet/clients/http/templates/first-time-wizard.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ <h4>{{ l10n("bandwidthCommonInternetConnectionSpeeds") }}</h4>
</fieldset>
<fieldset>
<legend>{{ l10n("storage") }}</legend>
<input id="donateDiskSpace" name="donateDiskSpace" type="checkbox" {{ donateDiskSpace }}>
<label for="donateDiskSpace">{{ l10n("donateDiskSpace") }}</label>
<p>
<div id="chooseStoreSize" hidden="hidden">
{% if errors.storageLimitError is not null %}
<div class="error">{{ errors.storageLimitError }}</div>
{% endif %}
Expand All @@ -90,6 +93,7 @@ <h4>{{ l10n("bandwidthCommonInternetConnectionSpeeds") }}</h4>
<!-- TODO: Possibly attach this number with a slider of percentage of free space. -->
<input id="storage" value="{{ storageLimit }}" name="storage" type="number" min="{{ minStorageLimit }}" max="999999999" step=".01">
{{ l10n("storageLimitDescription") }}
</div>
</p>
{% if setPassword is not null %}
<input id="setPassword" name="setPassword" type="checkbox" {{ setPassword }}>
Expand Down
10 changes: 7 additions & 3 deletions src/freenet/clients/http/wizardsteps/DATASTORE_SIZE.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,13 @@ public static void setDatastoreSize(String selectedStoreSize, Config config, Obj
_setDatastoreSize(selectedStoreSize, true, config, callback);
}

public static void setDatastoreSize(String selectedStoreSize, boolean chooseSaltHash, Config config, Object callback) {
_setDatastoreSize(selectedStoreSize, chooseSaltHash, config, callback);
}

private static void _setDatastoreSize(
String selectedStoreSize,
boolean firsttime,
boolean chooseSaltHash,
Config config,
Object callback) {
try {
Expand Down Expand Up @@ -154,10 +158,10 @@ private static void _setDatastoreSize(

System.out.println("Setting datastore size to "+Fields.longToString(storeSize, true));
config.get("node").set("storeSize", Fields.longToString(storeSize, true));
if (firsttime) config.get("node").set("storeType", "salt-hash");
if (chooseSaltHash) config.get("node").set("storeType", "salt-hash");
System.out.println("Setting client cache size to "+Fields.longToString(clientCacheSize, true));
config.get("node").set("clientCacheSize", Fields.longToString(clientCacheSize, true));
if (firsttime) config.get("node").set("clientCacheType", "salt-hash");
if (chooseSaltHash) config.get("node").set("clientCacheType", "salt-hash");
System.out.println("Setting slashdot/ULPR/recent requests cache size to "+Fields.longToString(slashdotCacheSize, true));
config.get("node").set("slashdotCacheSize", Fields.longToString(slashdotCacheSize, true));

Expand Down
1 change: 1 addition & 0 deletions src/freenet/l10n/freenet.l10n.en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ FirstTimeWizardToadlet.bandwidthMonthlyNetTransfer=Monthly Net Transfer
FirstTimeWizardToadlet.bandwidthCouldNotParse=Unable to parse limit "${limit}", please try again.
FirstTimeWizardToadlet.bandwidthErrorSettingTitle=Error Setting Bandwidth Limit
FirstTimeWizardToadlet.storage=Storage
FirstTimeWizardToadlet.donateDiskSpace=Donate disk space to support the network?
FirstTimeWizardToadlet.storageLimitDescription=GiB of storage space to Freenet.
FirstTimeWizardToadlet.requirePassword=I want Freenet to require a password when it starts.
FirstTimeWizardToadlet.password=Password:
Expand Down
31 changes: 31 additions & 0 deletions src/freenet/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,21 @@ private <T extends StorableBlock> void migrateOldStore(StoreCallback<T> old, Sto

public <T extends StorableBlock> void closeOldStore(StoreCallback<T> old) {
FreenetStore<T> store = old.getStore();
_closeOldStore(store);
}

private static <T extends StorableBlock> void _closeOldStore(FreenetStore<T> store) {
if(store instanceof SaltedHashFreenetStore) {
SaltedHashFreenetStore<T> saltstore = (SaltedHashFreenetStore<T>) store;
saltstore.close();
saltstore.destruct();
} else if (store instanceof CachingFreenetStore) {
store.close();
FreenetStore<T> underlyingStore = store.getUnderlyingStore();
if (underlyingStore instanceof SaltedHashFreenetStore) {
SaltedHashFreenetStore<T> saltStore = (SaltedHashFreenetStore<T>) underlyingStore;
saltStore.destruct();
}
}
}

Expand Down Expand Up @@ -359,12 +370,23 @@ public void set(String val) throws InvalidConfigValueException, NodeNeedRestartE
}
} else {
synchronized(Node.this) {
closeAllStores();
storeType = val;
}
throw new NodeNeedRestartException("Store type cannot be changed on the fly");
}
}

private void closeAllStores() {
// close the old stores so switching back to salt-hash doesn't run into locking trouble
closeOldStore(sskDatastore);
closeOldStore(pubKeyDatastore);
closeOldStore(chkDatastore);
closeOldStore(sskDatacache);
closeOldStore(pubKeyDatacache);
closeOldStore(chkDatacache);
}

@Override
public String[] getPossibleValues() {
return new String[] { "salt-hash", "ram" };
Expand Down Expand Up @@ -416,8 +438,10 @@ public void set(String val) throws InvalidConfigValueException, NodeNeedRestartE
throw new InvalidConfigValueException("Unable to create new store: "+e);
}
} else if(val.equals("ram")) {
closeAllClientCaches();
initRAMClientCacheFS();
} else /*if(val.equals("none")) */{
closeAllClientCaches();
initNoClientCacheFS();
}

Expand All @@ -427,6 +451,13 @@ public void set(String val) throws InvalidConfigValueException, NodeNeedRestartE
}
}

private void closeAllClientCaches() {
// close the old client caches so switching back to salt-hash doesn't run into locking trouble
closeOldStore(sskClientcache);
closeOldStore(pubKeyClientcache);
closeOldStore(chkClientcache);
}

@Override
public String[] getPossibleValues() {
return new String[] { "salt-hash", "ram", "none" };
Expand Down
14 changes: 12 additions & 2 deletions src/freenet/store/saltedhash/SaltedHashFreenetStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.Arrays;
import java.util.Deque;
Expand Down Expand Up @@ -571,10 +572,13 @@ private boolean onWrite() {
private File metaFile;
private RandomAccessFile metaRAF;
private FileChannel metaFC;
private FileLock metaFCLock;

// header+data file
private File hdFile;
private RandomAccessFile hdRAF;
private FileChannel hdFC;
private FileLock hdFCLock;
private final int hdPadding;

/**
Expand Down Expand Up @@ -842,15 +846,15 @@ private boolean openStoreFiles(File baseDir, String name) throws IOException {
metaFC = metaRAF.getChannel();

try {
metaFC.lock();
metaFCLock = metaFC.lock();
} catch(OverlappingFileLockException ex) {
throw new Error("Could not aquire lock for file " + baseDir.toPath().resolve(name + ".metadata"), ex);
}

hdRAF = new RandomAccessFile(hdFile, "rw");
hdFC = hdRAF.getChannel();
try {
hdFC.lock();
hdFCLock = hdFC.lock();
} catch(OverlappingFileLockException ex) {
throw new Error("Could not aquire lock for file " + baseDir.toPath().resolve(name + ".hd"), ex);
}
Expand Down Expand Up @@ -1040,12 +1044,18 @@ private void flushAndClose(boolean abort) {
Logger.normal(this, "Flush and closing this store: " + name);
try {
metaFC.force(true);
if (metaFCLock.isValid()) {
metaFCLock.release();
}
metaFC.close();
} catch (Exception e) {
Logger.error(this, "error flusing store", e);
}
try {
hdFC.force(true);
if (hdFCLock.isValid()) {
hdFCLock.release();
}
hdFC.close();
} catch (Exception e) {
Logger.error(this, "error flusing store", e);
Expand Down