Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove trailing whitespaces from driver when converting #8

Closed
joaopluigi opened this issue Feb 21, 2019 · 11 comments
Closed

Remove trailing whitespaces from driver when converting #8

joaopluigi opened this issue Feb 21, 2019 · 11 comments

Comments

@joaopluigi
Copy link

joaopluigi commented Feb 21, 2019

Hey there,

I had some problems when trying to update my wslgit.sh to the last version 2019-02-08. Debugging it, I found that my variable MOUNTED_DRVFS was getting some trailing whitespaces when splitted on mount_len = split(_mount, mount_list, "\n");. For that reason, the result of, for example, to_unix_path_by_awk function would be something like:

/c <SPACE>/apps/cool_app

Which was leading to error.

I solved by removing the whitespaces using gsub on awk

# Usage: to_unix_path_by_awk "$path"
function to_unix_path_by_awk() {
	.
        .
        .
	for(i = 1; i <= mount_len ; i += 2 ) {
	  gsub(/ /, "", mount_list[i])
	  gsub(/ /, "", mount_list[i+1])
	  if(driver != mount_list[i]) continue;
	    suffix = substr($0, 3); gsub(/\\/, "/", suffix); gsub("//", "/", suffix);
	    print mount_list[i+1] suffix;
        .
        .
        .
}

I'm not sure if this was the correct approach. Also, I don't know why I was getting the trailing spaces.

See my changes on: https://github.com/hangxingliu/wslgit/compare/master...joaopluigi:fix/convertion-whitespace?expand=1

@hangxingliu
Copy link
Owner

hangxingliu commented Feb 21, 2019

@joaopluigi Thanks for your report. I am also curious why there are extra white characters.

Can you report some information to here too:

  1. The output of following commands:
mount -t drvfs
mount -t drvfs | base64 -
  1. Your Windows version and OS build code. (you can find them in Settings > System > About)

By the way, I may reply later because of my work.

@hangxingliu
Copy link
Owner

And I guess the problem is caused by function get_mounted_drvfs:

function get_mounted_drvfs() {
	# region need-to-be-replaced-in-unit-test
	#     The previous line is used for mark the following statments
	#     need to be replaced to other implementation for unit test (travis-CI)
	mount -t drvfs | awk '{
		if(split($0, lr, "type drvfs") < 2) next;
		if(split(lr[1], part, "on") < 2) next;

		drive = part[1];     gsub(/^\s/, "", drive);    gsub(/\s$/, "", drive);
		mount_to = part[2];  gsub(/^\s/, "", mount_to); gsub(/\s$/, "", mount_to);
		print toupper(drive) "\n" mount_to;
	}';
	# endregion need-to-be-replaced-in-unit-test
}

Maybe the \s in gsub(/^\s/, "", drive); gsub(/\s$/, "", drive); and gsub(/^\s/, "", mount_to); gsub(/\s$/, "", mount_to); should be replaced to \s+.

@joaopluigi
Copy link
Author

Thanks for the answer! These are the info you asked for:

$ mount -t drvfs
C: on /c type drvfs (rw,noatime,uid=1000,gid=1000,metadata,case=off)
$ mount -t drvfs | base64 -
Qzogb24gL2MgdHlwZSBkcnZmcyAocncsbm9hdGltZSx1aWQ9MTAwMCxnaWQ9MTAwMCxtZXRhZGF0
YSxjYXNlPW9mZikK

Windows version: 1809 (I'm pretty sure I was getting this error on 1803 also)
OS build: 17763.316

@hangxingliu
Copy link
Owner

@joaopluigi And could you report the output of following command to here to, thanks

awk --version

Because I found a Q&A on the StackOverflow:

More generally, you can use [[:space:]] to match a space, a tab or a newline (GNU Awk also supports \s), and [[:blank:]] to match a space or a tab.

https://stackoverflow.com/questions/29512854/how-to-define-a-space-in-a-regular-expression-in-awk

@joaopluigi
Copy link
Author

Ok, my awk is pointing to mawk

mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

compiled limits:
max NF             32767
sprintf buffer      2040

@hangxingliu
Copy link
Owner

hangxingliu commented Feb 21, 2019

And you can try replace the implement of function get_mounted_drvfs to:

function get_mounted_drvfs() {
	# region need-to-be-replaced-in-unit-test
	#     The previous line is used for mark the following statments
	#     need to be replaced to other implementation for unit test (travis-CI)
	mount -t drvfs | awk '
	function trim(s) { gsub(/^[[:space:]]+/, "", s); gsub(/[[:space:]]+$/, "", s); return s; }
	{
		if(split($0, lr, "type drvfs") < 2) next;
		if(split(lr[1], part, "on") < 2) next;

		drive = trim(part[1]); mount_to = trim(part[2]);
		print toupper(drive) "\n" mount_to;
	}';
	# endregion need-to-be-replaced-in-unit-test
}

And try again, It maybe working fine for you. @joaopluigi

@joaopluigi
Copy link
Author

This solution did not work @hangxingliu

@hangxingliu
Copy link
Owner

@joaopluigi Have you updated that wslgit.sh to your WSL? Because I replaced gawk to awk in the script and working fine. (But I forgot copy wslgit.sh into WSL before test succeed)


And the reason why don't replace space characters after get_mount_drvfs is because the wrong path will be generated if mount path contains space char. (Eg. /win disk/c, although basically no one will do like this)


P.s. And my next reply will be appear in here after a few hours. I have to take a nap

@joaopluigi
Copy link
Author

@joaopluigi Have you updated that wslgit.sh to your WSL? Because I replaced gawk to awk in the script and working fine. (But I forgot copy wslgit.sh into WSL before test succeed)

I did the change directly on /usr/bin/wslgit.sh and tried. It only worked when I added back the gsub workaround

And the reason why don't replace space characters after get_mount_drvfs is because the wrong path will be generated if mount path contains space char. (Eg. /win disk/c, although basically no one will do like this)

You're right. I agree

P.s. And my next reply will be appear in here after a few hours. I have to take a nap

Haha go for it, get some rest

@hangxingliu
Copy link
Owner

@joaopluigi Sorry for reply late. (I am busying on the work and graduation thesis recently) And i found the the root of problem about this issue in my scripts. and I will fix it in the latest commit.

The root of problem are:

  1. the awk binary in get_mount_drvfs is awk but not $AWK. so the result will be converted by gawk but not mawk if you installed gawk
    • This command could explain: readlink "$(readlink "$(which awk)")"
  2. mawk doesn't support [[:space]] in regex expression.

I will be grateful if you can give me a feedback of my latest commit for fix this issue.

@joaopluigi
Copy link
Author

It worked just fine!

Thanks for that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants