Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jruby.ext.etc;
  
  import jnr.posix.Passwd;
  import jnr.posix.Group;
 import org.jruby.Ruby;
 
 @JRubyModule(name="Etc")
 public class RubyEtc {
     public static RubyModule createEtcModule(Ruby runtime) {
         RubyModule etcModule = runtime.defineModule("Etc");
 
         runtime.setEtc(etcModule);
         
         etcModule.defineAnnotatedMethods(RubyEtc.class);
         
         definePasswdStruct(runtime);
         defineGroupStruct(runtime);
         
         return etcModule;
     }
     
     private static void definePasswdStruct(Ruby runtime) {
         IRubyObject[] args = new IRubyObject[] {
                 runtime.newString("Passwd"),
                 runtime.newSymbol("name"),
                 runtime.newSymbol("passwd"),
                 runtime.newSymbol("uid"),
                 runtime.newSymbol("gid"),
                 runtime.newSymbol("gecos"),
                 runtime.newSymbol("dir"),
                 runtime.newSymbol("shell"),
                 runtime.newSymbol("change"),
                 runtime.newSymbol("uclass"),
                 runtime.newSymbol("expire")
         };
         
         runtime.setPasswdStruct(RubyStruct.newInstance(runtime.getStructClass(), args.));
         if (runtime.is1_9()) {
             runtime.getEtc().defineConstant("Passwd"runtime.getPasswdStruct());
         }
     }
 
     private static void defineGroupStruct(Ruby runtime) {
         IRubyObject[] args = new IRubyObject[] {
                 runtime.newString("Group"),
                 runtime.newSymbol("name"),
                 runtime.newSymbol("passwd"),
                 runtime.newSymbol("gid"),
                 runtime.newSymbol("mem")
         };
         
         runtime.setGroupStruct(RubyStruct.newInstance(runtime.getStructClass(), args.));
         if (runtime.is1_9()) {
             runtime.getEtc().defineConstant("Group"runtime.getGroupStruct());
         }
     }
     
     private static IRubyObject setupPasswd(Ruby runtimePasswd passwd) {
         IRubyObject[] args = new IRubyObject[] {
                 runtime.newString(passwd.getLoginName()),
                 runtime.newString(passwd.getPassword()),
                 runtime.newFixnum(passwd.getUID()),
                 runtime.newFixnum(passwd.getGID()),
                 runtime.newString(passwd.getGECOS()),
                 runtime.newString(passwd.getHome()),
                 runtime.newString(passwd.getShell()),
                 runtime.newFixnum(passwd.getPasswdChangeTime()),
                 runtime.newString(passwd.getAccessClass()),
                 runtime.newFixnum(passwd.getExpire())
 
         };
         
         return RubyStruct.newStruct(runtime.getPasswdStruct(), args.);
     }
 
     
     private static IRubyObject setupGroup(Ruby runtimeGroup group) {
         IRubyObject[] args = new IRubyObject[] {
                 runtime.newString(group.getName()),
                 runtime.newString(group.getPassword()),
                 runtime.newFixnum(group.getGID()),
                intoStringArray(runtimegroup.getMembers())
        };
        
        return RubyStruct.newStruct(runtime.getGroupStruct(), args.);
    }
    private static IRubyObject intoStringArray(Ruby runtimeString[] members) {
        IRubyObject[] arr = new IRubyObject[members.length];
        for(int i = 0; i<arr.lengthi++) {
            arr[i] = runtime.newString(members[i]);
        }
        return runtime.newArrayNoCopy(arr);
    }
    @JRubyMethod(name = "getpwuid", optional=1, module = true)
    public static IRubyObject getpwuid(IRubyObject recvIRubyObject[] args) {
        Ruby runtime = recv.getRuntime();
        POSIX posix = runtime.getPosix();
        try {
            int uid = args.length == 0 ? posix.getuid() : RubyNumeric.fix2int(args[0]);
            Passwd pwd = posix.getpwuid(uid);
            if(pwd == null) {
                if (.) {  // MRI behavior
                    return recv.getRuntime().getNil();
                }
                throw runtime.newArgumentError("can't find user for " + uid);
            }
            return setupPasswd(runtimepwd);
        } catch (RaiseException re) {
            if (runtime.getNotImplementedError().isInstance(re.getException())) {
                return runtime.getNil();
            }
            throw re;
        } catch (Exception e) {
            if (runtime.getDebug().isTrue()) {
                runtime.getWarnings().warn(."Etc.getpwuid is not supported by JRuby on this platform");
            }
            return runtime.getNil();
        }
    }
    @JRubyMethod(name = "getpwnam", required=1, module = true)
    public static IRubyObject getpwnam(IRubyObject recvIRubyObject name) {
        Ruby runtime = recv.getRuntime();
        String nam = name.convertToString().toString();
        try {
            Passwd pwd = runtime.getPosix().getpwnam(nam);
            if(pwd == null) {
                if (.) {  // MRI behavior
                    return runtime.getNil();
                }
                throw runtime.newArgumentError("can't find user for " + nam);
            }
            return setupPasswd(recv.getRuntime(), pwd);
        } catch (Exception e) {
            if (runtime.getDebug().isTrue()) {
                runtime.getWarnings().warn(."Etc.getpwnam is not supported by JRuby on this platform");
            }
            return runtime.getNil();
        }
    }
    @JRubyMethod(module = true)
    public static IRubyObject passwd(IRubyObject recvBlock block) {
        Ruby runtime = recv.getRuntime();
        POSIX posix = runtime.getPosix();
        try {
            // call getpwent to fail early if unsupported
            posix.getpwent();
            if(block.isGiven()) {
                ThreadContext context = runtime.getCurrentContext();
                
                if (!.compareAndSet(falsetrue)) {
                    throw runtime.newRuntimeError("parallel passwd iteration");
                }
                
                posix.setpwent();
                try {
                    Passwd pw;
                    while((pw = posix.getpwent()) != null) {
                        block.yield(contextsetupPasswd(runtimepw));
                    }
                } finally {
                    posix.endpwent();
                    .set(false);
                }
            }
            Passwd pw = posix.getpwent();
            if (pw != null) {
                return setupPasswd(runtimepw);
            } else {
                return runtime.getNil();
            }
        } catch (Exception e) {
            if (runtime.getDebug().isTrue()) {
                runtime.getWarnings().warn(."Etc.passwd is not supported by JRuby on this platform");
            }
            return runtime.getNil();
        }
    }
    @JRubyMethod(name = "getlogin", module = true)
    public static IRubyObject getlogin(IRubyObject recv) {
        Ruby runtime = recv.getRuntime();
        try {
            String login = runtime.getPosix().getlogin();
            if (login != null) {
                return runtime.newString(login);
            }
            login = System.getenv("USER");
            if (login != null) {
                return runtime.newString(login);
            }
            
            return runtime.getNil();
        } catch (Exception e) {
            // fall back on env entry for USER
            return runtime.newString(System.getProperty("user.name"));
        }
    }
    @JRubyMethod(name = "endpwent", module = true)
    public static IRubyObject endpwent(IRubyObject recv) {
        Ruby runtime = recv.getRuntime();
        try {
            runtime.getPosix().endpwent();
        } catch (Exception e) {
            if (runtime.getDebug().isTrue()) {
                runtime.getWarnings().warn(."Etc.endpwent is not supported by JRuby on this platform");
            }
        }
        return runtime.getNil();
    }
    @JRubyMethod(name = "setpwent", module = true)
    public static IRubyObject setpwent(IRubyObject recv) {
        Ruby runtime = recv.getRuntime();
        try {
            runtime.getPosix().setpwent();
        } catch (Exception e) {
            if (runtime.getDebug().isTrue()) {
                runtime.getWarnings().warn(."Etc.setpwent is not supported by JRuby on this platform");
            }
        }
        return runtime.getNil();
    }
    @JRubyMethod(name = "getpwent", module = true)
    public static IRubyObject getpwent(IRubyObject recv) {
        Ruby runtime = recv.getRuntime();
        try {
            Passwd passwd = runtime.getPosix().getpwent();
            if (passwd != null) {
                return setupPasswd(runtimepasswd);
            } else {
                return runtime.getNil();
            }
        } catch (Exception e) {
            if (runtime.getDebug().isTrue()) {
                runtime.getWarnings().warn(."Etc.getpwent is not supported by JRuby on this platform");
            }
            return runtime.getNil();
        }
    }
    @JRubyMethod(name = "getgrnam", required=1, module = true)
    public static IRubyObject getgrnam(IRubyObject recvIRubyObject name) {
        Ruby runtime = recv.getRuntime();
        String nam = name.convertToString().toString();
        try {
            Group grp = runtime.getPosix().getgrnam(nam);
            if(grp == null) {
                if (.) {  // MRI behavior
                    return runtime.getNil();
                }
                throw runtime.newArgumentError("can't find group for " + nam);
            }
            return setupGroup(runtimegrp);
        } catch (Exception e) {
            if (runtime.getDebug().isTrue()) {
                runtime.getWarnings().warn(."Etc.getgrnam is not supported by JRuby on this platform");
            }
            return runtime.getNil();
        }
    }
    @JRubyMethod(name = "getgrgid", optional=1, module = true)
    public static IRubyObject getgrgid(IRubyObject recvIRubyObject[] args) {
        Ruby runtime = recv.getRuntime();
        POSIX posix = runtime.getPosix();
        try {
            int gid = args.length == 0 ? posix.getgid() : RubyNumeric.fix2int(args[0]);
            Group gr = posix.getgrgid(gid);
            if(gr == null) {
                if (.) {  // MRI behavior
                    return runtime.getNil();
                }
                throw runtime.newArgumentError("can't find group for " + gid);
            }
            return setupGroup(runtimegr);
        } catch (RaiseException re) {
            throw re;
        } catch (Exception e) {
            if (runtime.getDebug().isTrue()) {
                runtime.getWarnings().warn(."Etc.getgrgid is not supported by JRuby on this platform");
            }
            return runtime.getNil();
        }
    }
    @JRubyMethod(name = "endgrent", module = true)
    public static IRubyObject endgrent(IRubyObject recv) {
        Ruby runtime = recv.getRuntime();
        try {
            runtime.getPosix().endgrent();
        } catch (Exception e) {
            if (runtime.getDebug().isTrue()) {
                runtime.getWarnings().warn(."Etc.engrent is not supported by JRuby on this platform");
            }
        }
        return runtime.getNil();
    }
    @JRubyMethod(module = true)
    public static IRubyObject setgrent(IRubyObject recv) {
        Ruby runtime = recv.getRuntime();
        try {
            runtime.getPosix().setgrent();
        } catch (Exception e) {
            if (runtime.getDebug().isTrue()) {
                runtime.getWarnings().warn(."Etc.setgrent is not supported by JRuby on this platform");
            }
        }
        return runtime.getNil();
    }
    @JRubyMethod(module = true)
    public static IRubyObject group(IRubyObject recvBlock block) {
        Ruby runtime = recv.getRuntime();
        POSIX posix = runtime.getPosix();
        try {
            // try to read grent to fail fast
            posix.getgrent();
        } catch (Exception e) {
            if (runtime.getDebug().isTrue()) {
                runtime.getWarnings().warn(."Etc.group is not supported by JRuby on this platform");
            }
        }
        if (block.isGiven()) {
            Boolean blocking = (Boolean)recv.getInternalVariables().getInternalVariable("group_blocking");
            if (blocking != null && blocking) {
                throw runtime.newRuntimeError("parallel group iteration");
            }
            try {
                recv.getInternalVariables().setInternalVariable("group_blocking"true);
                ThreadContext context = runtime.getCurrentContext();
                posix.setgrent();
                Group gr;
                while((gr = posix.getgrent()) != null) {
                    block.yield(contextsetupGroup(runtimegr));
                }
            } finally {
                posix.endgrent();
                recv.getInternalVariables().setInternalVariable("group_blocking"false);
            }
        } else {
            Group gr = posix.getgrent();
            if (gr != null) {
                return setupGroup(runtimegr);
            } else {
                return runtime.getNil();
            }
        }
        return runtime.getNil();
    }
    @JRubyMethod(name = "getgrent", module = true)
    public static IRubyObject getgrent(IRubyObject recv) {
        Ruby runtime = recv.getRuntime();
        try {
            Group gr = runtime.getPosix().getgrent();
            if (gr != null) {
                return setupGroup(recv.getRuntime(), gr);
            } else {
                return runtime.getNil();
            }
        } catch (Exception e) {
            if (runtime.getDebug().isTrue()) {
                runtime.getWarnings().warn(."Etc.getgrent is not supported by JRuby on this platform");
            }
            return runtime.getNil();
        }
    }
    
    @JRubyMethod(name = "systmpdir", module = true, compat = .)
    public static IRubyObject systmpdir(ThreadContext contextIRubyObject recv) {
        Ruby runtime = context.getRuntime();
        ByteList tmp = ByteList.create(System.getProperty("java.io.tmpdir")); // default for all platforms except Windows
        
        if (.) {
            String commonAppData = System.getenv("CSIDL_COMMON_APPDATA");
            // TODO: need fallback mechanism
            if (commonAppData != nulltmp = ByteList.create(commonAppData);
        }
        RubyString ret = RubyString.newString(runtimetmpruntime.getDefaultExternalEncoding());
        ret.untaint(context);
        ret.trust(context);
        
        return ret;
    }
    
    @JRubyMethod(name = "sysconfdir", module = true, compat = .)
    public static IRubyObject sysconfdir(ThreadContext contextIRubyObject recv) {
        Ruby runtime = context.getRuntime();
        ByteList tmp = ByteList.create(RbConfigLibrary.getSysConfDir(runtime)); // default for all platforms except Windows
        
        if (.) {
            String localAppData = System.getenv("CSIDL_LOCAL_APPDATA");
            // TODO: need fallback mechanism
            if (localAppData != nulltmp = ByteList.create(localAppData);
        }
        RubyString ret = RubyString.newString(runtimetmpruntime.getDefaultExternalEncoding());
        ret.untaint(context);
        ret.trust(context);
        
        return ret;
    }
    
    private static final AtomicBoolean iteratingPasswd = new AtomicBoolean(false);
New to GrepCode? Check out our FAQ X