Re: Using JS to write to file under /sys



2012/7/28 Alessandro Crismani <alessandro crismani gmail com>:
> Il 27/07/2012 01:04, Alessandro Crismani ha scritto:
>
>> but I get an error that sounds like:
>>
>> root at alessandro-sammy in /data/Documenti/Linux/SammySettings
>> # LC_ALL=C gjs SammySettings.js
>>      JS ERROR: !!!   Exception was: Error: Error invoking
>> GLib.file_set_contents: Failed to create file
>> '/sys/devices/platform/samsung/battery_life_extender.ZTDFIW': No such
>> file or directory
>>      JS ERROR: !!!     lineNumber = '0'
>>      JS ERROR: !!!     fileName = '"gjs_throw"'
>>      JS ERROR: !!!     stack = '"("Error invoking
>> GLib.file_set_contents: Failed to create file
>> '/sys/devices/platform/samsung/battery_life_extender.ZTDFIW': No such
>> file or directory")@gjs_throw:0
>> ([object _private_Gtk_Switch],[object
>> _private_GObject_ParamSpec])@SammySettings.js:108
>> wrapper([object _private_Gtk_Switch],[object
>> _private_GObject_ParamSpec])@/usr/share/gjs-1.0/lang.js:204
>> @SammySettings.js:146
>> "'
>>      JS ERROR: !!!     message = '"Error invoking
>> GLib.file_set_contents: Failed to create file
>> '/sys/devices/platform/samsung/battery_life_extender.ZTDFIW': No such
>> file or directory"'
>
>
> Ok, I guess that file because it tries to create a temp file and then
> replace the original one with it. That doesn't work under /sys.
>
> I also tried something like:
>         let _batteryGFile = Gio.file_new_for_path(batteryFile);
>         let _batteryOut = _batteryGFile.replace(null, false,
> Gio.FileCreateFlags.NONE, null);
>         let _batteryStream = new Gio.DataOutputStream({ base_stream:
> _batteryOut});
>         _batteryStream.put_string("1", null);
>         _batteryStream.close(null);
>
> But it throws an error too:
>         JS ERROR: !!!     message = '"Error invoking Gio.close: Error
> writing to file: Invalid argument"'
>
> Both solutions work when I try to write a file in my home, hence they should
> be ok from the tpo pint of view.
>
> I am thinking of spawning a damn "echo 1" and get the work done, but it
> seems to be *horribly* bad. Anyone has a better idea? How am I supposed to
> change such config files according to some good programming rules?
>
> Thanks a lot!
>
> Alessandro
>
>
> _______________________________________________
> gnome-shell-list mailing list
> gnome-shell-list gnome org
> https://mail.gnome.org/mailman/listinfo/gnome-shell-list

/sys is a special filesystem, and it seems Gio has some trouble with
it. Keep in mind, writing into a file in /sys might give back an
"invalid argument" error if you're trying to set something in an
incorrect way.
Anyway, I can think of two solutions: use another library for writing
to files OR spawn an echo. Spawning a new process (shell with "echo 1
> /sys/something") is not THAT horrible - sure, it's not an elegant
solution but it's not really an overhead and it would work.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]